ActiveRecord does not save

The question was asked: 6 years 11 months ago   views: 18

Here's a piece of code, no warning is issued. The model has the count field. Is not saved. I do not understand why.

if($file == null) {
 throw new CHttpException(404,'Not found'); 
$count = $file->count; 
$file->count = $count; 
Asked: angry angry 21-06-2012 в 21:32:49

Answers   6


save saves the new loaded from the database model. Check whether this field is a validation rule (rules method) and check all the methods models beforeValidate, beforeSave they must return true and better parent::beforeSave()

Answered: xomaa xomaa 22-06-2012 в 09:49:43

make it so


The method will show whether there are errors in validation.

Answered: chandler chandler 22-06-2012 в 13:44:41

you are a model loaded from the database for a particular id) the save method saves a new field (obtained from the database, creating using the new operator) model in the database) then read - the paragraph the record is updated

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

Answered: Ale_x Ale_x 22-06-2012 в 05:11:24

Disable validation when saving.

Answered: Barton Barton 22-06-2012 в 10:26:21

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

if($file === null) throw new CHttpException(404,'Not found');

Pruflink: Work with counters in the database through AR

Answered: VasyOk VasyOk 27-07-2012 в 15:57:19


Well then get the error which occurs when you save and then we'll look further.

Answered: artem artem 27-07-2012 в 16:30:15