自动填充 created_at、updated_at
①、created_at 和 updated_at 字段属性是 int 时,可使用默认自动填充
use yii\behaviors\TimestampBehavior;
public function behaviors()
{
return [
TimestampBehavior::className(),
"createdAtAttribute" => "created_at", //默认的字段是created_at和updated_at
"updatedAtAttribute" => "updated_at", //如果没有updated_at字段,则写为false
];
}
②、created_at 和 updated_at 字段属性是 timestamp 时,可使用如下方法自动填充
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
public function behaviors()
{
return [
[
"class" => TimestampBehavior::className(),
"createdAtAttribute" => "created_at",
"updatedAtAttribute" => "updated_at",
"value" => new Expression("NOW()"),
],
];
}