Class yii\behaviors\TimestampBehavior

Inheritanceyii\behaviors\TimestampBehavior » yii\behaviors\AttributeBehavior » yii\base\Behavior » yii\base\Object
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/behaviors/TimestampBehavior.php

TimestampBehavior automatically fills the specified attributes with the current timestamp.

To use TimestampBehavior, insert the following code to your ActiveRecord class:

use yii\behaviors\TimestampBehavior;

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

By default, TimestampBehavior will fill the created_at and updated_at attributes with the current timestamp when the associated AR object is being inserted; it will fill the updated_at attribute with the timestamp when the AR object is being updated. The timestamp value is obtained by time().

If your attribute names are different or you want to use a different way of calculating the timestamp, you may configure the $createdAtAttribute, $updatedAtAttribute and $value properties like the following:

use yii\db\Expression;

public function 
behaviors()
{
    return [
        [
            
'class' => TimestampBehavior::className(),
            
'createdAtAttribute' => 'create_time',
            
'updatedAtAttribute' => 'update_time',
            
'value' => new Expression('NOW()'),
        ],
    ];
}

In case you use an yii\db\Expression object as in the example above, the attribute will not hold the timestamp value, but the Expression object itself after the record has been saved. If you need the value from DB afterwards you should call the refresh() method of the record.

TimestampBehavior also provides a method named touch() that allows you to assign the current timestamp to the specified attribute(s) and save them to the database. For example,

$model->touch('creation_time');

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attributes array List of attributes that are to be automatically filled with the value specified via $value. yii\behaviors\AttributeBehavior
$createdAtAttribute string The attribute that will receive timestamp value Set this property to false if you do not want to record the creation time. yii\behaviors\TimestampBehavior
$owner yii\base\Component The owner of this behavior yii\base\Behavior
$updatedAtAttribute string The attribute that will receive timestamp value. yii\behaviors\TimestampBehavior
$value callable|yii\db\Expression The expression that will be used for generating the timestamp. yii\behaviors\TimestampBehavior

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Object
__construct() Constructor. yii\base\Object
__get() Returns the value of an object property. yii\base\Object
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Object
__set() Sets value of an object property. yii\base\Object
__unset() Sets an object property to null. yii\base\Object
attach() Attaches the behavior object to the component. yii\base\Behavior
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Object
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Object
className() Returns the fully qualified name of this class. yii\base\Object
detach() Detaches the behavior object from the component. yii\base\Behavior
evaluateAttributes() Evaluates the attribute value and assigns it to the current attributes. 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\Object
hasProperty() Returns a value indicating whether a property is defined. yii\base\Object
init() Initializes the object. yii\behaviors\TimestampBehavior
touch() Updates a timestamp attribute to the current timestamp. yii\behaviors\TimestampBehavior

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getValue() Returns the value of the current attributes. yii\behaviors\TimestampBehavior

Property Details

$createdAtAttribute public property
string $createdAtAttribute 'created_at'

The attribute that will receive timestamp value Set this property to false if you do not want to record the creation time.

$updatedAtAttribute public property
string $updatedAtAttribute 'updated_at'

The attribute that will receive timestamp value. Set this property to false if you do not want to record the update time.

$value public property

The expression that will be used for generating the timestamp. This can be either an anonymous function that returns the timestamp value, or an yii\db\Expression object representing a DB expression (e.g. new Expression('NOW()')). If not set, it will use the value of time() to set the attributes.

Method Details

getValue() protected method

Returns the value of the current attributes.

This method is called by evaluateAttributes(). Its return value will be assigned to the attributes corresponding to the triggering event.

mixed getValue$event )
$event yii\base\Event

The event that triggers the current attribute updating.

return mixed

The attribute value

init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

void init( )
touch() public method

Updates a timestamp attribute to the current timestamp.

$model->touch('lastVisit');
void touch$attribute )
$attribute string

The name of the attribute to update.

throws yii\base\InvalidCallException

if owner is a new record.