From 1adc546fa58786241db130c5ed6dc171f2e00937 Mon Sep 17 00:00:00 2001 From: Jesus Yepes Date: Mon, 30 Sep 2019 12:50:48 +0200 Subject: [PATCH] Added option to ignore attributes. --- readme.md | 11 +++++++++++ src/Log.php | 12 ++++++++++-- src/LogBehavior.php | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 0ef3a07..74114e0 100644 --- a/readme.md +++ b/readme.md @@ -24,6 +24,17 @@ public function behaviors() } ``` +### Ignoring attributes + +In the model, just define an attribute **$logIgnoredAttributes** which is an array of string, representings the properties you wish to ignore. + +``` +public $logIgnoredAttributes = ['attribute1', 'attribute2']; +``` + +If $logIgnoredAttributes is not defined, every attribute will be logged. + + ## TODO Right now, logs are just saved in the database, in a table named goltratec_log diff --git a/src/Log.php b/src/Log.php index 4988056..3411e13 100755 --- a/src/Log.php +++ b/src/Log.php @@ -54,11 +54,19 @@ public function attributeLabels() ]; } - static function l(array $oldAttributes, array $newAttributes, string $event, $object, $uid = false) { + static function l(array $oldAttributes, array $newAttributes, $event, $object, $uid = false) { $model = new self; + $sender = $event->sender; + if (isset($sender->logIgnoredAttributes) && is_array($sender->logIgnoredAttributes) && count($sender->logIgnoredAttributes) > 0) { + foreach ($sender->logIgnoredAttributes as $attr) { + unset($oldAttributes[$attr]); + unset($newAttributes[$attr]); + } + + } $model->old_attributes = Json::encode($oldAttributes); $model->new_attributes = Json::encode($newAttributes); - $model->event = $event; + $model->event = $event->name; $model->object = $object; $model->user = $uid; $model->date = new \yii\db\Expression('NOW()'); diff --git a/src/LogBehavior.php b/src/LogBehavior.php index 6d17507..615f352 100755 --- a/src/LogBehavior.php +++ b/src/LogBehavior.php @@ -24,7 +24,7 @@ public function handleLog(yii\base\ModelEvent $event) { Log::l( $model->oldAttributes, $model->attributes, - $event->name, + $event, $model::className(), Yii::$app->user->id ?? null );