user1592714
user1592714

Reputation: 473

Doctrine 2 update of more than one column?

How can I combine these two updates on the same tuple into one operation?

$q = $this->em->createQuery('update \Entity\UserEn u set u.last = :last where u.name = :name');
$q->setParameters( array(
    'last'  => new \DateTime($newLast),
    'name'  => $theUser,

));
$q->getResult();


$q = $this->em->createQuery('update \Entity\UserEn u set u.contribution = :contribution where u.name = :name');
$q->setParameters( array(
    'contribution'  => $this->rContributionUser($theUser),
    'name'          => $theUser,

));
$q->getResult();

I think one update is cheaper than 2 updates.

Upvotes: 2

Views: 401

Answers (1)

Andrew Vaughan
Andrew Vaughan

Reputation: 234

Use a comma to separate the two assignments:

$q = $this->em->createQuery('update \Entity\UserEn u set u.last = :last, u.contribution = :contribution where u.name = :name');
$q->setParameters( array(
    'last'         => new \DateTime($newLast),
    'contribution' => $this->rContributionUser($theUser),
    'name'         => $theUser,
));
$q->getResult();

Upvotes: 2

Related Questions