From what I understand you want to put in the CGridView
information from a third table. From what I noticed you have a relationship N to N
amid Veículos
and Rotas
.
According to the reference (in English) of the Framework wiki you will have to:
1º) In the Vehicle model you must add a variable:
public $rotas_search;
2º) Change the function rules()
to add the new field to the list of "Safe on Search".
public function rules() {
return array(
...
array( '........,rotas_search', 'safe', 'on'=>'search' ),
);
}
3º) It is now necessary to add the field in your method search()
public function search() {
$criteria = new CDbCriteria;
$criteria->with = array( 'equipamento0' );
...
//Onde rotas é a relação entre a tabela equipamento e a tabela rota
//rotas[0] indica que vai pegar a primeira rota da lista.
$criteria->compare( 'equipamento0.rotas[0].id', $this->rotas_search, true );
...
}
4º) Then, at the end of the method search
you modify the return by adding your new field.
return new CActiveDataProvider( $this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'rotas_search'=>array(
'asc'=>'equipamento0.rotas[0].id',
'desc'=>'equipamento0.rotas[0].id DESC',
),
'*',
),
),
));
5º) Finally in its CGridView
, add the column.
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
array( 'name'=>'rotas_search', 'value'=>'$data->equipamento0->rotas[0]->id' ),
...
),
));
Note that it only works for displaying a route value. You can find more information and other approaches to this problem here(in English). And here(English) you will have another approach of how to put more than one field in the display(I found it somewhat gambiarra, but if it works and does not bring problems, then it is a solution).
I don’t understand your answer. I need to do in order to add what I get from the route table in the criteria(with) to send and use with cgridview. something like $data->route->campo_desired.
– Caique Oliveira
Voce having the model loaded with the correct fks ... Voce can get these other data understood
– Otto