From 729da68f475727ddb7e47a10eed051c8c66c82e7 Mon Sep 17 00:00:00 2001 From: forecho Date: Sun, 28 Apr 2019 11:59:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EventBootstrap.php | 4 +--- src/HLogBehavior.php | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/EventBootstrap.php b/src/EventBootstrap.php index 046bfae..aedafaa 100644 --- a/src/EventBootstrap.php +++ b/src/EventBootstrap.php @@ -17,9 +17,7 @@ class EventBootstrap implements BootstrapInterface public function bootstrap($app) { $activeRecordEvents = [ - ActiveRecord::EVENT_AFTER_DELETE, - ActiveRecord::EVENT_AFTER_INSERT, - ActiveRecord::EVENT_AFTER_UPDATE, + ActiveRecord::EVENT_AFTER_VALIDATE, ]; $controllerEvents = [ diff --git a/src/HLogBehavior.php b/src/HLogBehavior.php index 897cfa8..5dae43d 100644 --- a/src/HLogBehavior.php +++ b/src/HLogBehavior.php @@ -107,22 +107,29 @@ public function getHLogTemplate($uniqueId, $method) */ public function update(HLogTemplate $hLogTemplate) { - $model = new HLog(); $user = \Yii::$app->user->identity; $transaction = \Yii::$app->db->beginTransaction(); try { + $owner = $this->owner; $template = str_replace('{h-log-request-url}', Yii::$app->request->url, $hLogTemplate->template); if ($hLogTemplate->method != HLogTemplate::METHOD_VIEW) { - preg_match_all('/\{(.*?)\}/', $template, $match); - if (isset($match[0]) && is_array($match[0])) { - $owner = $this->owner; - foreach ($match[0] as $key => $value) { - $template = str_replace($value, $owner->{$match[1][$key]}, $template); + if ($owner instanceof ActiveRecord) { + /** @var ActiveRecord $owner */ + $template = str_replace('{old-data}', json_encode($owner->oldAttributes), $template); + preg_match_all('/\{(.*?)\}/', $template, $match); + if (isset($match[0]) && is_array($match[0])) { + foreach ($match[0] as $key => $value) { + $attribute = $match[1][$key]; + if ($owner->hasAttribute($attribute)) { + $template = str_replace($value, $owner->{$attribute}, $template); + } + } } } } + $model = new HLog(); $model->setAttributes([ 'h_log_template_id' => $hLogTemplate->id, 'user_id' => $user->getId(),