koleto
(Koleto Pz)
February 19, 2010, 1:04pm
1
Hi. I want to get last 10 records from database by using CActiveDataProvider. Here is my configuration:
public function actionLast()
{
$dataProvider=new CActiveDataProvider('Models',array(
'criteria'=>array(
'order'=>'id DESC',
'limit'=>10,
),
));
$this->render('last',array(
'dataProvider'=>$dataProvider
));
}
Тhe problem is that CActiveDataProvider returns all the records, not the last 10, but at least they are ordered. Am I missing something with the configuration?
qiang
(Qiang Xue)
February 19, 2010, 1:12pm
2
What’s the generated SQL? Also, if you don’t want to use pagination, you should configure ‘pagination’=>false.
mdomba
(Maurizio Domba Cerin)
February 19, 2010, 1:16pm
3
I don’t know if it’s by design… but “limit” functions only if pagination is set to false!
qiang
(Qiang Xue)
February 19, 2010, 1:23pm
4
That’s because if pagination is not false, the pagination’s limit will be used instead.
mdomba
(Maurizio Domba Cerin)
February 19, 2010, 1:32pm
5
I tryed this one:
'pagination'=>array(
'pageSize'=>self::PAGE_SIZE,
'limit'=>1,
),
but I get this error: Property "CPagination.limit" is read only.
koleto
(Koleto Pz)
February 19, 2010, 1:33pm
6
I set pagination to false and It works fine now. Thank you very much
qiang
(Qiang Xue)
February 19, 2010, 2:15pm
7
@mdombla : for pagination, please set pageSize instead.
mdomba
(Maurizio Domba Cerin)
February 19, 2010, 2:46pm
8
First let me tell you that this framework is great !!!
I would like to retreive first 100 rows but to display them in pages… 10 items per page…
Something like this would be usable:
'pagination'=>array(
'pageSize'=>10,
'maxItem'=>100,
),
qiang
(Qiang Xue)
February 19, 2010, 3:26pm
9
For this special feature, you need to override CActiveDataProvider::calculateTotalItemCount() as:
protected function calculateTotalItemCount()
{
$count=CActiveRecord::model($this->modelClass)->count($this->getCriteria());
return $count>100 ? 100 : $count;
}