Ho un problema con le tabelle di relazione Yii 2. Il mio lavoro ha molti rapporti, ma solo in questo caso mi restituisce un errore:Yii 2: relazione multipla con la stessa tabella
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'father.name' in 'where clause'
Credo che il problema è il doppio rapporto con la stessa tabella "Agent". Vedere il pezzo di codice nel modello:
public function getAgent()
{
return $this->hasOne(Agent::className(), ['id' => 'id_agent']);
}
public function getFather()
{
return $this->hasOne(Agent::className(), ['id' => 'id_father']);
}
Nel mio GridView vedo i valori corretti, ma quando cerco di filtrare con ordine o con "andwhere", Yii2 restituisce l'errore.
Qui di seguito potete trovare il pezzo di codice per la 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]);
attributi L'AgentName funziona bene. Qualche suggerimento? Grazie!
------- UPDATE: più codice --------- 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;
}
Potrebbe essere il nome della colonna con errori di ortografia. –
@InsaneSkulll se uso "agente" come nome di colonna (ora uso "padre", il nome della relazione) la ricerca filtra i dati con la prima relazione (Agente):/ – garsim
Hai una riga $ query-> con (['agente', 'padre']) o qualcosa del genere? Puoi mostrare l'intera fonte della query? – robsch