TimestampBehavior is not working in Yii2

2020-04-08 01:02发布

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.

标签: yii2
5条回答
倾城 Initia
2楼-- · 2020-04-08 01:43

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'],
    ],
  ];
}
查看更多
beautiful°
3楼-- · 2020-04-08 01:46

Try this, it works for me:

use yii\db\Expression;
...

        [
            'class' => TimestampBehavior::className(),
            'updatedAtAttribute' => 'update_time',
            'value' => new Expression('NOW()'),
        ],
查看更多
该账号已被封号
4楼-- · 2020-04-08 01:46

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()'),
        ],
    ];
}
查看更多
一纸荒年 Trace。
5楼-- · 2020-04-08 01:55

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'),
        ],
    ];
}
查看更多
一夜七次
6楼-- · 2020-04-08 02:05

You can also use

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

if you would like to use the PHP datetime.

查看更多
登录 后发表回答