2015-09-28 85 views
6

Yii 2關係表格存在問題。我的工作有很多關係,但只有在這種情況下返回我一個錯誤:Yii 2:與同一表格的多重關係

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'father.name' in 'where clause'

我認爲這個問題是與同表「代理」的雙重關係。看到模型中的一段代碼:

public function getAgent() 
{ 
    return $this->hasOne(Agent::className(), ['id' => 'id_agent']); 
} 
public function getFather() 
{ 
    return $this->hasOne(Agent::className(), ['id' => 'id_father']); 
} 

在我的GridView我看到正確的價值觀,但是當我試圖用命令或用「andWhere」來過濾,Yii2返回錯誤。

下面你可以找到的searchModel的一段代碼:

$dataProvider->sort->attributes['agentName'] = [ 
     'asc' => ['agent.name' => SORT_ASC], 
     'desc' => ['agent.name' => SORT_DESC], 
     'default' => SORT_ASC 
    ]; 

$dataProvider->sort->attributes['fatherName'] = [ 
     //'asc' => ['father.name' => SORT_ASC], 
     //'desc' => ['father.name' => SORT_DESC], 
     'default' => SORT_ASC 
    ]; 
//....... 
$query->andFilterWhere(['like', 'agent.name', $this->agentName]); 
$query->andFilterWhere(['like', 'father.name', $this->fatherName]); 

的AGENTNAME屬性工作正常。 有什麼建議嗎? 謝謝!

-------更新:更多的代碼--------- searchModel:

public function search($params) 
{ 
    $agent_aux = new Agent(); 
    $agent_id= $agent_aux->getAgentIdFromUser(); 

    if (Yii::$app->user->can('admin')){ 
     $query = Contract::find(); 
    } 
    else{ 

     $query = Contract::find()->where(['contract.agent_id' => $agent_id]);    
    } 

    $query->joinWith(['agent','seminar']); 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 
    $this->load($params); 
    $dataProvider->sort->attributes['seminar_location'] = [ 
     'asc' => ['seminar.location' => SORT_ASC], 
     'desc' => ['seminar.location' => SORT_DESC], 
    ]; 
    $dataProvider->sort->attributes['agentName'] = [ 
     'asc' => ['agent.name' => SORT_ASC], 
     'desc' => ['agent.name' => SORT_DESC], 
     'default' => SORT_ASC 
    ]; 

    $dataProvider->sort->attributes['fatherName'] = [ 
     //'asc' => ['father.name' => SORT_ASC], 
     //'desc' => ['father.name' => SORT_DESC], 
     'default' => SORT_ASC 
    ]; 
    if (!$this->validate()) { 
     return $dataProvider; 
    } 
    $query->andFilterWhere([ 
     'id' => $this->id, 
     'data' => $this->data, 
     'id_agent' => $this->id_agent, 
     'id_father' => $this->id_father, 
     'id_seminar' => $this->id_seminar, 
    ]); 
    $query->andFilterWhere(['like', 'agent.name', $this->agentName]); 
    $query->andFilterWhere(['like', 'father.name', $this->fatherName]); 
    return $dataProvider; 
} 
+0

可能是你有拼寫錯誤的列名稱。 –

+0

@InsaneSkulll如果我使用「agent」作爲列名稱(現在我使用「father」,關係的名稱)搜索過濾第一個關係(代理)的數據:/ – garsim

+0

你有一行$ query->與(['代理人','父親])或類似的東西?你能顯示查詢的完整來源嗎? – robsch

回答

13

你需要做以下模型的變化。 from子句實際上是創建一個別名。代理人和父親關係將在獨立連接條款中被挑選出來。在列名稱的過濾條件中使用「agent」和「father」別名。

public function getAgent() 
{ 
    return $this->hasOne(Agent::className(), ['id' => 'id_agent'])->from(['agent' => Agent::tableName()]); 
} 

public function getFather() 
{ 
    return $this->hasOne(Agent::className(), ['id' => 'id_father'])->from(['father' => Agent::tableName()]) 
} 

另一件事改變的是

$query->joinWith(['agent','seminar', 'father']); 
+0

好!它的工作原理:) 謝謝! – garsim

1

到@FidoXLNC的答案另一種可能是定義當你做別名的加入:

$query->joinWith([ 
    'seminar', 
    'agent' => function ($q) { $q->from(Agent::tableName() . ' agent'); }, 
    'father' => function ($q) { $q->from(Agent::tableName() . ' father'); } 
]); 

但據我所知你必須指定兩個關係,而不僅僅是一個。