Here is a piece of code, no warnings are issued. In the model there is a field count. Not saved. I do not understand why.

$file=Files::model()->findByPk($id); if($file == null) { throw new CHttpException(404,'Не найдено'); } $count = $file->count; $count++; $file->count = $count; $file->save(); $this->redirect(Yii::app()->request->hostInfo."/".$file->path); 

    6 answers 6

    save saves both new models loaded from the database. Check whether a validation rule is written for this field (the rules method) and also check all methods of the model of the type beforeValidate, beforeSave - they should return true and better parent :: beforeSave ()

      do so

       if(!$model->save()) print_r($model->getErrors()); 

      The method will show if there are errors during validation.

        you have loaded the model from the base on a specific id) save method - saves the new (fields are not received from the base, created using the new operator) model to the base) read here - paragraph Update the record

        there are two solutions - first use the update () method or before the line file-> save (); reset the CActiveRecord :: isNewRecord field to false, and then when the save method is called, the update will actually be called

          Disable validation when saving.

           $file->save(false); 

            If I'm not mistaken, the count field is your counter. Keep in mind that the correct implementation of the counter will be like this:

             $file=Files::model()->findByPk($id); if($file === null) throw new CHttpException(404,'Не найдено'); $file->saveCounters(array('count'=>1)); 

            Pruflink: http://rmcreative.ru/blog/post/yii-1.1.8 Working with counters in the database via AR

               if($file->save()) good else error 

              Well, output the error that occurs when save and then we will look further.