Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupBY with identification alias is missing discriminator field #11459

Open
kshtompel opened this issue May 16, 2024 · 1 comment
Open

groupBY with identification alias is missing discriminator field #11459

kshtompel opened this issue May 16, 2024 · 1 comment

Comments

@kshtompel
Copy link

kshtompel commented May 16, 2024

Bug Report

While using groupBy(DQL_ALIAS) for aggregated query resulted SQL with missing discriminator field and this is leading to

[42803] ERROR: column "p0_.type" must appear in the GROUP BY clause or be used in an aggregate function

Q A
BC Break no
Version 2.x.x, 3.x.x, 4.x.x

Summary

Suggestion to fix

class SqlWalker implements TreeWalker
{
    ...

    public function walkGroupByItem($groupByItem)
    {

    ...
      
       // IdentificationVariable
        $sqlParts = [];

        foreach ($this->getMetadataForDqlAlias($groupByItem)->fieldNames as $field) {
            $item       = new AST\PathExpression(AST\PathExpression::TYPE_STATE_FIELD, $groupByItem, $field);
            $item->type = AST\PathExpression::TYPE_STATE_FIELD;

            $sqlParts[] = $this->walkPathExpression($item);
        }

        foreach ($this->getMetadataForDqlAlias($groupByItem)->associationMappings as $mapping) {
            if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadata::TO_ONE) {
                $item       = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $groupByItem, $mapping['fieldName']);
                $item->type = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION;

                $sqlParts[] = $this->walkPathExpression($item);
            }
        }

        ### NEW CODE HERE ###
        if ($this->getMetadataForDqlAlias($groupByItem)->isInheritanceTypeSingleTable()) {
            $field = $this->getMetadataForDqlAlias($groupByItem)->getDiscriminatorColumn()['name'];
            $sqlParts[] = $this->walkIdentificationVariable($groupByItem, $field) . '.' . $field;
        }
        // or add it as part of $this->walkPathExpression($item) with new AST\PathExpression::TYPE_DISCRIMINATOR
        ### END OF NEW CODE ###

        return implode(', ', $sqlParts);

Current behavior

No discriminator field added to GROUP BY clause

How to reproduce

Create aggregated query with group by entity identification alias and entity has discriminator field.

As a result there is no discriminator field in the resulted SQL GROUP BY clause

Expected behavior

All the columns should appear in the SQL GROUP BY clause for aggregated queries

@kshtompel
Copy link
Author

kshtompel commented May 16, 2024

Currently can be tackled with inheritance from the base SqlWalker and overriding
public function walkGroupByItem($groupByItem): string method

but all the checks from parent should be covered for instance: if $groupByItem is not path_expression, not returning variable, etc..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant