Skip to content

Loading…

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

Open
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

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.

@doctrinebot

Comment created by stof:

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

@doctrinebot

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

@beberlei beberlei was assigned by doctrinebot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.