MATCH AGAINST脚本不能与Symfony2一起使用

时间:2013-07-08 19:49:43

标签: php symfony doctrine dql

我尝试在Symfony2项目中设置此类: http://xlab.pl/en/full-text-searching/(与部分匹配)

这个课应该允许我使用匹配与这样的学说:

$searchResult =      $em->createQueryBuilder('uvaluo_user')
                                ->addSelect("MATCH_AGAINST (uvaluo_user.firstname, uvaluo_user.lastname, :name 'IN NATURAL MODE') as score")
                                ->add('where', 'MATCH_AGAINST (uvaluo_user.firstname, uvaluo_user.lastname, :name) > 0.8')
                                ->setParameter('name', $name)
                                ->getQuery()
                                ->getResult();

正如他们在网站上所说,我把它放在我app的config.yml中:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true
    dql:
      string_functions:
        match_against: Uvaluo\UserBundle\Extension\Doctrine\MatchAgainstFunction

我有类似MatchAgainstFunction这样的

<?

namespace Uvaluo\UserBundle\Extension\Doctrine;

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

/**
 * @example by https://gist.github.com/1234419 Jérémy Hubert 
 * "MATCH_AGAINST" "(" {StateFieldPathExpression ","}* InParameter {Literal}? ")"
 */
class MatchAgainstFunction extends FunctionNode {
//Content that I got in the website
}

最后,当我尝试我的代码时,我得到了以下错误:

  

[语义错误]第0行,第107行'MATCH_AGAINST'附近:错误:未定义类'MATCH_AGAINST'。   500内部服务器错误 - QueryException   1个链接的异常:QueryException»

你知道这是什么原因吗?

2 个答案:

答案 0 :(得分:2)

基于这篇文章Extending DQL in Doctrine 2: User-Defined Functions

config.yml:

doctrine:
  orm:
    dql:
      string_functions:
        MATCH: My\Bundle\DQL\MatchAgainstFunction

MatchAgainstFunction.php

<?php

namespace My\Bundle\DQL;

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

在您的存储库中:

class MyRepository extends EntityRepository
{

    public function getUsingAgainstMatch() {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb
            ->select('m')
            ->from('MyBundle:MyEntity', 'm')
            ->andWhere('MATCH (m.field) AGAINST (:field) > 1')
            ->setParameter('field', 'value')
        ;
        return $qb;
    }

    //...

}

你可以在repository找到很多Doctrine Extensions。

希望这有帮助

答案 1 :(得分:0)

您可以通过在作曲家中要求"beberlei/DoctrineExtensions":"^1.0",来安装doctrineExtensions。那么你将有很多mysql函数以及匹配,那么你可以在querybuilder中的where子句或自定义查询中使用match函数而没有任何问题。

安装捆绑包后,您需要转到配置文件并添加所需的功能

doctrine:
   .
   .
   .
    orm:
    .
    .
    .
       dql:
           string_functions:
                MATCH: DoctrineExtensions\Query\Mysql\MatchAgainst