Search code examples
doctrine-ormzend-framework2dql

Doctrine DQL Extention not working in Zend Framework 2


I have just added a DQL extension into my ZF2 app but it is not working. The extension should allow a match against query.

In my module.config.php I have

'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',
            ),
        ),
    ),

and in my DQL I have

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

The extension is not even being executed all I get is an error.

Error: Expected known function, got 'MATCH'

Does anyone know what I am missing?

Many thanks in advance.

EDIT

I have got the extension working by changing the line from 'MatchAgainst' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction', to 'Match' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',.

The problem I am having now is the DQL query is still not working.

My DQL extension looks like

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);
    }

}

and in my DQL I have

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

Now I am getting the error

[Syntax Error] line 0, col 204: Error: Expected =, <, <=, <>, >, >=, !=, got 'ORDER'

Does anyone know why Doctrine is looking for a comparison operator?


Solution

  • I have got the extension working by changing the line from 'MatchAgainst' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction', to 'Match' => 'Freedom\Doctrine\ORM\AST\Functions\MatchAgainstFunction',

    In answer to my second question I found this works

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