1

Bouncing from this old post i am looking for a way to update several Symfony entites in one sql query - for optimisation reasons. I have a Content entity, and a method in a service, that updates all "sibling" contents with setCurrent(0). Here's the working code so far:

/**
 * Set given content the latest regarding its siblings
 */
public function setLatestContent(Entity\Content $content)
{
    // Get siblings entities
    $siblings = $this
        ->em
        ->getRepository('LPFrontRteBundle:Content')
        ->findBySibling($content);

    foreach ($siblings as $sibling_content) {
        $sibling_content->setCurrent(0);
    }

    $this->em->flush();
}

Well that works. But since i have 38 sibling contents, i get 38 SQL queries like:

UPDATE mr_content SET current = 0 WHERE id = 55
UPDATE mr_content SET current = 0 WHERE id = 56
UPDATE mr_content SET current = 0 WHERE id = 57
...

I would like to use Doctrine's "clean" Entity system, in a way to have one query like:

UPDATE mr_content SET current = 0 WHERE id = 55 OR id = 56 OR id = 57 ...

Any thoughts on how to achieve that - or a smarter workaround, would be greatly appreciated.


EDIT

For the record, here's what i came up with - i like it verbose :) With $qb as query builder.

    $qb->update('LPFrontRteBundle:Content', 'c')
        ->set('c.current', 0)

        ->where('c.keyword = :keyword')
        ->setParameter('keyword', $content->getKeyword())

        ->andWhere('c.locale = :locale')
        ->setParameter('locale', $content->getLocale())

        ->andWhere('c.id != :id')
        ->setParameter('id', $content->getId())

        ->getQuery()->execute();
Community
  • 1
  • 1
Loïc Pennamen
  • 197
  • 1
  • 17

2 Answers2

6

Batch processing is what you're looking for.

There are two bulk update methods in Doctrine2.

First one is DQL Update Query, which will best probably best for you. It would be something like:

$q = $em->createQuery("UPDATE LPFrontRteBundle:Content c SET c.current = 0 WHERE id IN (:ids)")
        ->setParameter(':ids', $ids, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY));
$numUpdated = $q->execute();
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
2

You can gather all ids and execute DQL to update all entities at once

$ids = array();
foreach ($siblings as $sibling_content) {
    $ids[] = $sibling_content->getId();
}

$this->em->createQuery("
    UPDATE
        LPFrontRteBundle:Content t
    SET
        t.current = 0
    WHERE
        t.id IN (:ids)
")
->setParameter(':ids', $ids, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY)
->execute();
Max P.
  • 5,579
  • 2
  • 13
  • 32
  • Thanks! What i did was skipping the "entities fetching" part to directly update from query. I read Repos are for "fetching" purpose only, but maybe it's only a philosophical matter? – Loïc Pennamen Oct 04 '16 at 10:16
  • I think that you are right and and some service is more applicable for so operations. In my project I place so operations in repository for simplicity :) I'll delete 2-nd part of my answer not to confuse other users – Max P. Oct 04 '16 at 10:29