Inserting New Active Record By Cloning

Hi everybody,

I am trying to insert new active records by cloning. Here is the code:

$new_hose = clone $hose;



Weird thing is that this way it creates 2 new records in the db, while if I remove the setIsNewRecord line it updates last record without inserting anything.

Any hint?

P.S. database server is unfortunately MS SQL.

Many thanks

Why are you doing that? :huh:

$newHose=new Hose;


unset($newHose->id); // ... or whatever your pk is


Hi, thanks for your answer. Honestly I thought cloning was a more elegant way, I tried this way and I get validation errors (non sense to me: why don’t I get same errors with cloning?).

thanks a lot again

Because cloning won’t trigger validation. Alos, you’re also cloning the primary key, so the db won’t assign a new one. If you get a lot of validation errors by the method above, perhaps you could create a new scenario that will declare all attributes except for the pk to be safe?

Ok wanted to try this way. So in my controller I did:


while in my model (inside the rules() function) I added:

array('sa_ext_diameter_m, sa_int_diameter_m, sa_tail_m', 'safe', 'on' => 'cloning')

still getting validation error. What am I doing wrong?

Thanks a lot