views:

61

answers:

3

i have problems inserting multiple records in symfony what happens is the 2nd up to the last records just updates the first record

ex:

$n = new Object();

// start loop until i = 5
$n->count = $i;
$n->text = 'a'.$i;
$n->save();
i++;
//end loop

after this loop theres only 1 record... count = 5, text= a5;

is there a way to clear $n so that when i insert a new record again it will not update the first?

+3  A: 

I think your pseudocode needs to change a bit - include the new definition inside the loop:

// start loop until i = 5
$n = new Object();
$n->count = $i;
$n->text = 'a'.$i;
$n->save();
i++;
//end loop

It makes more sense this way too - you want to make 5 new rows, so to do that you have to create a new object 5 times.

benlumley
A: 

if you want to mantain the new operation outside the loop, unset ($n->id) will do the trick.

gpilotino
A: 

You're modifying the same object which symfony interprets as modifying the same database row. Move the object creation code inside the loop.

You might think that you're optimizing for performance by creating the object only once, but by calling save() on an object that already has an 'id' attribute, symfony is going to perform a database update instead of an insert. Inserts are generally constant time operations while the performance of your updates would depend on different factors including how your indices are set up.

P.S I am NO db expert so maybe I'm completely off :-)

sjobe