duplicate a module with setIsNewRecord(TRUE)

Hi all

I have a model which I want to be duplicated on save()

So, I created the following beforeSave() function

	public function beforeSave() 


		$this->version ++ ;

		$this->setIsNewRecord(TRUE) ;	

	        return true ;


This model has a composite primary key: PRIMARY KEY(id, version)

However, this doesn’t result in an INSERT but in an UPDATE. Any suggestion whats going wrong here ?



Is better to manually copy the record, like that:

$copy= new Record();

$copy->attributes= $this->attributes;


Is also better to do a function for it and not do in the beforesave, because it would be called even in the beforesave of the copy generating a recursive save of copies.

You’re right, but still what I’m trying should work. I looked at the save function inside CActiveRecord.php, and I noticed that there is no call to beforeSave!!

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


                if(!$runValidation || $this->validate($attributes)) 

                        return $this->getIsNewRecord() ? $this->insert($attributes) : $this->update($attributes);


                        return false;


I’ve added print statements in my before function and in this save function and noticed that save() is first executed, and after that my beforeSave.

Any suggestion whats going on here ?

UPDATE: found the solution. Before save is called inside the insert(…) or update(…) so the check if its a new record is already done. So I’m using now afterValidate, and it works!!