1

Is it possible to have a Query build like this:

    $qb = $this->em->createQueryBuilder();

    $qb->select(
        'af.shortKey as wg, 
        af.id as afId', 
        'afl.name as afName', 
        'l.id as langId') // -> this one
        ->from('DatabaseBundle:ArticleFamily', 'af')
        ->leftJoin('af.articleFamilyLanguages', 'afl')
        ->leftJoin('afl.language', 'l')
        ->where('langId = :languageId') //-> this is causing the problem
        //if i use it like l.id = :languageId is working. But I don't want it like this.
        ->setParameter('languageId', $params['lang']);

I need to use it like this because I am passing some parameters in the url and I cannot use l.id

If I am using this query I am getting the following error:

An exception occurred while executing 'SELECT a0_.short_key AS short_key0, a0_.id AS id1, a1_.name AS name2, l2_.id AS id3 FROM article_family a0_ LEFT JOIN article_family_language a1_ ON a0_.id = a1_.article_family_id LEFT JOIN language l2_ ON a1_.language_id = l2_.id WHERE id3 = ? ORDER BY name2 ASC LIMIT 10 OFFSET 0' with params ["3"]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id3' in 'where clause'

Where id3 should be actually l2_.id

potashin
  • 44,205
  • 11
  • 83
  • 107
Herr Nentu'
  • 1,438
  • 3
  • 18
  • 49

1 Answers1

2

You cannot using field alias in WHERE clause. Instead, you could try with HAVING.

Related SO question/answer: Can you use an alias in the WHERE clause in mysql?

Community
  • 1
  • 1
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85