Doctrine期望Lexer :: T_FROM,得到'>'

时间:2017-11-15 19:59:40

标签: php symfony doctrine-orm

(Symfony 3.2.7)

我在存储库中创建了此查询:

<Kronos_WFC version='1.0'>
    <Request Action='Load'>
        <FailedXMLTransaction SourceName='WIM' TransactionType='Pay Code Edits'/>
    <Request>
</Kronos_WFC>

正在生成此DQL:

$qb = $this->createQueryBuilder('c');
$qb->select($qb->expr()->gte('c.createdDate', 'CURRENT_TIMESTAMP()').' AS HIDDEN x');
$qb->orderBy('x');
$qb->getQuery()->execute();

而且......它正在产生这个错误:

  

[语法错误]第0行,第21行:错误:预期的Doctrine \ ORM \ Query \ Lexer :: T_FROM,得到&#39;&gt;&#39;

这里可能有什么问题?

感谢@bishop的回答,我设法通过创建自定义Doctrine DQL函数来创建解决方案。配置:

SELECT c.createdDate >= CURRENT_TIMESTAMP() AS HIDDEN x 
FROM App\CatalogBundle\Entity\Company c 
ORDER BY x ASC

这里提到的课程包含:

doctrine:
    orm:
        dql:
            datetime_functions:
                DATES_COMPARE: AppBundle\DQL\DatesCompareFunction

所有这些都适用于以下DQL:

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

class DatesCompareFunction extends FunctionNode
{
    /* @var Query\AST\Node  */
    public $param1;

    /* @var Query\AST\Node  */
    public $param2;

    /* @var Query\AST\Node  */
    public $param3;

    /** @inheritdoc */
    public function getSql(Query\SqlWalker $sqlWalker)
    {
        return sprintf(
            'CASE (%s %s %s) WHEN 1 THEN 1 ELSE 0 END',
            $this->param1->dispatch($sqlWalker),
            $this->param2,
            $this->param3->dispatch($sqlWalker)
        );
    }

    /** @inheritdoc */
    public function parse(Query\Parser $parser): void
    {
        $parser->match(Query\Lexer::T_IDENTIFIER);
        $parser->match(Query\Lexer::T_OPEN_PARENTHESIS);

        $this->param1 = $parser->StringPrimary();

        $parser->match(Query\Lexer::T_COMMA);

        $this->param2 = $parser->ComparisonOperator();

        $parser->match(Query\Lexer::T_COMMA);

        $this->param3 = $parser->StringPrimary();

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

1 个答案:

答案 0 :(得分:3)

您的语句是有效的SQL,但请记住:SQL!= DQL。检查docs是否有DQL,我们看到SELECT有此BNF:

  

SelectExpression :: =(IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression |“(”Subselect“)”| CaseExpression | NewObjectExpression)[[“A​​S”] [“HIDDEN”] AliasResultVariable]

>=运算符仅在ConditionalExpression中出现

  

ConditionalExpression :: = ConditionalTerm {“OR”ConditionalTerm} *   ConditionalTerm :: = ConditionalFactor {“AND”ConditionalFactor} *   ConditionalFactor :: = [“NOT”] ConditionalPrimary   ConditionalPrimary :: = SimpleConditionalExpression | “(”ConditionalExpression“)”   SimpleConditionalExpression :: = ComparisonExpression | BetweenExpression | LikeExpression |                                   InExpression | NullComparisonExpression | ExistsExpression |                                   EmptyCollectionComparisonExpression | CollectionMemberExpression |                                   InstanceOfExpression

     

ComparisonExpression :: = ArithmeticExpression ComparisonOperator(QuantifiedExpression | ArithmeticExpression)

     

ComparisonOperator :: =“=”| “&LT;” | “&lt; =”| “&LT;&gt;” 中| “&gt;” 中| “&gt; =”| “!=”

由于SelectExpression不包含ConditionalExpression,因此您会收到错误的错误。

要完成您想要的任务,您需要一个自定义功能。有关如何执行此操作,请参阅"Adding your own functions to the DQL language"