Insert multiple objects with Dbal Doctrine

Asked

Viewed 89 times

0

I am using Silex in conjunction with Doctrine dbal 2.5.

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

Reading the documentation I didn’t find anything that made it possible, there’s some way to do this?

1 answer

1

Normally, with Doctrine, multiple objects with persist before using the command flush:

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

If the question concerns 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 the 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 to do fast Bulk Inserts, then a 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.

  • Dbal does not use Entity manager.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.