I have created one model for active record log and
Created behaviour as reference Link for log and changing the table name for each model dynamically in behaviors, see below code :
And this is working in version yii 1.1.14 that i have already implemented. But now it not worked.
public function setTable($log){
$log->table_name = strtolower(get_class($this->Owner))."_logs";
$log->refreshMetadata();
}
public function afterSave($event){
if (!$this->Owner->isNewRecord) {
// new attributes
$newattributes = $this->Owner->getAttributes();
$oldattributes = $this->getOldAttributes();
// compare old and new
foreach ($newattributes as $name => $value) {
if (!empty($oldattributes)) {
$old = $oldattributes[$name];
} else {
$old = '';
}
if ($value != $old) {
//$changes = $name . ' ('.$old.') => ('.$value.'), ';
$log = new ActiveRecordLog;
$log->description = 'User ' . Yii::app()->user->Name
. ' changed ' . $name . ' for '
. get_class($this->Owner)
. ' [' . $this->Owner->getPrimaryKey() .'].';
$log->action = 'CHANGE';
$log->model = get_class($this->Owner);
$log->old_value = $old;
$log->new_value = $value;
$log->model_id = $this->Owner->getPrimaryKey();
$log->field = $name;
$log->created = new CDbExpression('NOW()');
$log->member_id = Yii::app()->user->id;
$this->setTable($log);
$log->save();
}
}
} else {
$log=new ActiveRecordLog;
$log->description = 'User ' . Yii::app()->user->Name
. ' created ' . get_class($this->Owner)
. ' [' . $this->Owner->getPrimaryKey() .'].';
$log->action = 'CREATE';
$log->model = get_class($this->Owner);
$this->setTable($log);
$log->idModel = $this->Owner->getPrimaryKey();
$log->field = '';
$log->creationdate = new CDbExpression('NOW()');
$log->userid = Yii::app()->user->id;
$log->save();
}
}
ActiveRecordLog model
public $table_name;
public function tableName()
{
if(!empty($this->table_name))
return $this->table_name;
else
return 'ActiveRecordLog';
}