Reputation: 398
I have 3 tables. car_types:
id | main_image | title
car_type_classifiers:
id | car_type_id | classifier_id
classifiers:
id | class
I want to display a CGridView so there are columns: Title | class. But Classes can be many for one car_type. I tried to search online, but couldn't understand those $criteria->compare()
functions in model search function.
I want those to show up as little lists. How can I do that? my view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'car-type-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns' => array(
'title'
),
));
my controller:
public function actionIndex()
{
$model=new EeCarTypes('search');
$model->unsetAttributes();
$this->render('index',array('model'=>$model));
}
and my 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,
));
}
Upvotes: 0
Views: 83
Reputation: 8960
I assume you model has the good relations.
In your view:
$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'),
),
),
));
In your controller:
//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;
}
Upvotes: 1