Answer the question
In order to leave comments, you need to log in
Yii GridView, array output?
Good night, tell me Yii experts.
There is a directory of disks (Drivers), each disk has genres (genres) (a disk can belong to several genres).
Connected them via many_to_many
public function relations()
{
return array(
'genres' => array(self::MANY_MANY, 'Genres', 'drives_in_genres(driver_id,genre_id)'),
);
}
And I can't figure out how to display a list of all disk categories in the GridView, I tried through the filter to none, everything says that the array cannot be overtaken into a string.
here is part of the code...
<?php $this->widget('zii.widgets.grid.CGridView', array(<br/>
'id'=>'drives-grid',<br/>
'dataProvider'=>$model->search(),<br/>
'filter'=>$model,<br/>
'columns'=>array(<br/>
'id',<br/>
'name',<br/>
'year',<br/>
array(<br/>
'name' => 'user_id',<br/>
'value'=> '$data->user->username',<br/>
<br/>
),<br/>
array(<br/>
'name' => 'genres',<br/>
'value'=> '$data->genres->name',<br/>
<br/>
),<br/>
array(<br/>
'class'=>'CButtonColumn',<br/>
),<br/>
),<br/>
)); ?><br/>
<br/>
Answer the question
In order to leave comments, you need to log in
Alternatively, you can create a method in the Drivers model that will convert an array of Genres models to a string, for example:
public function getGenresText($sSep = ', ')
$aRes = array();
foreach ($this->genres as $itm) {
$aRes[] = $itm->name;
}
return implode($sSep, $aRes);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question