when a model attributes are edited or updated I do not want that record to updated. Instead a new record should be created and the old record should be disabled. Also I have another log table where the old record is saved.
My code is given below
public function afterSave($insert, $changedAttributes)
{
if ($insert) {
// Да это новая запись (insert)
$model = new Test3log();
$model->desc = $this->desc ;
$model->id_old = $this->id;
$model->isdisabled=1;
$model->save();
} else {
$save = "";
foreach ($changedAttributes as $change => $val) {
if (trim($val) != trim($this->{$change})) {
$save .= $this->attributeLabels()[$change] . '[' . $val . '->' . $this->{$change} . "]\n";
}
}
$xx =$this->getoldattributes();
if ($save != "") {
// Get Old data
// Get New data
// repl new record with old id
// repl old record with new id
$modelnewline = new Test3();
$modelnewline->desc = $xx['desc'];
$modelnewline->id_old = $xx['id'];
$modelnewline->id = NULL;
$modelnewline->isdisabled = 1;
$modelnewline->save();
$newid = $modelnewline->id;
$oldid =$this->id;
$this->isdisabled=1;
$this->id = $newid;
$this->desc = $changedAttributes['desc'];
$this->save(false);
}
}
parent::afterSave($insert, $changedAttributes);
}