Getting data between 3 tables in Yii , cgridview

Asked

Viewed 834 times

3

I am using the Yii framework and I need the information that is in the route table. I’m looking for this information from the vehicle table. There is a third table called equipment In vehicle I have a foreign key called outfit_fk En route I have a foreign key for equipment (outfit_fk) Vehicle relation:

'cliente' => array(self::BELONGS_TO, 'Cliente', 'Cliente'),
'equipamento0' => array(self::BELONGS_TO, 'Equipamento', 'equipamento'),
'motorista0' => array(self::BELONGS_TO, 'Motorista', 'motorista'),

The doubt is how from vehicle I can get the data that are in the route table. And use this information with cgridview.

Exemplo / duvida relation com 3 tabelas

2 answers

2


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).

0

It would be something like this:

$equipamentos = $model->Equipamento;

foreach($equipamentos as $equipamento) {
    echo 'codigo_equipamento:'.$equipamento->id.'<br/>';
    echo '<hr />';

}

thus being able to do for other related tables.

  • 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.

  • Voce having the model loaded with the correct fks ... Voce can get these other data understood

Browser other questions tagged

You are not signed in. Login or sign up in order to post.