Commit 4ec95e6d by Qiang Xue

Fixes issue #600.

parent 1e93fcf2
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\behaviors; namespace yii\behaviors;
use yii\base\Behavior; use yii\base\Behavior;
use yii\base\Event;
use yii\db\Expression; use yii\db\Expression;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
...@@ -64,30 +65,27 @@ class AutoTimestamp extends Behavior ...@@ -64,30 +65,27 @@ class AutoTimestamp extends Behavior
*/ */
public function events() public function events()
{ {
$events = array(); $events = $this->attributes;
$behavior = $this; foreach ($events as $i => $event) {
foreach ($this->attributes as $event => $attributes) { $events[$i] = 'updateTimestamp';
if (!is_array($attributes)) {
$attributes = array($attributes);
}
$events[$event] = function () use ($behavior, $attributes) {
$behavior->updateTimestamp($attributes);
};
} }
return $events; return $events;
} }
/** /**
* Updates the attributes with the current timestamp. * Updates the attributes with the current timestamp.
* @param array $attributes list of attributes to be updated. * @param Event $event
*/ */
public function updateTimestamp($attributes) public function updateTimestamp($event)
{ {
$attributes = isset($this->attributes[$event->name]) ? (array)$this->attributes[$event->name] : array();
if (!empty($attributes)) {
$timestamp = $this->evaluateTimestamp(); $timestamp = $this->evaluateTimestamp();
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$this->owner->$attribute = $timestamp; $this->owner->$attribute = $timestamp;
} }
} }
}
/** /**
* Gets the current timestamp. * Gets the current timestamp.
......
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