Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-3364

QueryBuilder fails when using alias in having with like expr

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Security Level: All
    • Environment:
      Debian, MySQL, PHP, Zend Framework 2, Doctrine Module, Doctrine Extensions

      Description

      In my select I create an alias. I use a like expr on this alias in the having clause.

      Trying several variations I still get the following error:
      Expected '.' or '(', got 'long_name'

      My example including variations inside comments:

      $having = $qb->expr()->like('long_name', $qb->expr()->literal('%' . $term . '%'));
      
      $qb->select(array(
          'b.id', 'b.name AS branch_name',
          'c.name AS company_name',
          'GroupConcat(b.name, \', \', c.name) AS long_name' // same for 'c.name AS long_name'
      ))
      ->from('Application\Entity\Branch', 'b')
      ->join('b.company', 'c')
      ->where('b.compartment = 1')
      ->having('(' . $having . ')') // same for having($having)
      ->groupBy('c.id')
      ->orderBy('c.name')
      ->addOrderBy('b.name');
      

      I use a Doctrine Extension for the MySQL GROUP_CONCAT functionality:
      https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/GroupConcat.php

      This should have no effect on the result since I tried a simple alias - see comment - instead of it.

      The problem seems to be using an alias inside the having clause when adding the like expr.
      The following clause would work:

      $having = $qb->expr()->like('c.name', $qb->expr()->literal('%' . $term . '%'));
      

      I also tried this workaround using the GROUP_CONCAT method inside the where part:

      ->andWhere($qb->expr()->like('GroupConcat(b.name, \', \', c.name)', $qb->expr()->literal('%' . $term . '%')))
      

      Allthough I used the groupBy part I got this error:
      General error: 1111 Invalid use of group function

        Activity

        Hide
        Marco Pivetta added a comment -

        What is the actual failure/exception type? What about the generated SQL?

        Show
        Marco Pivetta added a comment - What is the actual failure/exception type? What about the generated SQL?
        Hide
        Steve Müller added a comment -

        Moved to ORM, the error and use case is ORM related.

        Show
        Steve Müller added a comment - Moved to ORM, the error and use case is ORM related.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            webDEVILopers
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: