using aliases from select clause in having clause does not work #6077

Closed
opened 2026-01-22 15:26:23 +01:00 by admin · 14 comments
Owner

Originally created by @ghost on GitHub (Sep 26, 2018).

Originally assigned to: @Ocramius on GitHub.

Bug Report

Using aliases from select clause in having clause throw an exception.

Q A
BC Break no
Version 2.6.2

Summary

I am trying call a function created function in Entity Repository

public function getBillGroupByDate($dateStart, $dateEnd) {
    return $query = $this->createQueryBuilder('d')
        ->select('count(d) as nb, SUBSTRING(d.created, 1, 10) as bill_date')
        ->having('DATE(bill_date) BETWEEN :from AND :to')
        ->groupBy('bill_date')
        ->orderBy('d.created', 'ASC')
        ->setParameter('from', $dateStart)
        ->setParameter('to', $dateEnd)
        ->getQuery()
        ->getResult();
}

I'm using a berberlei bundle who's have create most needed DQL function to doctrine. The function date seems well

<?php

namespace DoctrineExtensions\Query\Mysql;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;

/**
 * @author Steve Lacey <steve@stevelacey.net>
 */
class Date extends FunctionNode
{
    public $date;

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'DATE(' . $sqlWalker->walkArithmeticPrimary($this->date) . ')';
    }

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->date = $parser->ArithmeticPrimary();

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }
}

Throw an exception, because the column name is an alias and not really exist.

