Possible Bug with CActiveRecord

Hi,

I think, i have find a bug into CActiveRecord class.

Here is my issue :




$finder = WQuizz::model()->with('ugc');

	

$critera = new CDbCriteria();

$critera->limit = 20;

$critera->offset = 0;

$critera->order = "ugc.count_used DESC";

$critera->condition = "ugc.count_used = 0";


$result = $finder->findAll($critera);


$criteraCount = new CDbCriteria();

$criteraCount->condition = $critera->condition;


$count = $finder->count($criteraCount);



I get this error :

“CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘ugc.count_used’ in ‘where clause’”

-> The SQL request doesn’t include the with parameter ???

But if i do, one unique call :




...


// $result = $finder->findAll($critera);


$criteraCount = new CDbCriteria();

$criteraCount->condition = $critera->condition;


$count = $finder->count($criteraCount);



$finder->count works great, no errors.

Same result with :




...


$result = $finder->findAll($critera);


$criteraCount = new CDbCriteria();

$criteraCount->condition = $critera->condition;


//$count = $finder->count($criteraCount);



$finder->findAll works.

I think, in CActiveRecord some properties is modified by the first call and not corectly initialized for the second call ?

Any idea ?

Thanks.

MLKiiwy

You should not reuse your finder object. See the upgrade notes from 1.1.3:

http://www.yiiframework.com/files/UPGRADE-1.1.3.txt

Ok thanks for the information mike.

But i have another question ?

Is it possible to duplicate a model (finder) ?

My idea is to give the model in argument :


function search($finder)

{

...

$finder->findAll(xxx);

$finder->count(xxx);

}


search(MyModel::model()->with(...));

How can i duplicate the finder ?

Thks

Ok, i think i undestand your "performance" issue for not reuse finder … because you have a singleton model for all finder you need to erase the search scope between every call.

So i made this "hack" for my function :




defaultCriteria = clone $model->getDbCriteria();

$res = $model->findAll($criteria);

		

$count_criteria = new CDbCriteria();

$count_criteria->condition = $criteria->condition;

			

$model->setDbCriteria($defaultCriteria);	// Hack for reuse same Model

$count = $model->count($count_criteria);



Why don’t you call model() a second time, as it is shown in the upgrade notes?

EDIT:

I see, you supply a $finder as method argument. With the above change in mind, maybe that’s not a very whise design decision. If you can, it might be worth to change that piece of your API.