I am doing this to append the auto-increment primary key, field id to another field in my model
//in the model class
class SomeModel extends CActiveRecord{
...
protected function afterSave(){
parent::afterSave();
$this->updateByPk($this->id, array('append_field'=>$this->append_field.$this->id));
}
}
My controller’s action actionCreate looks something like this:
public function actionCreate(){
$model= new SomeModel;
if(isset($_POST['SomeModel'])){
$model->attributes=$_POST['SomeModel'];
if($model->save()){
someFunction($model->append_field);
// so the append_field doesn't have the appended id, although in the db it is correctly appended because of the afterSave() function
// but the following works
$model=$this->loadModel($model->id);
someFunction($model->append_field);
}
}
$this->render('create',array('model'=>$model));
}
So basically what i want to know is that if it is possible for the old model object to have the updated model object without loading the model again. Maybe there is something that i can do/change in the save process, so that the model has the newly appended field as expected.
I know that i can do the following:
someFunction($model->append_field.$model->id);
but i am just curious if there is anything else that yii’s activerecord saving process has, that i can exploit.
Also i would be glad if anyone can point out if i’m overriding the afterSave() method correctly, i mean am i calling the parent afterSave() correctly, in the correct order etc?