Yii Framework Forum: Problem with CActiveDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with CActiveDataProvider can't assign limit in criteria Rate Topic: -----

#1 User is offline   koleto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 07-July 09
  • Location:Bulgaria

Posted 19 February 2010 - 08:04 AM

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?
I want to fix the world but they won't give me the source code
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 19 February 2010 - 08:12 AM

What's the generated SQL? Also, if you don't want to use pagination, you should configure 'pagination'=>false.
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 19 February 2010 - 08:16 AM

I don't know if it's by design... but "limit" functions only if pagination is set to false!
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 19 February 2010 - 08:23 AM

That's because if pagination is not false, the pagination's limit will be used instead.
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 19 February 2010 - 08:32 AM

I tryed this one:
                'pagination'=>array(
                    'pageSize'=>self::PAGE_SIZE,
                    'limit'=>1,
                ),


but I get this error: Property "CPagination.limit" is read only.
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   koleto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 07-July 09
  • Location:Bulgaria

Posted 19 February 2010 - 08:33 AM

I set pagination to false and It works fine now. Thank you very much :)
I want to fix the world but they won't give me the source code
0

#7 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 19 February 2010 - 09:15 AM

@mdombla: for pagination, please set pageSize instead.
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 19 February 2010 - 09:46 AM

View Postqiang, on 19 February 2010 - 09:15 AM, said:

@mdombla: for pagination, please set pageSize instead.


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,
                ),



Find more about me.... btw. Do you know your WAN IP?
0

#9 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 19 February 2010 - 10:26 AM

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;
    }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users