Pagination Not Working.

Hi,

On a forum app I need to select the topics with its last post and the topics have to be ordered by its last post created date.

I’m using:




$topicsDP = new CActiveDataProvider('Topic', array(

  'criteria'=>array(

    'together'=>true,

    'condition'=>Yii::app()->db->quoteColumnName('t.forum_id').'=:fid',

    'params'=>array(':fid'=>$forumID),

    'with'=>array(

      'posts'=>array(

        'joinType'=>'INNER JOIN',

        'order'=>Yii::app()->db->quoteColumnName('posts.created').' DESC',

        'limit'=>1,

        'distinct'=>true,

      ),

    ),

  ),

  'pagination'=>array(

    'pageSize'=>3,

  ),

));



When testing I have 4 topics, the first page is ok but the second isn’t. I think it is because the “OFFSET 3” is also affecting the posts select.

How can I do this?

Also, all posts are beeing select, is there some way of only selecting the last one?

Thanks.

Hi NCS_One,

I think that you have to do the lazy loading for the post objects.

You are now doing the eager loading … joining topic table and post table. In this case ‘order’, ‘limit’ and ‘offset’ will be applied to the virtually joined single table, not to the post table (nor to the topic table).

And when we use CActiveDataProvider with CPagination, it will automatically insert the ‘offset’ and ‘limit’ clauses according to the demand of the pagination, and they will overwrite your own ones.

I hope these wiki articles will be a little help.

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/

http://www.yiiframework.com/wiki/428/drills-search-by-a-has_many-relation/