Doctrine 2: Update query with query builder

To update an entry in a database using Doctrine's query builder, you can use the update method of the QueryBuilder class. Here's an example of how you can do it:

<?php

$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder
  ->update('MyEntity', 'e')
  ->set('e.field1', ':newValue1')
  ->set('e.field2', ':newValue2')
  ->where('e.id = :id')
  ->setParameter('newValue1', $newValue1)
  ->setParameter('newValue2', $newValue2)
  ->setParameter('id', $id);

$query = $queryBuilder->getQuery();
$query->execute();

This will create and execute an UPDATE query that sets the field1 and field2 fields of the MyEntity entity to the specified new values, for the entity with the specified id.

Watch a course Learn object oriented PHP

You can also use the set method to set multiple fields at once, like this:

<?php

$queryBuilder
  ->update('MyEntity', 'e')
  ->set('e', $newValues)
  ->where('e.id = :id')
  ->setParameter('id', $id);

Here, $newValues is an associative array that maps field names to their new values.