Trying to treat an AR instance always as new using an extension

Hi All

I have a situation in which I always want an ActiveRecord instance to be treated as new. For example:




$obj = new User ;

$ob->name = 'Foo' ;

$obj->save() ;

$obj->name = 'Bar' ; 

$obj->save() ;



Together with a behavior which does the following:




public function afterValidate() {

  $obj->setIsNewRecord(TRUE);

}



the above actions will create 2 db records!

However, if I save without validation ( save(false) ) it doesn’t work anymore, because afterValidate is not called!

Any suggestions how this can be done within an extension/behavior using AR methods ?

cheers

NOTE that you cannot replace afterValidate with beforeSave and that you cannot use afterSave to set isNewRecord to TRUE!!

don’t know if it would be the same problem, but yesterday i was facing a “insert the record twice” problem like this and it was just that i removed the ajaxValidation from create action and let the enableAjaxValidation = true in the form…

hope this helps

:)

regards

Thanks for your reply, but it sounds like a different problem, I guess you got your data inserted twice, using new instances, like




$obj = new User ;

..... 

$obj->save() ;

...

$obj = new User ;

....

$obj->save()



You can overwrite CActiveRecord::save() method itself in the model, if your business logic really needs it.




class User extends CActiveRecord

{

  public function save($runValidation = true, $attributes = null)

  {

    	$this->setIsNewRecord(true);

    	return parent::save($runValidation, $attributes);

  }

}




EDIT: As topic title says you want to do it in a behavior, so the above is not a solution.