Skip to content

Commit

Permalink
Merge pull request #23 from d3yii2/master
Browse files Browse the repository at this point in the history
Audittrail table column action changed to enum field type
  • Loading branch information
Sammaye authored Oct 4, 2016
2 parents 2560e91 + f17e22c commit e813cc8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
11 changes: 8 additions & 3 deletions LoggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -31,7 +36,7 @@ public function events()

public function afterDelete($event)
{
$this->leaveTrail('DELETE');
$this->leaveTrail(self::ACTION_DELETE);
}

public function afterFind($event)
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions migrations/m161004_132206_change_action_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use yii\db\Schema;
use yii\db\Migration;
use sammaye\audittrail\LoggableBehavior;

class m161004_132206_change_action_field extends Migration
{
public function up()
{

$this->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();
}
}

0 comments on commit e813cc8

Please sign in to comment.