I am overriding the built-in update() method in Phalcon\Mvc\Model in order to do audit logging. So if I have a MyModel, it extends ModelBase which extends Phalcon\Mvc\Model. In ModelBase:
public function update($data=false, $whiteList=false){
date_default_timezone_set('America/New_York');
$ret=parent::update($data,$whiteList);
//acount for blank passed data array
if(!$data){
$data = $this->toArray();
}
$preupdatedata = $this->getSnapshotData();
//create a new audit transaction row
$tr = new AuditTransaction();
$ts = new \DateTime();
$identity = $this->getDI()->get('auth')->getIdentity();
//assignment of variables
$tr->assign ( array(
'schema_name' => 'dsmc',
'table_name' => $this->table_name,
'table_id' => $this->id,
'timestamp' => $ts->format('Y-m-d H:i:s:u'),
'ip_address' => $this->getIP(),
'user_id' => $identity['id'], //change to real user ID once authentication implemented
'action_type' => 'U' //update
));
$tr->create();
foreach($data as $col_name => $datum){
if($this->hasChanged($col_name)){
$av = new AuditValues();
$av->assign(array (
'transaction_id' => $tr->id,
'column_name' => $col_name,
'new_value' => $datum,
'old_value' => $preupdatedata[$col_name]));
$av->create();
}
}
return $ret;
}
Since I am calling the update() on the second line, I assume that $this->getShapShotData() will contain the previous data values after the update occurs, is that correct? I am setting $this->keepSnapshots(true) in the ModelBase's initialize()...
If MyModel (which extends ModelBase) is using getters and setters, and I do setSomeField() does the value change immediately or only after an explicit call to update()? When I do currently in a controller:
$model = MyModel::findFirstById($someid);
$model->setSomeValue($value);
$model->update();
I get "Data passed to update() must be an array"
Any ideas why?