Skip to content

Commit

Permalink
Add logged model PK. Closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yepes committed Sep 17, 2021
1 parent 89b29ee commit a0647b9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
25 changes: 25 additions & 0 deletions migrations/m210917_105805_add_object_id_column_to_log_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use yii\db\Migration;

/**
* Handles adding columns to table `{{%log}}`.
*/
class m210917_105805_add_object_id_column_to_log_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('goltratec_log', 'object_id', $this->integer());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('goltratec_log', 'object_id');
}
}
9 changes: 6 additions & 3 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property integer $user
* @property string $event
* @property string $object
* @property integer $object_id
*/
class Log extends ActiveRecord
{
Expand All @@ -33,7 +34,7 @@ public function rules()
{
return [
[['old_attributes', 'new_attributes'], 'string'],
[['user'], 'integer'],
[['user', 'object_id'], 'integer'],
[['date'], 'safe'],
[['event', 'object'], 'string', 'max' => 30],
];
Expand All @@ -54,22 +55,24 @@ public function attributeLabels()
];
}

static function l(array $oldAttributes, array $newAttributes, $event, $object, $uid = false) {
static function l(array $oldAttributes, array $newAttributes, $event, $object, $uid = false, $object_id = null) {
$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->name;
$model->object = $object;
$model->user = $uid;
$model->date = new \yii\db\Expression('NOW()');
$model->object_id = $object_id;

$model->save();
}
}
18 changes: 17 additions & 1 deletion src/LogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,23 @@ public function handleLog($event) {
$model->attributes,
$event,
$model::className(),
Yii::$app->user->id ?? null
Yii::$app->user->id ?? null,
$this->getPK($model)
);
}

/**
* Returns the PK of a given model given that it's formed by only one field.
* Rows with multiple fields as PKs are not currently supported
* @param $model
* @return false|mixed
*/
private function getPK($model) {
$pks = $model::getTableSchema()->primaryKey;

if (count($pks) === 1)
return $model->{$pks[0]};

return null;
}
}

1 comment on commit a0647b9

@vercel
Copy link

@vercel vercel bot commented on a0647b9 Sep 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.