要使用 TimestampBehavior,把下面的代码加到你的 ActiveRecord 类中:

use yii\behaviors\TimestampBehavior;

public function behaviors()
{
    return [
        TimestampBehavior::className(),
    ];
}

默认情况下,当关联的 AR 对象执行插入操作时,TimestampBehavior 将会给 created_atupdated_at 两个属性赋值为当前时间戳;而当 AR 对象执行更新操作时, 它只给 updated_at 属性赋值为当前时间戳。时间戳的值来自于 time()

由于属性值是被这个行为自动设置,所以属性值不必用户输入也因此没有必要验证。 因此,created_atupdated_at 这两个属性不应该出现在 rules() 这个模型方法中。

对于应用在 MySQL 数据库的上述实现,请声明 columns(created_at, updated_at) 为整型来存储时间戳。

https://www.yiichina.com/doc/api/2.0/yii-behaviors-timestampbehavior

相关文章:

  • 2022-12-23
  • 2021-11-18
  • 2022-02-17
  • 2021-09-24
  • 2021-11-18
  • 2021-12-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2022-01-16
  • 2022-01-29
  • 2022-12-23
相关资源
相似解决方案