Findall() And Updateall() Are Returning Different Sets

Hi all,

I’m trying to updateAll() on a set of models defined by both scopes and CDbCriteria.

When I run a findAll() query on the models, it returns the correct dataset, my initial code is;

[PHP]$games = new Game;

$criteria = new CDbCriteria;

$criteria->addCondition(‘has_images = :has_images’);

$criteria->params[’:has_images’] = ‘0’;

$criteria->scopes=array(‘inCategory’=>array($categoryId,true),‘imageErrors’=>array(false));[/PHP]

After this, if I run;

[PHP]$results = $games->findAll($criteria);[/PHP]

This returns the correct number of results (in this case 149 records from a table of 151 rows). So what I want to do is update all those matched rows (149 of them) and set the ‘has_images’ field to 1.

So I swap out the above line and replace it with;

[PHP]$results = $games->updateAll(array(‘has_images’=>1),$criteria);[/PHP]

Which, after reading the manual and a few other posts here, I believe should update all those rows (the same rows found in the findAll() query), and update the ‘has_images’ field to 1.

However it doesn’t, it updated the entire table, all 151 rows.

Am I missing something on how updateAll() works?

I’ve also tried the following:

[PHP]$games = new Game;

$criteria = new CDbCriteria;

$criteria->addCondition(‘has_images = :has_images’);

$criteria->params[’:has_images’] = ‘0’;

$games->inCategory($categoryId,true)->imageErrors(false)->updateAll(array(‘has_images’=>1),$criteria)[/PHP]

and

[PHP]$games = new Game;

$games->inCategory($categoryId,true)->imageErrors(false)->updateAll(array(‘has_images’=>1),‘has_images = 0’)[/PHP]

Any help would be muchly appreciated!

Not sure if it matters, but does it make any difference if you use


$results = Game::model()->updateAll(array('has_images'=>1),$criteria);

Hi Keith,

Thanks for the input, unfortunately I was using that syntax originally and gives the same results. I changed to the other way for the same reason you mentioned; on the off change it’d make a difference!

According to the reference, "scopes" is not effective on UpdateAll.

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#scopes-detail

nuts, didn’t see that, thanks!

That’s a bummer, is there any way round this as I’d rather not rewrite those scopes lol

Also, wouldn’t be very DRY, if I update the scopes I don’t want to have to have to update all the methods that have updateAll() queries… hmmmm

yeah, this is going to cause me a few problems in my setup :(

Does this have the potential to be a design request for a future release? heh