Pagination with a model's database relation

Hi all. I'm trying to paginate questions that belong to a user. I have created $model->numQuestions as self::STAT relation, and I have access to $model->questions. The thing is, I need to somehow add the $criteria to the $model->questions so that it returns only the rows now showing in pagination (instead of all questions).

Please see my code below:



		$model = $this->loaduser();





		$criteria=new CDbCriteria;





		$pages=new CPagination($model->numQuestions);


		$pages->pageSize=self::PAGE_SIZE;


		$pages->applyLimit($criteria);





		$questions = $model->questions( array('condition'=>$criteria) );


The last line is giving me problems. How do I insert the additional $criteria to the questions? This line does not work ATM.

Any help appreciated!

This is not the right usage. You should use

$questions=$model->questions($criteria->toArray());

I have similar question:


    public function actionShow()

	{

	    $post=$this->loadPost();

	    $comment=$this->newComment($post);


            $criteria=new CDbCriteria;

            $criteria->params = array('??.status' => COMMENT::STATUS_APPROVED) ; /* !!! */


            $pages=new CPagination(Comment::model()->count($criteria));

            $pages->pageSize=self::PAGE_SIZE;

            $pages->applyLimit($criteria);


            $comments = $post->comments($criteria->toArray());


	    $this->render('show',array(

		'model'=>$post,

		'comments'=>$comments,

		'newComment'=>$comment,

                'pages' => $pages

	    ));

	}



Params for comments won’t work. What is wrong ? Thanks.

update:


       public function actionShow()

	{

	    $post=$this->loadPost();

	    $comment=$this->newComment($post);


            $criteria=new CDbCriteria;

            $criteria->condition = 'status=' . COMMENT::STATUS_APPROVED;


            $pages=new CPagination(Comment::model()->count($criteria));

            $pages->pageSize=self::PAGE_SIZE;

            $pages->applyLimit($criteria);


            $criteria=new CDbCriteria;

            $criteria->condition = 'comments.status = ' . COMMENT::STATUS_APPROVED;


            $comments = $post->comments($criteria->toArray());


	    $this->render('show',array(

		'model'=>$post,

		'comments'=>$comments,

		'newComment'=>$comment,

                'pages' => $pages

	    ));

	}

And in model Post.php in relation for comments “‘alias’ => ‘comments’” .

Everything works :)

[b]

update2:[/b] Not everything :) We need no initializing of second criteria, only change of condition


$criteria->condition = 'comments.status = ' . COMMENT::STATUS_APPROVED;

instead




$criteria=new CDbCriteria;

$criteria->condition = 'comments.status = ' . COMMENT::STATUS_APPROVED;