title: Doctrine 2 conditional multiple row update with QueryBuilder
body: <p>The question has some answers on SO, but non of them seems to help accomplish, actually, a simple task.</p>
  
  <p>I need to update multiple rows based on condition in one query, using Doctrine2 QueryBuilder. Most obvious way is supposed to be wrong:</p>
  
  <pre><code>$userAgeList = [
      'user_name_a' =&gt; 30,
      'user_name_b' =&gt; 40,
      'user_name_c' =&gt; 50,
  ];
  
  //Array of found `User` objects from database
  $usersList = $this-&gt;getUsersList();
  
  foreach($usersList as $user)
  {
      $userName = $user-&gt;getName();
  
      $user-&gt;setAge($userAgeList[$userName]);
  
      $this-&gt;_manager-&gt;persist($user);
  }
  
  $this-&gt;_manager-&gt;flush();
  </code></pre>
  
  <p>It will create an update query for each <code>User</code> object in transaction, but I need only one query. <a href="http://labs.octivi.com/mastering-symfony2-performance-doctrine/" rel="nofollow noreferrer">This source</a> suggests that instead you should rely on the <code>UPDATE</code> query, because in case of it we only execute one <code>SQL UPDATE</code> statement, so I've made like this:</p>
  
  <pre><code>$userAgeList = [
      'user_name_a' =&gt; 30,
      'user_name_b' =&gt; 40,
      'user_name_c' =&gt; 50,
  ];
  
  $builder = $this-&gt;_manager-&gt;getRepository(self::REPOSITORY_USER)
      -&gt;createQueryBuilder(self::REPOSITORY_USER);
  
  foreach($userAgeList as $userName =&gt; $age)
  {
      $builder-&gt;update(self::REPOSITORY_USER, 'user')
          -&gt;set('user.age', $builder-&gt;expr()-&gt;literal($age))
          -&gt;where('user.name = :name')
          -&gt;setParameter('name', $userName)
          -&gt;getQuery()-&gt;execute();
  }
  </code></pre>
  
  <p>But that also makes (obviously) a bunch of updates instead of one. If I assign result of <code>getQuery()</code> to a variable, and try to <code>execute()</code> it after foreach loop, I see that $query understands and accumulates a <code>set()</code>, but it does no such thing for <code>WHERE</code> condition.</p>
  
  <p>Is there any way to accomplish such task in QueryBuilder?</p>
  
  <p>UPDATE - similar questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/10405467/multiple-update-queries-in-doctrine-and-symfony2">Multiple update queries in doctrine and symfony2</a> - this one does not assume UPDATE in <em>one</em> query;</p>
  
  <p><a href="https://stackoverflow.com/questions/8764029/symfony-update-multiple-records">Symfony - update multiple records</a> - this also says, qoute, 'one select - one update';</p>
  
  <p><a href="https://stackoverflow.com/questions/4803436/update-multiple-columns-with-doctrine-in-symfony">Update multiple columns with Doctrine in Symfony</a> - <code>WHERE</code> condition in this query is always the same, not my case;</p>
  
  <p><a href="https://stackoverflow.com/questions/4337751/doctrine-2-update-query-with-query-builder">Doctrine 2: Update query with query builder</a> - same thing as previous, only one <code>WHERE</code> clause;</p>
  
  <p><a href="http://doctrine-orm.readthedocs.org/en/latest/reference/batch-processing.html" rel="nofollow noreferrer">http://doctrine-orm.readthedocs.org/en/latest/reference/batch-processing.html</a> - doctrine batch processing does not mention conditions at all...</p>
  
  <p>In MySQL I used to do it using <code>CASE-THEN</code>, but that's not supported by Doctrine.</p>