Hi all, I try to implement possibilities for user to sort and filter columns displaying field from a many_many relation. I try to follow this wiki http://www.yiiframework.com/wiki/385/displaying-sorting-and-filtering-hasmany-manymany-relations-in-cgridview/#hh9 but it only works for relation “belongs_to” that the author use though … anyway I succeeded to do it for many_many relation however I have problems with pager… Grid doesn’t display all my models although wiki…
Let’s take an example :
I have three tables : author(id,name) / author_genre(id,author_id,genre_id) / genre(id,genre_name)
relations :
in author model : "genres" => array(self::MANY_MANY, "genre", "author_genre(author_id,genre_id)");
in genre : "authors" => array(self::MANY_MANY, "author", "author_genre(genre_id,author_id)");
in author_genre : "genre" => array(self::BELONGS_TO, "genre", "genre_id");
"author" => array(self::BELONGS_TO, "author", "author_id");
I want to display in author gridview his genres and his name as follow :
[...]
'dataProvider'=>$model->search(),
[...]
'columns'=>array(
'name',
array('name'=>'hisGenre','type'=>'raw','filter'=>CHtml::listData(genre::model()->findAll(array('order'=>'name ASC')),'id','genre_name'),'value'=>'$data->getGenreForAuthor()'),
[...]
-
with getGenreForAuthor() which returns a string with all author’s genre separate by a comma. I do it with a simple Chtml::listData and an implode.
-
hisGenre is a public variable declared in model author, added in rules for safe and search "on".
-
here is $model->search() for author:
$criteria=new CDbCriteria;
$criteria->with=array('genres');
$criteria->group='t.id, genres.id';
$criteria->together=true;
$criteria->compare('t.id',$this->id);
$criteria->compare('t.name',$this->name,true);
$criteria->compare('genres.id',$this->hisGenre);
return new CActiveDataProvider(get_class($this), array(
'pagination'=>array(
'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),
'criteria'=>$criteria,
'sort'=>array('defaultOrder'=>'t.id ASC','attributes'=>array('hisGenre'=>array('asc'=>'genres.name','desc'=>'genres.name DESC'),'*')),
));
With that, I can filter and sort my “hisGenre” column… but the problem comes from pager… grid doesn’t display all entries…
Therefor in wiki :
but seems to not work. I use postgres btw. Another thing, I try with lazy loading but filter and sort do not work… and I tried keenLoading as well but I didn’t get it working.
Can you heelp =)