From f17e22c9c9c8c57c3df6eb157e103de47f92cf43 Mon Sep 17 00:00:00 2001 From: Uldis Date: Tue, 4 Oct 2016 15:46:04 +0300 Subject: [PATCH] Audittrail table column action changed to enum field type --- LoggableBehavior.php | 11 +++++-- .../m161004_132206_change_action_field.php | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 migrations/m161004_132206_change_action_field.php diff --git a/LoggableBehavior.php b/LoggableBehavior.php index 2733b6c..13f5094 100644 --- a/LoggableBehavior.php +++ b/LoggableBehavior.php @@ -9,6 +9,11 @@ class LoggableBehavior extends Behavior { + const ACTION_DELETE = 'DELETE'; + const ACTION_CREATE = 'CREATE'; + const ACTION_SET = 'SET'; + const ACTION_CHANGE = 'CHANGE'; + private $_oldattributes = array(); public $allowed = array(); public $ignored = array(); @@ -31,7 +36,7 @@ public function events() public function afterDelete($event) { - $this->leaveTrail('DELETE'); + $this->leaveTrail(self::ACTION_DELETE); } public function afterFind($event) @@ -98,7 +103,7 @@ public function audit($insert) // If this is a new record lets add a CREATE notification if ($insert) { - $this->leaveTrail('CREATE'); + $this->leaveTrail(self::ACTION_CREATE); } // Now lets actually write the attributes @@ -120,7 +125,7 @@ public function auditAttributes($insert, $newattributes, $oldattributes = array( // If they are not the same lets write an audit log if ($value != $old) { - $this->leaveTrail($insert ? 'SET' : 'CHANGE', $name, $value, $old); + $this->leaveTrail($insert ? self::ACTION_SET : self::ACTION_CHANGE, $name, $value, $old); } } } diff --git a/migrations/m161004_132206_change_action_field.php b/migrations/m161004_132206_change_action_field.php new file mode 100644 index 0000000..eabd58a --- /dev/null +++ b/migrations/m161004_132206_change_action_field.php @@ -0,0 +1,31 @@ +alterColumn('tbl_audit_trail', 'action', "ENUM('".LoggableBehavior::ACTION_CHANGE."', '".LoggableBehavior::ACTION_CREATE."', '".LoggableBehavior::ACTION_DELETE."', '".LoggableBehavior::ACTION_SET."') NOT NULL"); + + } + + public function down() + { + $this->alterColumn('tbl_audit_trail', 'action', Schema::TYPE_STRING . ' NOT NULL'); + } + + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + $this->up(); + } + + public function safeDown() + { + $this->down(); + } +}