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!