3

I work on a project with symfony2 and doctrine-mongodb-odm. I want to execute an atomic update on several documents with the querybuilder but I am missing something:

$this->createQueryBuilder('MyBundle:MyDoc')
->update()
->field('isOpen')->set(false)
->field('isOpen')->equals(true)
->getQuery()
->execute();

It works but it only updates one document. I guess I should add an option like

array('multi' => true)

somewhere but I didn't find anything about that in the docs.

Can somebody help me please?

JuCachalot
  • 1,010
  • 4
  • 15
  • 27

2 Answers2

4

I found the answer by looking in the class definition. There is a method of the query builder named multiple to set this option.

$this->createQueryBuilder('MyBundle:MyDoc')
->update()
->multiple(true)
->field('isOpen')->set(false)
->field('isOpen')->equals(true)
->getQuery()
->execute();
JuCachalot
  • 1,010
  • 4
  • 15
  • 27
2

By now using multiple() is deprecated. You can simply use updateMany() instead.

/**
 * Set the "multiple" option for an update query.
 *
 * @param boolean $bool
 * @return $this
 *
 * @deprecated Deprecated in version 1.4 - use updateOne or updateMany instead
 */
public function multiple($bool = true)
{
    $this->query['multiple'] = (boolean) $bool;
    return $this;
}

/**
 * Change the query type to update multiple documents
 *
 * @return $this
 */
public function updateMany()
{
    $this->query['type'] = Query::TYPE_UPDATE;
    $this->query['multiple'] = true;
    return $this;
}
webDEVILopers
  • 1,886
  • 1
  • 21
  • 35