Last active
October 20, 2015 22:48
-
-
Save AFelipeTrujillo/a1d3ef3bf854ba991512 to your computer and use it in GitHub Desktop.
Audit class with Yii2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\lib; | |
use app\lib\Log; | |
/** | |
* @author Andres Felipe Trujillo | |
*/ | |
class AuditActiveRecord extends \yii\db\ActiveRecord | |
{ | |
private $log; | |
const ACTION_CREATE = 'I'; | |
const ACTION_UPDATE = 'U'; | |
const ACTION_DELETE = 'D'; | |
public function __construct() | |
{ | |
$this->log = new Log(); | |
$this->log->creation_date = date('Y-m-d H:i:s'); | |
$this->log->table = $this->tableName(); | |
parent::__construct(); | |
} | |
/** | |
* overide afterSave function | |
* @access public | |
* **/ | |
public function afterSave($insert, $changedAttributes) | |
{ | |
if ($insert === TRUE) { | |
$this->log->type = self::ACTION_CREATE; | |
$this->log->record = json_encode($this->attributes,JSON_UNESCAPED_UNICODE); | |
$this->log->save(); | |
} elseif(sizeof($changedAttributes) !== 0) { | |
$pkColumnName = $this->tableSchema->primaryKey[0]; | |
$changedAttributes = array($pkColumnName => $this->getPrimaryKey(false)) + $changedAttributes; | |
$this->log->type = self::ACTION_UPDATE; | |
$this->log->record = json_encode($changedAttributes,JSON_UNESCAPED_UNICODE); | |
$this->log->save(); | |
} | |
parent::afterSave($insert, $changedAttributes); | |
} | |
/** | |
* overide afterDelete function | |
* @access public | |
* **/ | |
public function afterDelete() | |
{ | |
$this->log->type = self::ACTION_DELETE; | |
$this->log->record = json_encode($this->attributes,JSON_UNESCAPED_UNICODE); | |
$this->log->save(); | |
parent::afterDelete(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\lib; | |
use Yii; | |
/** | |
* This is the model class for table "logs". | |
* | |
* @property integer $id | |
* @property string $type | |
* @property string $creation_date | |
* @property string $table | |
* @property string $record | |
*/ | |
class Log extends \yii\db\ActiveRecord | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public static function tableName() | |
{ | |
return 'logs'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['type', 'creation_date', 'record'], 'required'], | |
[['creation_date'], 'safe'], | |
[['table', 'record'], 'string'], | |
[['type'], 'string', 'max' => 1] | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => Yii::t('app', 'ID'), | |
'type' => Yii::t('app', 'Type'), | |
'creation_date' => Yii::t('app', 'Creation Date'), | |
'table' => Yii::t('app', 'Table'), | |
'record' => Yii::t('app', 'Record') | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE logs | |
( | |
id serial NOT NULL, | |
type character varying(1) NOT NULL, | |
creation_date timestamp without time zone NOT NULL, | |
"table" text, | |
record json NOT NULL, | |
CONSTRAINT log_pk PRIMARY KEY (id) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment