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();