added history of changes

This commit is contained in:
vladrigos
2020-08-04 10:47:53 +03:00
parent 38069c57d4
commit 812bb2b23a
7 changed files with 186 additions and 36 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace Common\Behaviors;
use yii\base\Event;
use yii\db\ActiveRecord;
use yii\base\Behavior;
class LogBehavior extends Behavior
{
public function events()
{
return[
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate',
];
}
public function beforeUpdate(Event $event)
{
$model = $event->sender;
$dirtyAttributes = $model->getDirtyAttributes();
foreach ($dirtyAttributes as $key => $value)
{
if($model->getOldAttribute($key) == $value)
{
continue;
}
$change = new \common\models\ChangeHistory([
'type' => get_class($model),
'type_id' => $model->getAttribute('id'),
'field_name' => $key,
'label' => $model->getAttributeLabel($key),
'old_value' => $model->getOldAttribute($key),
'new_value' => $value,
'created_at' => date('Y-m-d-H:i:s', time()),
]);
$change->save();
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace common\Models;
class ChangeHistory extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'change_history';
}
public function attributeLabels()
{
return [
'table' => 'Таблица',
'row_id' => 'ID строки',
'field_name' => 'Имя поля',
'old_value' => 'Старое значение',
'new_value' => 'Новое значение',
'label' => 'Поле',
];
}
public function translate()
{
}
}