Doctrine - insert multiple rows with just one save() Doctrine - insert multiple rows with just one save() php php

Doctrine - insert multiple rows with just one save()


Add each record to a Doctrine_Collection the call save() on the collection object.

$collection = new Doctrine_Collection('tablename');$collection->add($record1);$collection->add($record2);$collection->add($record3);$collection->add($record4);$collection->save();

This only works if all the records are for the same table. Otherwise you're out of luck.


Here another solution ,tested on Doctrine 1.2. No need to save each records, the flush() automatically finds out all the unsaved instances and saves them all.

$row = new \My_Doctrine_Record();$row->name = 'aaa';$row->approved = 1;/// ...$row = new \My_Doctrine_Record();$row->name = 'val';$row->approved = 'bbb';Doctrine_Manager::connection()->flush();


If you use symfony2 it is so easy

// get the manager$em = $this->getDoctrine()->getManager();// enter the records$em->persist($entitiy1);$em->persist($entitiy2);$em->persist($entitiy3);$em->persist($entitiy4);$em->persist($entitiy5);// save the entries$em->flush();