Commit 4c20b57b by Alexander Kochetov Committed by Qiang Xue

\yii\behaviors\TimestampBehavior updated

parent 8d9f5b48
......@@ -13,7 +13,7 @@ use yii\db\Expression;
/**
* TimestampBehavior automatically fills the specified attributes with the current timestamp.
*
* To use TimestampBehavior, simply insert the following code to your ActiveRecord class:
* To use TimestampBehavior, insert the following code to your ActiveRecord class:
*
* ```php
* use yii\behaviors\TimestampBehavior;
......@@ -31,7 +31,7 @@ use yii\db\Expression;
* 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 [[attributes]] and [[value]] properties like the following:
* you may configure the [[createdAtAttribute]], [[updatedAtAttribute]] and [[value]] properties like the following:
*
* ```php
* use yii\db\Expression;
......@@ -39,12 +39,10 @@ use yii\db\Expression;
* public function behaviors()
* {
* return [
* 'timestamp' => [
* [
* 'class' => TimestampBehavior::className(),
* 'attributes' => [
* ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
* ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
* ],
* 'createdAtAttribute' => 'create_time',
* 'updatedAtAttribute' => 'update_time',
* 'value' => new Expression('NOW()'),
* ],
* ];
......@@ -64,17 +62,13 @@ use yii\db\Expression;
class TimestampBehavior extends AttributeBehavior
{
/**
* @var array list of attributes that are to be automatically filled with timestamps.
* The array keys are the ActiveRecord events upon which the attributes are to be filled with timestamps,
* and the array values are the corresponding attribute(s) to be updated. You can use a string to represent
* a single attribute, or an array to represent a list of attributes.
* The default setting is to update both of the `created_at` and `updated_at` attributes upon AR insertion,
* and update the `updated_at` attribute upon AR updating.
* @var string the attribute that will receive timestamp value
*/
public $attributes = [
BaseActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
];
public $createdAtAttribute = 'created_at';
/**
* @var string the attribute that will receive timestamp value
*/
public $updatedAtAttribute = 'updated_at';
/**
* @var callable|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
......@@ -86,6 +80,21 @@ class TimestampBehavior extends AttributeBehavior
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (empty($this->attributes)) {
$this->attributes = [
BaseActiveRecord::EVENT_BEFORE_INSERT => [$this->createdAtAttribute, $this->updatedAtAttribute],
BaseActiveRecord::EVENT_BEFORE_UPDATE => $this->updatedAtAttribute,
];
}
}
/**
* @inheritdoc
*/
protected function getValue($event)
{
if ($this->value instanceof Expression) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment