title: Yii 2: multiple relation with same table
body: <p>I've got a problem with Yii 2 Relation Tables. My work has many relations, but only in this case return me an error:</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'father.name' in 'where clause'</p>
  </blockquote>
  
  <p>I think the problem is the double relation with the same Table "Agent". See the piece of code in the model:</p>
  
  <pre><code>public function getAgent()
  {
      return $this-&gt;hasOne(Agent::className(), ['id' =&gt; 'id_agent']);
  }
  public function getFather()
  {
      return $this-&gt;hasOne(Agent::className(), ['id' =&gt; 'id_father']);
  }
  </code></pre>
  
  <p>In my GridView I see the correct values, but when I try to filter with ORDER or with "andWhere", Yii2 returns the error.</p>
  
  <p>Below you can find the piece of code for the searchModel:</p>
  
  <pre><code>$dataProvider-&gt;sort-&gt;attributes['agentName'] = [ 
          'asc' =&gt; ['agent.name' =&gt; SORT_ASC],
          'desc' =&gt; ['agent.name' =&gt; SORT_DESC],
          'default' =&gt; SORT_ASC
      ];
  
  $dataProvider-&gt;sort-&gt;attributes['fatherName'] = [
          //'asc' =&gt; ['father.name' =&gt; SORT_ASC],
          //'desc' =&gt; ['father.name' =&gt; SORT_DESC],
          'default' =&gt; SORT_ASC
      ];
  //.......
  $query-&gt;andFilterWhere(['like', 'agent.name', $this-&gt;agentName]);
  $query-&gt;andFilterWhere(['like', 'father.name', $this-&gt;fatherName]);
  </code></pre>
  
  <p>The agentName attributes works fine.
  Any suggestion?
  Thank you!</p>
  
  <p><strong>-------UPDATE: more code---------</strong>
  searchModel:</p>
  
  <pre><code>public function search($params)
  {
      $agent_aux = new Agent();
      $agent_id= $agent_aux-&gt;getAgentIdFromUser();
  
      if (Yii::$app-&gt;user-&gt;can('admin')){
          $query = Contract::find();
      }
      else{
  
          $query = Contract::find()-&gt;where(['contract.agent_id' =&gt; $agent_id]);            
      }
  
      $query-&gt;joinWith(['agent','seminar']);
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
      $this-&gt;load($params);
      $dataProvider-&gt;sort-&gt;attributes['seminar_location'] = [
          'asc' =&gt; ['seminar.location' =&gt; SORT_ASC],
          'desc' =&gt; ['seminar.location' =&gt; SORT_DESC],
      ];
      $dataProvider-&gt;sort-&gt;attributes['agentName'] = [ 
          'asc' =&gt; ['agent.name' =&gt; SORT_ASC],
          'desc' =&gt; ['agent.name' =&gt; SORT_DESC],
          'default' =&gt; SORT_ASC
      ];
  
      $dataProvider-&gt;sort-&gt;attributes['fatherName'] = [
          //'asc' =&gt; ['father.name' =&gt; SORT_ASC],
          //'desc' =&gt; ['father.name' =&gt; SORT_DESC],
          'default' =&gt; SORT_ASC
      ];
      if (!$this-&gt;validate()) {
          return $dataProvider;
      }
      $query-&gt;andFilterWhere([
          'id' =&gt; $this-&gt;id,
          'data' =&gt; $this-&gt;data,
          'id_agent' =&gt; $this-&gt;id_agent,
          'id_father' =&gt; $this-&gt;id_father,
          'id_seminar' =&gt; $this-&gt;id_seminar,
      ]);
      $query-&gt;andFilterWhere(['like', 'agent.name', $this-&gt;agentName]);
      $query-&gt;andFilterWhere(['like', 'father.name', $this-&gt;fatherName]);
      return $dataProvider;
  }
  </code></pre>