Skip to content

Commit

Permalink
Added option to ignore attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus Yepes authored and Jesus Yepes committed Sep 30, 2019
1 parent 0a52691 commit 1adc546
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()');
Expand Down
2 changes: 1 addition & 1 deletion src/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down

0 comments on commit 1adc546

Please sign in to comment.