Hello, how are?
The company where I work a Behavior log that is using getAttributes () method within a afterFind () was created. The problem is that this getAttributes, according to the documentation directly retrieves data from the database, which show many indices in the logs and I wanted to treat this data before writing this log table.
How can I do this?
I did not find anything related to it by searching the internet.
<?php
class LoggableBehavior extends CActiveRecordBehavior
{
private $_oldattributes = array();
public function afterSave($event)
{
$newattributes = $this->Owner->getAttributes();
$oldattributes = $this->getOldAttributes();
if (!$this->Owner->isNewRecord) {
// compare old and new
foreach ($newattributes as $name => $value) {
if (!empty($oldattributes)) {
$old = $oldattributes[$name];
} else {
$old = '';
}
if ($value != $old) {
$log=new LogTable();
$log->old_value = $old;
$log->new_value = $value;
$log->action = 'CHANGE';
$log->model = get_class($this->Owner);
$log->model_id = $this->Owner->getPrimaryKey();
// Conditions for primary key advanced for more one columns.
if(is_array($log->model_id))
$log->model_id = implode('#', $log->model_id);
$log->field = $name;
$log->stamp = date('Y-m-d H:i:s');
$log->user_id = $userid;
$log->save();
}
}
// ...
public function afterFind($event)
{
// Save old values
$this->setOldAttributes($this->Owner->getAttributes());
return parent::afterFind($event);
}
public function getOldAttributes()
{
return $this->_oldattributes;
}
public function setOldAttributes($value)
{
$this->_oldattributes=$value;
}
}
I hope they help me in this.
Thank you.
[Solved]
In model add code:
public function getFormatAttributes() {
$model = new DMVidaConexao;
$model->attributes = $this->getAttributes();
$model->principal = ($model->principal == 'S') ? 'Sim' : 'Não';
$model->ativo = ($model->ativo == '1') ? 'Sim' : 'Não';
$model->data_alteracao = DConverter::convertMB($model->data_alteracao, 'date');
$model->id_vida_conexao = $this->id_vida_conexao;
return $model->attributes;
}
In behavior overwrite code:
getAttributes()
for
getFormatAttributes();
Thanks guys!