Skip to content

Commit

Permalink
Fixed: #23
Browse files Browse the repository at this point in the history
  • Loading branch information
sammi authored and sammi committed Oct 4, 2015
1 parent d769881 commit 4620ab6
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions behaviors/LoggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class LoggableBehavior extends CActiveRecordBehavior{
public $storeTimestamp = false;
public $skipNulls = true;

/**
* The component name to use for the db connection for the logging, allows one to use
* qc server or whatever for logging
* @var string
*/
public $db = null;

public function afterSave($event){
$allowedFields = $this->allowed;
$ignoredFields = $this->ignored;
Expand Down Expand Up @@ -60,10 +67,10 @@ public function afterSave($event){

// Now lets actually write the attributes
$this->auditAttributes($newattributes, $oldattributes);

// Reset old attributes to handle the case with the same model instance updated multiple times
$this->setOldAttributes($this->getOwner()->getAttributes());

return parent::afterSave($event);
}

Expand Down Expand Up @@ -103,7 +110,16 @@ public function setOldAttributes($value){
}

public function leaveTrail($action, $name = null, $value = null, $old_value = null){
$log = new AuditTrail();
$log = new AuditTrail();

if($this->db){
if(Yii::$app->getComponent($this->db) instanceof CDbConnection){
$log->db = Yii::app()->getComponent($this->db);
}else{
throw new CDbException(Yii::t('yii','Loggable behaviour requires that "db" be a valid CDbConnection application component.'));
}
}

$log->old_value = $old_value;
$log->new_value = $value;
$log->action = $action;
Expand All @@ -112,6 +128,9 @@ public function leaveTrail($action, $name = null, $value = null, $old_value = nu
$log->field = $name;
$log->stamp = $this->storeTimestamp ? time() : date($this->dateFormat); // If we are storing a timestamp lets get one else lets get the date
$log->user_id = $this->getUserId(); // Lets get the user id

$log->db = null;

return $log->save();
}

Expand Down

0 comments on commit 4620ab6

Please sign in to comment.