Insert multiple objects with Doctrine Dbal

0

I'm using silex in conjunction with doctrine dbal 2.5.

How do I insert multiple objects into the database with Doctrine dbal?

Reading the documentation I did not find anything that would allow this, is there any way to do this?

    
asked by anonymous 23.07.2015 / 20:54

1 answer

1

Typically, with Doctrine, you must persist multiple objects with persist before using the command flush :

$em = $this->getEntityManager();
foreach ($objects as $object) {
    $em->persist($object);
}
$em->flush();

If the question is about having multiple INSERT in the same query, Doctrine does not allow this. The reason is that with this Doctrine can not get the identifier of each line and put in the corresponding object.

According to their own documentation:

  

First of all, this syntax is only supported on mysql and newer postgresql versions. Secondly, there is no easy way to get hold of all generated identifiers in such a multi-insert when using AUTO_INCREMENT or SERIAL and an ORM needs the identifiers for identity management of the objects. Lastly, insert performance is rarely the bottleneck of an ORM. Normal inserts are more than fast enough for most situations and if you really want bulk fast inserts, then multi-insert is not the best way anyway, i.e. Postgres COPY or Mysql LOAD DATA INFILE are several orders of magnitude faster.   These are the reasons why it is not worth the effort to implement an abstraction that performs multi-inserts on mysql and postgresql in an ORM.

More details here: Doctrine2 Batch Processing .

    
24.07.2015 / 22:12