E
E
enchikiben2012-01-19 21:53:49
Yii
enchikiben, 2012-01-19 21:53:49

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

&lt;?php $this-&gt;widget('zii.widgets.grid.CGridView', array(<br/>
 'id'=&gt;'drives-grid',<br/>
 'dataProvider'=&gt;$model-&gt;search(),<br/>
 'filter'=&gt;$model,<br/>
 'columns'=&gt;array(<br/>
 'id',<br/>
 'name',<br/>
 'year',<br/>
 array(<br/>
 'name' =&gt; 'user_id',<br/>
 'value'=&gt; '$data-&gt;user-&gt;username',<br/>
 <br/>
 ),<br/>
 array(<br/>
 'name' =&gt; 'genres',<br/>
 'value'=&gt; '$data-&gt;genres-&gt;name',<br/>
 <br/>
 ),<br/>
 array(<br/>
 'class'=&gt;'CButtonColumn',<br/>
 ),<br/>
 ),<br/>
)); ?&gt;<br/>
<br/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nikel303, 2012-01-20
@EnChikiben

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);
}

and call this method in the grid.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question