DDC-3175: Update documentation for QueryBuilder to give example of using "set()" with parameter #3935

Open
opened 2026-01-22 14:31:46 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 17, 2014).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user max.summe:

QueryBuilder->set method uses Expr\Comparison object, which tries to cast values as strings.

When trying to set('updated', new \DateTime) in an update statement, this causes an exception as \DateTime has no **toString method.

Not sure what the fix is - the format for the \DateTime there depends on the platform and ( I think) the type of field.

Originally created by @doctrinebot on GitHub (Jun 17, 2014). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user max.summe: QueryBuilder->set method uses Expr\Comparison object, which tries to cast values as strings. When trying to set('updated', new \DateTime) in an update statement, this causes an exception as \DateTime has no **toString method. Not sure what the fix is - the format for the \DateTime there depends on the platform and ( I think) the type of field.
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2014):

Comment created by stof:

you should not set a value directly in the DQL query. You should use the query parameters instead.

@doctrinebot commented on GitHub (Jun 17, 2014): Comment created by stof: you should not set a value directly in the DQL query. You should use the query parameters instead.
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2014):

Comment created by max.summe:

So you're saying it should not be used like this.

$qb = $em->createQueryBuilder();
$qb->update('User', 'u')
->set("u.field", "new value")
->where("u.field = :oldvalue")
->setParameter("oldvalue", "old value");

Instead, it should be:

$qb = $em->createQueryBuilder();
$qb->update('User', 'u')
->set("u.field", ":value")
->where("u.field = :oldvalue")
->setParameter("oldvalue", "old value")
->setParameter("value", "new value");

Is that correct?

If yes, it would be helpful to add an example to the documentation in this page: http://docs.doctrine-project.org/en/latest/reference/query-builder.html

@doctrinebot commented on GitHub (Jun 17, 2014): Comment created by max.summe: So you're saying it should not be used like this. $qb = $em->createQueryBuilder(); $qb->update('User', 'u') ->set("u.field", "new value") ->where("u.field = :oldvalue") ->setParameter("oldvalue", "old value"); Instead, it should be: $qb = $em->createQueryBuilder(); $qb->update('User', 'u') ->set("u.field", ":value") ->where("u.field = :oldvalue") ->setParameter("oldvalue", "old value") ->setParameter("value", "new value"); Is that correct? If yes, it would be helpful to add an example to the documentation in this page: http://docs.doctrine-project.org/en/latest/reference/query-builder.html
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3935