[MVC] Best practices: where to put CPagination code, controller or model?

Hi,

I have extended my model, let’s assume it’s Article, with “findActive” method:




public function findActive()

{

		$criteria = new CDbCriteria;		

		$criteria->conditon = "status='active'";

				

		return $this->findAll($criteria);

}



so that there’s no Criteria definition in Controller, just a call to this method.

now I need to use CPagination on these results. Which means I need an access to CDbCriteria, in order to get count & apply the limit / offset, before calling findAll.

So, should I change the function to return prepared criteria instead of actual data set? Than I could do "pagination-related" operations in Controller and later simply call findAll.

I’m pretty sure putting pagination-related code directly inside model doesn’t make much sense (I need to pass pagination object back to the view).

What is your solution to follow MVC pattern and keep the code clean and reusable in this case?

The solution is the one implemented in the search function of the model:

Instead of return simply a criteria, you can return a cActiveDataProvider, wich incorporates pagination and sorting.

Take a look at how works the action admin of a gii generated crud + the function search of a gii generated active record, is exactly what you are looking for.