Yii Framework Forum: Cactivedataprovider And Cactiverecordbehavior - Yii Framework Forum

Jump to content

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

Cactivedataprovider And Cactiverecordbehavior Rate Topic: -----

#1 User is offline   KJL Jon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 18-September 12

Posted 18 September 2012 - 11:26 AM

I have a behavior attached to a model and it makes the CActiveDataProvide not count properly (it says in the pagination that it has all the records in the database)

From looking around the source code, I think it probably deals with calculateTotalItemCount() but a little stumped on creating a patch (and not too sure if it's a bug or intended this way).

If it is intended this way I am probably going to just create a custom class that extends CActiveDataProvide to provide a fix for my models but wondering what I should change to make it work properly.

Here is a simplified version of the Behavior:
class MyBehavior extends CActiveRecordBehavior{
	function afterConstruct( $event ){
		$condition = $this->owner->getDbCriteria();
		$condition->compare( $this->owner->getTableAlias() .'.active', 1 );
	}
}


Table
id active
1 1
2 0
3 1
4 0

So in the Controller:

class TableController extends CController{
	actionIndex(){
		$dataProvider = CActiveDataProvider( 'Table' );
		$this->render( 'index', array( 'dataProvider' => $dataProvider ) );
	}
}


Expected results: "Displaying 1-2 of 2 result(s)."
Results I am getting: "Displaying 1-2 of 4 result(s)."

Yii Version: 1.1.12
OS: Windows 7 32bit
DB: MySQL 5.5.20
PHP: 5.3.9
Webserver: Apache 2.2.21
0

#2 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 18 September 2012 - 03:47 PM

The problem occurs because the first query fetching the data has the condition applied, but second has not as scopes and criteria will be reset after a query.
This is expected behavior so you have to adjust your behavior to work as you want it.
Changing criteria in afterContruct is not a good idea as it only works for the first query executed on that object.

Currently have no idea how you could do that transparently without touching models code.
criteria modification in beforeFind() would be one way, but this would still not apply to the count query.
There is a feature request on beforeCount() event on github https://github.com/y...yii/issues/1353.
0

#3 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 535
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 18 September 2012 - 03:51 PM

Something similar is done here: https://github.com/y...havior.php#L113
0

#4 User is offline   KJL Jon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 18-September 12

  Posted 19 September 2012 - 08:16 AM

Yea I had it in the beforeFind before, but I figured I would try it in the afterConstruct when I was testing it to see if I could get something working.

I think beforeFind would be good and I am glad to see some work on getting it implemented.

Thanks for your help :)
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