I need to roll back model’s attributes after fail in validate or save.
For example :
$model = new Person(); $model->setAttributes(array( 'name' => 'John' )); $model->save();
Then I do…
$model->setAttributes(array( 'name' => 'Jonathan' )); $model->save(); // This line failed! According to whatever validation rules I have.
But now the [color="#0000FF"]$model->name[/color] is ‘Jonathan’, I need the name ‘John’ back programmatically. (instead of let users fix it)
I know there is transaction roll back, but this is about attributes in model.
I have a solution in my mind for one.
Create another instance to copy $model and after failing in save or validate just copy them back.
But I’d like to know that if anyone have the same issue before? How you do guy solve it? Like what I mentioned above or something else.
Thanks in advance.