在创建的时候 created_at 与 updated_at都需要更新,更新的时候只更新 updated_at 此时可以定义行为,字段类型设置为int型的时间戳,参考如下:
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
public function behaviors()
{
return [
[
‘class’ => TimestampBehavior::class,
‘createdAtAttribute’ => ‘created_at’,
‘updatedAtAttribute’ => ‘updated_at’,
‘attributes’ => [
ActiveRecord::EVENT_BEFORE_INSERT => [‘created_at’, ‘updated_at’],
ActiveRecord::EVENT_BEFORE_UPDATE => [‘updated_at’],
],
// 设置默认值
‘value’ => time(),
],
];
}