Как получить данные из другой таблицы в CGridView?

У меня 3 стола. car_types:

id | main_image | title

car_type_classifiers:

id | car_type_id | classifier_id

классификаторы:

id | class

Я хочу отобразить CGridView, чтобы были столбцы: Заголовок | класс. Но классов может быть много для одного car_type. Я пытался искать в Интернете, но не мог понять эти $criteria->compare() функции в функции поиска модели.

Я хочу, чтобы они отображались в виде небольших списков. Как я могу это сделать? мой взгляд:

    $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'car-type-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
        'columns' => array(
                'title'
    ),
));

мой контроллер:

public function actionIndex()
{
    $model=new EeCarTypes('search');
    $model->unsetAttributes();

    $this->render('index',array('model'=>$model));
}

и моя модель:

public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('car_type',$this->car_type,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

person Cirvis    schedule 30.07.2014    source источник


Ответы (1)


Я предполагаю, что у вас с моделью хорошие отношения.

На ваш взгляд:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'car-type-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns' => array(
        'title',
        array(            
            'name'=>'Class',
            'type' => 'raw',
            //call the method 'gridDataColumn' from the controller
            'value'=>array($this,'gridDataColumn'), 
        ), 
    ),
));

В вашем контроллере:

//called on rendering the column for each row 
protected function gridDataColumn($data,$row)
{
    $cellValue = "<ul>";
    foreach($data->classifiers as $classifier) {
        $cellValue .= "<li>" . $classifier->class . '</li>';
    }
     $cellValue .= "</ul>";
    return $cellValue;    
}  
person darkheir    schedule 30.07.2014