Commit 4c20b57b by Alexander Kochetov Committed by Qiang Xue

\yii\behaviors\TimestampBehavior updated

parent 8d9f5b48
...@@ -13,7 +13,7 @@ use yii\db\Expression; ...@@ -13,7 +13,7 @@ use yii\db\Expression;
/** /**
* TimestampBehavior automatically fills the specified attributes with the current timestamp. * 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 * ```php
* use yii\behaviors\TimestampBehavior; * use yii\behaviors\TimestampBehavior;
...@@ -31,7 +31,7 @@ use yii\db\Expression; ...@@ -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()`. * 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, * 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 * ```php
* use yii\db\Expression; * use yii\db\Expression;
...@@ -39,12 +39,10 @@ use yii\db\Expression; ...@@ -39,12 +39,10 @@ use yii\db\Expression;
* public function behaviors() * public function behaviors()
* { * {
* return [ * return [
* 'timestamp' => [ * [
* 'class' => TimestampBehavior::className(), * 'class' => TimestampBehavior::className(),
* 'attributes' => [ * 'createdAtAttribute' => 'create_time',
* ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time', * 'updatedAtAttribute' => 'update_time',
* ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
* ],
* 'value' => new Expression('NOW()'), * 'value' => new Expression('NOW()'),
* ], * ],
* ]; * ];
...@@ -64,17 +62,13 @@ use yii\db\Expression; ...@@ -64,17 +62,13 @@ use yii\db\Expression;
class TimestampBehavior extends AttributeBehavior class TimestampBehavior extends AttributeBehavior
{ {
/** /**
* @var array list of attributes that are to be automatically filled with timestamps. * @var string the attribute that will receive timestamp value
* 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.
*/ */
public $attributes = [ public $createdAtAttribute = 'created_at';
BaseActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], /**
BaseActiveRecord::EVENT_BEFORE_UPDATE => 'updated_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. * @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, * This can be either an anonymous function that returns the timestamp value,
...@@ -86,6 +80,21 @@ class TimestampBehavior extends AttributeBehavior ...@@ -86,6 +80,21 @@ class TimestampBehavior extends AttributeBehavior
/** /**
* @inheritdoc * @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) protected function getValue($event)
{ {
if ($this->value instanceof Expression) { 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