Doctrine2 - How can I order by a discriminator column?

后端 未结 3 1642
攒了一身酷
攒了一身酷 2021-02-13 04:36

How should I go about ordering by a discriminator column in a doctrine repository query?

I have a pretty straight forward setup, I have different types of payment detai

3条回答
  •  难免孤独
    2021-02-13 05:04

    Even though TYPE is not implemented yet into the core of doctrine it's still possible to implement it as a custom user function.

    Someone already did an amazing job and implemented it for us. Just in case the resource will be removed in the future, here is a slightly adjusted version for php7+:

    getQueryComponent($this->dqlAlias)['metadata'];
            $tableAlias = $sqlWalker->getSQLTableAlias($class->getTableName(), $this->dqlAlias);
    
            if (!isset($class->discriminatorColumn['name'])) {
                $message = 'TYPE() only supports entities with a discriminator column.';
                throw QueryException::semanticalError($message);
            }
    
            return $tableAlias . '.' . $class->discriminatorColumn['name'];
        }
    
        public function parse(Parser $parser)
        {
            $parser->match(Lexer::T_IDENTIFIER);
            $parser->match(Lexer::T_OPEN_PARENTHESIS);
            $this->dqlAlias = $parser->IdentificationVariable();
            $parser->match(Lexer::T_CLOSE_PARENTHESIS);
        }
    }
    

    Now you can order by the discriminator column by doing something like:

    SELECT e, TYPE(e) AS HIDDEN my_type FROM Entity e ORDER BY my_type DESC;
    

提交回复
热议问题