HTTP 500 Internal Server Error Notice: Undefined index: metadata Exception Symfony\Component\Debug\Exception\ ContextErrorException in vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php (line 604) * * @return string */ public function walkEntityIdentificationVariable($identVariable) { $class = $this->queryComponents[$identVariable]['metadata']; $tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable); $sqlParts = array(); foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) { $sqlParts[] = $tableAlias . '.' . $columnName;

How to reproduce

https://github.com/helmidridi/doctrine_undefined_index_metadata

Originally created by @ghost on GitHub (Sep 26, 2018). Originally assigned to: @Ocramius on GitHub. ### Bug Report Using aliases from select clause in having clause throw an exception. | Q | A |------------ | ------ | BC Break | no | Version | 2.6.2 #### Summary I am trying call a function created function in Entity Repository ```php public function getBillGroupByDate($dateStart, $dateEnd) { return $query = $this->createQueryBuilder('d') ->select('count(d) as nb, SUBSTRING(d.created, 1, 10) as bill_date') ->having('DATE(bill_date) BETWEEN :from AND :to') ->groupBy('bill_date') ->orderBy('d.created', 'ASC') ->setParameter('from', $dateStart) ->setParameter('to', $dateEnd) ->getQuery() ->getResult(); } ``` I'm using a berberlei bundle who's have create most needed DQL function to doctrine. The function date seems well ```php <?php namespace DoctrineExtensions\Query\Mysql; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Lexer; /** * @author Steve Lacey <steve@stevelacey.net> */ class Date extends FunctionNode { public $date; public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) { return 'DATE(' . $sqlWalker->walkArithmeticPrimary($this->date) . ')'; } public function parse(\Doctrine\ORM\Query\Parser $parser) { $parser->match(Lexer::T_IDENTIFIER); $parser->match(Lexer::T_OPEN_PARENTHESIS); $this->date = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_CLOSE_PARENTHESIS); } } ``` Throw an exception, because the column name is an alias and not really exist. > ``` > HTTP 500 Internal Server Error Notice: Undefined index: metadata Exception Symfony\Component\Debug\Exception\ ContextErrorException in vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php (line 604) * * @return string */ public function walkEntityIdentificationVariable($identVariable) { $class = $this->queryComponents[$identVariable]['metadata']; $tableAlias = $this->getSQLTableAlias($class->getTableName(), $identVariable); $sqlParts = array(); foreach ($this->quoteStrategy->getIdentifierColumnNames($class, $this->platform) as $columnName) { $sqlParts[] = $tableAlias . '.' . $columnName; > ``` #### How to reproduce https://github.com/helmidridi/doctrine_undefined_index_metadata
admin added the BugInvalidMissing Tests labels 2026-01-22 15:26:23 +01:00
admin closed this issue 2026-01-22 15:26:23 +01:00
Author
Owner

@Ocramius commented on GitHub (Sep 26, 2018):

What version does this affect?

@Ocramius commented on GitHub (Sep 26, 2018): What version does this affect?
Author
Owner

@ghost commented on GitHub (Sep 26, 2018):

composer.zip
"doctrine/orm": "^2.5",
"doctrine/doctrine-bundle": "^1.6",

@ghost commented on GitHub (Sep 26, 2018): [composer.zip](https://github.com/doctrine/doctrine2/files/2420784/composer.zip) "doctrine/orm": "^2.5", "doctrine/doctrine-bundle": "^1.6",
Author
Owner

@Ocramius commented on GitHub (Sep 26, 2018):

Can you please try ^2.6.2?

@Ocramius commented on GitHub (Sep 26, 2018): Can you please try `^2.6.2`?
Author
Owner

@ghost commented on GitHub (Sep 27, 2018):

Symfony 3.4 use doctrine ^2.5, I will open the new issue in Symfony framework

@ghost commented on GitHub (Sep 27, 2018): Symfony 3.4 use doctrine ^2.5, I will open the new issue in Symfony framework
Author
Owner

@Ocramius commented on GitHub (Sep 27, 2018):

No, the issue is here - Symfony simply supports installing older versions of doctrine/orm, but you should really upgrade to latest available version before reporting a bug.

@Ocramius commented on GitHub (Sep 27, 2018): No, the issue is here - Symfony simply *supports installing* older versions of `doctrine/orm`, but you should really upgrade to latest available version before reporting a bug.
Author
Owner

@ghost commented on GitHub (Sep 27, 2018):

Yes i confirm still exists in 2.6.2
i updated the projet "How to reproduce" in github

@ghost commented on GitHub (Sep 27, 2018): Yes i confirm still exists in 2.6.2 i updated the projet "How to reproduce" in github
Author
Owner

@Ocramius commented on GitHub (Sep 27, 2018):

Can you port your test into a test case like in b40c2c6b78/tests/Doctrine/Tests/ORM/Functional/Ticket? See b40c2c6b78/CONTRIBUTING.md (tests)

@Ocramius commented on GitHub (Sep 27, 2018): Can you port your test into a test case like in https://github.com/doctrine/doctrine2/tree/b40c2c6b7893c929f754150d480c20f9b22e0afe/tests/Doctrine/Tests/ORM/Functional/Ticket? See https://github.com/doctrine/doctrine2/blob/b40c2c6b7893c929f754150d480c20f9b22e0afe/CONTRIBUTING.md#tests
Author
Owner

@ghost commented on GitHub (Sep 28, 2018):

DDC7414Test.php
You need to install beberlei/DoctrineExtensions or just add the function Date to doctrine project.

@ghost commented on GitHub (Sep 28, 2018): [DDC7414Test.php](https://github.com/helmidridi/doctrine2/blob/junk/using_aliases_having_clause/7414/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7414Test.php) You need to install [beberlei/DoctrineExtensions](https://github.com/beberlei/DoctrineExtensions) or just add the function Date to doctrine project.
Author
Owner

@ghost commented on GitHub (Sep 28, 2018):

I included the function directly in the test

@ghost commented on GitHub (Sep 28, 2018): I included the function directly in the test
Author
Owner

@Ocramius commented on GitHub (Sep 28, 2018):

Is it not possible to reproduce the problem without DoctrineExtensions?

@Ocramius commented on GitHub (Sep 28, 2018): Is it not possible to reproduce the problem without DoctrineExtensions?
Author
Owner

@Ocramius commented on GitHub (Sep 28, 2018):

For instance, since an ArithmeticPrimary is requested in that location, would HAVING bill_date > 'foo' suffice?

@Ocramius commented on GitHub (Sep 28, 2018): For instance, since an `ArithmeticPrimary` is requested in that location, would `HAVING bill_date > 'foo'` suffice?
Author
Owner

@ghost commented on GitHub (Sep 28, 2018):

You don't need doctrine extension I included a DQL "DATE" function directly in the test File DDC7414Test.php. I have no bug without using a DQL function.
HAVING bill_date > 'foo' return just a QueryException

@ghost commented on GitHub (Sep 28, 2018): You don't need doctrine extension I included a DQL "DATE" function directly in the test File [DDC7414Test.php](https://github.com/helmidridi/doctrine2/blob/junk/using_aliases_having_clause/7414/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC7414Test.php). I have no bug without using a DQL function. `HAVING bill_date > 'foo'` return just a QueryException
Author
Owner

@ghost commented on GitHub (Sep 30, 2018):

It's not a bug, sorry. After having read the source code it has become clear that this is a problem due to that the date function of beberlei extension expects to have a column and parse an "ArithmeticPrimary" but alias is a String type. I'm going to create my own DATE function and parse a "StringExpression" it solves the problem.

@ghost commented on GitHub (Sep 30, 2018): It's not a bug, sorry. After having read the source code it has become clear that this is a problem due to that the date function of beberlei extension expects to have a column and parse an "ArithmeticPrimary" but alias is a String type. I'm going to create my own DATE function and parse a "StringExpression" it solves the problem.
Author
Owner

@Ocramius commented on GitHub (Sep 30, 2018):

Closing as per https://github.com/doctrine/doctrine2/issues/7414#issuecomment-425750000

@Ocramius commented on GitHub (Sep 30, 2018): Closing as per https://github.com/doctrine/doctrine2/issues/7414#issuecomment-425750000
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6077