TimestampBehavior is not working in Yii2

2020-04-08 01:11发布

问题:

public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],

        'access' => [
            'class' => AccessControl::className(),
            'only' => ['create', 'update', 'delete', 'view', 'index'],
            'rules' => [
                // allow authenticated users
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // everything else is denied by default
            ],
        ],

        [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
            ],
        ],
    ];
}

Above code is for my controller behavior function. While creating or updating, 'create_time' and 'update_time' fields are not getting updated by current time. Type of those fields are set to datetime. Please need your help.

回答1:

You have to declare it in the behaviors method of your model.

To use TimestampBehavior, insert the following code to your ActiveRecord class

public function behaviors()
{
  return [
    'class' => TimestampBehavior::className(),
        'attributes' => [
           ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
           ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
    ],
  ];
}


回答2:

Try this, it works for me:

use yii\db\Expression;
...

        [
            'class' => TimestampBehavior::className(),
            'updatedAtAttribute' => 'update_time',
            'value' => new Expression('NOW()'),
        ],


回答3:

You can also use

 'value' => date('Y-m-d H:i:s')

if you would like to use the PHP datetime.



回答4:

I used new Expression('NOW()') to store current timestamp. But It does't store the date based on current timezone. Instead it stores based on server time.

I just used normal php date function to solve this.

eg :

use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

public function behaviors()
{
    return [
        'timestamp' => [
            'class' => 'yii\behaviors\TimestampBehavior',
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
            ],
            'value' => date('Y-m-d H:i:s'),
        ],
    ];
}


回答5:

You must also add the value and use the class Expression

use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

public function behaviors()
{
    return [
        'timestamp' => [
            'class' => 'yii\behaviors\TimestampBehavior',
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
            ],
            'value' => new Expression('NOW()'),
        ],
    ];
}


标签: yii2