Yii分页的写法

AR写法:

$CDbCriteria = new CDbCriteria();
$CDbCriteria->order = '`id` desc';
$count=MessageModel::model()->count($CDbCriteria);
$pages=new CPagination($count);
$pages->pageSize=8;
$pages->applyLimit($CDbCriteria);
$model = MessageModel::model()->findAll($CDbCriteria);

CDB写法:

$sql='select * from tableName';
$criteria=new CDbCriteria();
$result = Yii::app()->db->createCommand($sql)->query();
$count = $result->rowCount;
$pages=new CPagination($count);
$pages->pageSize=$this->pageSize;
$pages->applyLimit($criteria);
$result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
$result->bindValue(':offset', $pages->currentPage*$pages->pageSize);
$result->bindValue(':limit', $pages->pageSize);
$model=$result->queryAll();
//$pages分页 $model数据

发表评论