yii2: TimestampBehavior not working

The project and this model is already six months old and all worked fine before, but suddenly the TimestampBehavior broke down. The database writes all the time zeros (in those fields where there should be time). I did not change anything related to this. It feels like a crash somewhere inside the framework…

Important! I noticed in the debugger that after saving the model the fields it is not empty !!! But the database still writes zeros. It’s very strange…

Here is my model:

/**
 * @property integer $id
 * @property integer $category_id
 * @property integer $user_id
 * @property integer $status_id
 * @property string $name
 * @property string $text
 * @property integer $created_at
 * @property integer $updated_at
 */
class Article extends ActiveRecord {
    public static function tableName() {
        return '{{%article}}';
    }

    public function behaviors() {
        return [
            'timestamp' => TimestampBehavior::class,
        ];
    }

    public function rules() {
        return [
            ['id', 'safe'],
            [['name', 'category_id'], 'required'],
            ['user_id', 'default', 'value' => Yii::$app->user->id],
        ];
    }
}

Yii 2.0.14

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 27 (12 by maintainers)

Most upvoted comments

Thank you for your question. In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

This is an automated comment, triggered by adding the label question.

The save() method is intended to save the model. In my opinion the method params are OK.

Simply there the first parameter goes the flag runValidation and by itself the conclusion suggests that the second parameter specifies attributes for validation… This is much more logical. I would do so in your place. And what for to specify attributes which it is necessary to save I generally do not know… After all, they can simply not be setted.

Or, remove the flag runValidation, because there is still a method validate in which already an array of attributes is also passed indicating which ones should be validated.

FUUUUUUUUUUCK!!! I think I understood what’s wrong … The problem was again in your unobvious infrastructure! I called the save(true, ['name', 'category_id', 'user_id', 'status_id']) method. But I somehow thought that this array specifies which attributes should be validated and which ones do not! And now I read that it indicates which attributes should be saved …