Skip to content

Commit

Permalink
fix: 修复 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Apr 28, 2019
1 parent 29f5e57 commit 729da68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/EventBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
19 changes: 13 additions & 6 deletions src/HLogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 729da68

Please sign in to comment.