Cant Save With Findall() Function

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.

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');

                }

            }



SOLVED, change from save() to update(). My mistake :lol: .

Anyway, thank for your help friend, Tsunami. :)

what if you just Use

foreach($model as $myobject){

// Code hre

}

instead of using counters and check!