Class yii\behaviors\TimestampBehavior
TimestampBehavior 用来自动给指定的属性填充当前时间戳。
要使用 TimestampBehavior,把下面的代码加到你的 ActiveRecord 类中:
use yii\behaviors\TimestampBehavior;
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
默认情况下,当关联的 AR 对象执行插入操作时,TimestampBehavior 将会给 created_at 和 updated_at
两个属性赋值为当前时间戳;而当 AR 对象执行更新操作时,
它只给 updated_at 属性赋值为当前时间戳。时间戳的值来自于 time()。
由于属性值是被这个行为自动设置,所以属性值不必用户输入也因此没有必要验证。
因此,created_at 和 updated_at 这两个属性不应该出现在 rules() 这个模型方法中。
对于应用在 MySQL 数据库的上述实现,请声明 columns(created_at, updated_at) 为整型来存储时间戳。
如果你的属性名不一样或者你想用不同的方式计算时间戳, 那么你可以像下面这样配置 $createdAtAttribute,$updatedAtAttribute 和 $value 来达到目的。
use yii\db\Expression;
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
'value' => new Expression('NOW()'),
],
];
}
如果你像上述例子那样使用了 yii\db\Expression 对象,那么当记录保存后 属性的值将不是时间戳而是这个 Expression 对象本身。 如果你随后需要这个值的话,应该先调用这个记录的 refresh() 方法。
TimestampBehavior 也提供了一个 touch() 方法, 它可以给指定的一个或多个属性设置为当前时间戳然后保存入库。比如,
$model->touch('creation_time');
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $attributes | array | 属性列表,属性的值将由 $value 自动填充。
数组的键是 ActiveRecord 的事件,属性就是更新于这些事件之上,
数组的值就是要更新的属性。
你可以用字符串来表示一个单独的属性也可以用一个数组来表示一系列属性。比如,
`php
[
ActiveRecord::EVENT_BEFORE_INSERT => ['attribute1', 'attribute2'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2',
]
` |
yii\behaviors\AttributeBehavior |
| $createdAtAttribute | string | 接收时间戳的属性名。 如果你不想记录生成时间的话把它设置为false。 | yii\behaviors\TimestampBehavior |
| $owner | yii\base\Component|null | The owner of this behavior | yii\base\Behavior |
| $preserveNonEmptyValues | boolean | 是否保留非空的属性值 | yii\behaviors\AttributeBehavior |
| $skipUpdateOnClean | boolean | 当 $owner
没有更新的时候是否跳过这个行为 |
yii\behaviors\AttributeBehavior |
| $updatedAtAttribute | string | 接收时间戳的属性名。 如果你不想记录更新时间的话把它设置为false。 | yii\behaviors\TimestampBehavior |
| $value | mixed | 要分配给当前属性的值。它可以是一个匿名函数,
数组格式的 callable(比如 [$this, 'methodName']),一个 Expression 对象表示的 DB 表达式
(比如 new Expression('NOW()')),标量,字符串或者一个任意的值。
如果是前者,函数的返回值将会设置给这些属性。
函数的签名应该像下面这样,
`php
function ($event)
{
// 返回值将会设置到当前的属性
}
` |
yii\behaviors\TimestampBehavior |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\BaseObject |
| __construct() | Constructor. | yii\base\BaseObject |
| __get() | Returns the value of an object property. | yii\base\BaseObject |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
| __set() | Sets value of an object property. | yii\base\BaseObject |
| __unset() | Sets an object property to null. | yii\base\BaseObject |
| attach() | Attaches the behavior object to the component. | yii\base\Behavior |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
| className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
| detach() | Detaches the behavior object from the component. | yii\base\Behavior |
| evaluateAttributes() | 计算属性的值并分配给当前属性。 | yii\behaviors\AttributeBehavior |
| events() | Declares event handlers for the $owner's events. | yii\behaviors\AttributeBehavior |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
| hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
| init() | Initializes the object. | yii\behaviors\TimestampBehavior |
| touch() | 把指定的属性更新为当前时间戳。 | yii\behaviors\TimestampBehavior |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| getValue() | 返回给当前属性准备的值。 该方法在 evaluateAttributes() 里调用。 它的返回值将会设置到对应触发事件的属性上。 | yii\behaviors\TimestampBehavior |
属性详情
接收时间戳的属性名。 如果你不想记录生成时间的话把它设置为false。
接收时间戳的属性名。 如果你不想记录更新时间的话把它设置为false。
如果这个值是 null,
那么它将用 PHP 函数 time() 作为 $value 的值。
要分配给当前属性的值。它可以是一个匿名函数,
数组格式的 callable(比如 [$this, 'methodName']),一个 Expression 对象表示的 DB 表达式
(比如 new Expression('NOW()')),标量,字符串或者一个任意的值。
如果是前者,函数的返回值将会设置给这些属性。
函数的签名应该像下面这样,
function ($event)
{
// 返回值将会设置到当前的属性
}
方法详情
返回给当前属性准备的值。 该方法在 evaluateAttributes() 里调用。 它的返回值将会设置到对应触发事件的属性上。
如果这个值是 null,
那么它将用 PHP 函数 time() 作为 $value 的值。
| protected mixed getValue($event) | ||
| $event | yii\base\Event | 触发当前属性更新的事件 |
| return | mixed | 属性值 |
|---|---|---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
| public void init() |
把指定的属性更新为当前时间戳。
$model->touch('lastVisit');
| public void touch($attribute) | ||
| $attribute | string | 要更新的属性名。 |
| throws | yii\base\InvalidCallException | 如果行为的拥有者( AR 对象)是一个未更新过的对象 (since version 2.0.6)。 |
|---|---|---|