Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-3175

Update documentation for QueryBuilder to give example of using "set()" with parameter

    Details

    • Type: Documentation Documentation
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: Git Master
    • Fix Version/s: None
    • Component/s: ORM
    • Labels:
      None
    • Environment:
      PHP 5.3

      Description

      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.

        Activity

        Hide
        Christophe Coevoet added a comment -

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

        Show
        Christophe Coevoet added a comment - you should not set a value directly in the DQL query. You should use the query parameters instead.
        Hide
        Max Summe added a comment - - edited

        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

        Show
        Max Summe added a comment - - edited 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

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Max Summe
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: