Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audittrail table column action changed to enum field type #23

Merged
merged 1 commit into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}