clonevn
(Clonevn)
May 19, 2013, 1:51pm
1
Here is my code:
public static function changeStatus()
{
$Criteria = new CDbCriteria();
$Criteria->condition = 'status = 3 AND date < CURDATE()';
$model = Stuff::model()->findAll($Criteria);
$count = count($model);
if($count)
{
for($i=0; $i<$count; $i++)
{
$model[$i]->status = 2;
if(!$model[$i]->save())
throw new CHttpException('Cant change the status');
}
}
}
It always throws the exeption . If I remove one of the conditions, it could be saved, otherwise system doesn’t save any object.
Please help.
Tsunami
(Erik)
May 19, 2013, 2:33pm
2
If ->save() returns false, the first thing you should do is check if there were any validation errors. If you don’t want to run validation you can use ->save(false). Also your code can be shortened a bit:
$models = Stuff::model()->findAll($Criteria);
foreach($models as $model)
{
$model->status = 2;
if(!$model->save())
{
echo '<pre>';
CVarDumper::dump($model->errors);
echo '</pre>';
throw new CHttpException('Cant change the status');
}
}
clonevn
(Clonevn)
May 19, 2013, 2:55pm
3
SOLVED, change from save() to update(). My mistake .
Anyway, thank for your help friend, Tsunami.
clonevn:
Here is my code:
public static function changeStatus()
{
$Criteria = new CDbCriteria();
$Criteria->condition = 'status = 3 AND date < CURDATE()';
$model = Stuff::model()->findAll($Criteria);
$count = count($model);
if($count)
{
for($i=0; $i<$count; $i++)
{
$model[$i]->status = 2;
if(!$model[$i]->save())
throw new CHttpException('Cant change the status');
}
}
}
It always throws the exeption . If I remove one of the conditions, it could be saved, otherwise system doesn’t save any object.
Please help.
what if you just Use
foreach($model as $myobject){
// Code hre
}
instead of using counters and check!