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;
}
可能是你有拼寫錯誤的列名稱。 –
@InsaneSkulll如果我使用「agent」作爲列名稱(現在我使用「father」,關係的名稱)搜索過濾第一個關係(代理)的數據:/ – garsim
你有一行$ query->與(['代理人','父親])或類似的東西?你能顯示查詢的完整來源嗎? – robsch