How to paginate using DAO

Hi everyone,

The sample application generated by Yii shell uses ActiveRecord to query the database.

The pagination works perfectly.

I tried changing it to using DAO but I cant get pagination to work.

Using AR way,

This line of code select the results for a page.

   $pages->applyLimit($criteria);

$pages is an instance of CPagination and $criteria is an instance of CDbCriteria.

Using DAO way, I am using CDbCommand.

I can’t use the applyLimit() method in CPagination.

As a result, I get the same data for every page.

I am puzzled. Can I do pagination of the results if I use DAO?

If you take a look at the implementation you see, that no magic happens in applyLimit():


    public function applyLimit($criteria)

    {

        $criteria->limit=$this->pageSize;

        $criteria->offset=$this->currentPage*$this->pageSize;

    }



In order to get the correct page data, you need to add a "LIMIT offset, row_count" clause to your SQL using the same values from $pages as above.

Just wanted to say that I’ve changed/extended my framework to circumvent $pages->applyLimit($criteria), because that statement is feature envy, imo (tightly coupling criteria to pagination). As a result, things are also consistent with DAO.