ActiveRecord does not save

The question was asked: 6 years 9 months ago   views: 5
0

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

$file=Files::model()->findByPk($id); 
if($file == null) {
 throw new CHttpException(404,'Not found'); 
}
$count = $file->count; 
$count++; 
$file->count = $count; 
$file->save(); 
$this->redirect(Yii::app()->request->hostInfo."/".$file->path);
Asked: angry angry 21-06-2012 в 21:32:49

Answers   6

0

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
0

make it so

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

The method will show whether there are errors in validation.

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

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
0

Disable validation when saving.

$file->save(false);
Answered: Barton Barton 22-06-2012 в 10:26:21
0

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:

$file=Files::model()->findByPk($id);
if($file === null) throw new CHttpException(404,'Not found');
$file->saveCounters(array('count'=>1));

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

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


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

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