Doctrine DQL Extention在Zend Framework 2中不起作用

时间:2015-08-27 18:30:50

标签: doctrine-orm zend-framework2 dql

我刚刚在我的ZF2应用程序中添加了DQL扩展,但它无法正常工作。 扩展名应允许匹配查询。

在我的module.config.php中我有

'doctrine' => array(
        'driver' => array(
            'Application_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/Application/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'Application\Entity' => 'Application_driver'
                ),
            ),
        ),
        'configuration' => array(
            'orm_default' => array(
                'string_functions' => array(
                    'MatchAgainst' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',
                ),
                'datetime_functions' => array(),
                'numeric_functions'   => array(),
                'metadata_cache'     => 'filesystem',
                'query_cache'        => 'filesystem',
                'result_cache'       => 'filesystem',
            ),
        ),
    ),

在我的DQL中我有

$builder->andwhere('MATCH (`t.title`) AGAINST (:title)')
        ->setParameter('title', $param);

扩展甚至没有被执行我得到的只是一个错误。

错误:预期的已知函数,得到'MATCH'

有谁知道我错过了什么?

非常感谢提前。

修改

通过将'MatchAgainst' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',行更改为'Match' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',,我的扩展程序正常工作。

我现在遇到的问题是DQL查询仍无效。

我的DQL扩展程序如

namespace Freedom\Doctrine\ORM\AST\Functions;

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

/**
 * MatchAgainstFunction ::=
 *  "MATCH" "(" StateFieldPathExpression {"," StateFieldPathExpression}* ")" "AGAINST" "("
 *      StringPrimary ["BOOLEAN"] ["EXPAND"] ")"
 */
class MatchAgainstFunction extends FunctionNode
{

    /** @var array list of \Doctrine\ORM\Query\AST\PathExpression */
    protected $pathExp = null;

    /** @var string */
    protected $against = null;

    /** @var boolean */
    protected $booleanMode = false;

    /** @var boolean */
    protected $queryExpansion = false;

    public function parse(Parser $parser)
    {
        // match
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        // first Path Expression is mandatory
        $this->pathExp = array();
        $this->pathExp[] = $parser->StateFieldPathExpression();

        // Subsequent Path Expressions are optional
        $lexer = $parser->getLexer();
        while ($lexer->isNextToken(Lexer::T_COMMA)) {
            $parser->match(Lexer::T_COMMA);
            $this->pathExp[] = $parser->StateFieldPathExpression();
        }

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

        // against
        if (strtolower($lexer->lookahead['value']) !== 'against') {
            $parser->syntaxError('against');
        }

        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->against = $parser->StringPrimary();

        if (strtolower($lexer->lookahead['value']) === 'boolean') {
            $parser->match(Lexer::T_IDENTIFIER);
            $this->booleanMode = true;
        }

        if (strtolower($lexer->lookahead['value']) === 'expand') {
            $parser->match(Lexer::T_IDENTIFIER);
            $this->queryExpansion = true;
        }

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

    public function getSql(SqlWalker $walker)
    {
        $fields = array();
        foreach ($this->pathExp as $pathExp) {
            $fields[] = $pathExp->dispatch($walker);
        }

        $against = $walker->walkStringPrimary($this->against)
                . ($this->booleanMode ? ' IN BOOLEAN MODE' : '')
                . ($this->queryExpansion ? ' WITH QUERY EXPANSION' : '');

        return sprintf('MATCH (%s) AGAINST (%s)', implode(', ', $fields), $against);
    }

}

在我的DQL中我有

$builder->andWhere('MATCH (t.title) AGAINST (:title)')
        ->setParameter('title', $param);

现在我收到了错误

[语法错误]第0行,第204行:错误:预期=,<,< =,<>,>,> =,!=,得到'订购'

有谁知道为什么Doctrine正在寻找比较运算符?

1 个答案:

答案 0 :(得分:0)

通过将行从'MatchAgainst' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',更改为'Match' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',

,我的扩展程序正常运行

在回答我的第二个问题时,我发现这是有效的

$builder->andWhere('MATCH (t.title) AGAINST (:title BOOLEAN) > 0')
        ->setParameter('title', $param);