Skip to content

Commit

Permalink
add migrations for new ngrest logger #1525
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Sep 30, 2017
1 parent e1fc42c commit a6da421
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
36 changes: 33 additions & 3 deletions modules/admin/src/behaviors/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Yii;
use yii\db\ActiveRecord;
use yii\web\Application;
use yii\base\Behavior;
use yii\helpers\Json;

/**
* LogBehavior stores informations when active records are updated or inserted.
*
* @author Basil Suter <basil@nadar.io>
*/
class LogBehavior extends \yii\base\Behavior
class LogBehavior extends Behavior
{
public $route = '';

Expand All @@ -31,7 +33,24 @@ public function init()
throw new \Exception('LogBehavior route or api property must be set.');
}
}

/**
*
* @param unknown $array
* @return string
*/
private function toJson($array)
{
$array = (array) $array;

return Json::encode($array);
}

/**
* After Insert.
*
* @param \yii\db\AfterSaveEvent $event
*/
public function eventAfterInsert($event)
{
if (Yii::$app instanceof Application) {
Expand All @@ -42,11 +61,19 @@ public function eventAfterInsert($event)
'api' => $this->api,
'is_insert' => true,
'is_update' => false,
'attributes_json' => json_encode($event->sender->getAttributes()),
'attributes_json' => $this->toJson($event->sender->getAttributes()),
'attributes_diff_json' => null,
'table_name' => $event->sender->tableName(),
'pk_value' => implode("-", $event->sender->getPrimaryKey(true)),
])->execute();
}
}

/**
* After Update.
*
* @param \yii\db\AfterSaveEvent $event
*/
public function eventAfterUpdate($event)
{
if (Yii::$app instanceof Application) {
Expand All @@ -57,7 +84,10 @@ public function eventAfterUpdate($event)
'api' => $this->api,
'is_insert' => false,
'is_update' => true,
'attributes_json' => json_encode($event->sender->getAttributes()),
'attributes_json' => $this->toJson($event->sender->getAttributes()),
'attributes_diff_json' => $this->toJson($event->changedAttributes),
'table_name' => $event->sender->tableName(),
'pk_value' => implode("-", $event->sender->getPrimaryKey(true)),
])->execute();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use yii\db\Migration;

class m170926_164913_add_ngrest_log_diff_data extends Migration
{
public function safeUp()
{
$this->addColumn('admin_ngrest_log', 'attributes_diff_json', $this->text()->null());
$this->addColumn('admin_ngrest_log', 'pk_value', $this->string());
$this->addColumn('admin_ngrest_log', 'table_name', $this->string());
}

public function safeDown()
{
$this->dropColumn('admin_ngrest_log', 'attributes_diff_json', $this->text());
$this->dropColumn('admin_ngrest_log', 'pk_value', $this->string());
$this->dropColumn('admin_ngrest_log', 'table_name', $this->string());

}
}

0 comments on commit a6da421

Please sign in to comment.