2013-01-17 65 views
2

我使用symfony2和doctrine-mongodb-odm開展項目。 我想在與QueryBuilder的幾個文件執行原子更新,但我失去了一些東西:原理mongodb中的多個文檔的原子更新

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

它的工作原理,但它僅更新一個文檔。我想我應該增加一個選項一樣

array('multi' => true) 

的地方,但我沒有找到有關的文檔任何東西。

有人可以幫助我嗎?

回答

2

我通過查找類定義找到答案。有一個名爲multiple的查詢生成器的方法來設置此選項。

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

現在使用多個()已過時。您可以簡單地使用updateMany()來代替。

/** 
* 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; 
}