2015-09-28 13 views
6

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; 
} 
+0

Potrebbe essere il nome della colonna con errori di ortografia. –

+0

@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

+0

Hai una riga $ query-> con (['agente', 'padre']) o qualcosa del genere? Puoi mostrare l'intera fonte della query? – robsch

risposta

13

Hai bisogno di fare a seguito di variazioni nel modello. dalla clausola in realtà sta creando un alias. la relazione agente e padre verrà scelta in clausole di join separate. Utilizza l'alias "agente" e "padre" nei criteri del filtro con i nomi delle colonne.

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()]) 
} 

Un'altra cosa da cambiare è

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

Buono! Funziona :) Grazie! – garsim

1

Un'alternativa alla risposta di @FidoXLNC potrebbe essere per definire l'alias quando stai facendo il join:

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

Ma per quanto ne so devi specificare entrambe le relazioni, non solo una sola.

Problemi correlati