Symfony2,doctrine自定义DQL函数

时间:2012-09-17 07:52:59

标签: php symfony doctrine-orm

我在Doctrine中创建了一个自定义'REPLACE'函数。 但是当我使用它时,我有一个错误。

$result->andWhere("replace(weight,X,C) <= :weight_from")
       ->setParameter('weight_from',$data['weight_from']);

替换功能:

namespace Doctrine\ORM\Query\AST\Functions;

use Doctrine\ORM\Query\Lexer;

/**
 * "REPLACE" "(" StringPrimary "," StringSecondary "," StringThird ")"
 *
 * 
 * @link    www.prohoney.com
 * @since   2.0
 * @author  Igor Aleksejev
 */
class ReplaceFunction extends FunctionNode {

    public $stringPrimary;
    public $stringSecondary;
    public $stringThird;

    /**
     * @override
     */
    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
        return $sqlWalker->getConnection()->getDatabasePlatform()->getReplaceExpression(
                        $this->stringPrimary, $this->stringSecondary, $this->stringThird
        );
    }

    /**
     * @override
     */
    public function parse(\Doctrine\ORM\Query\Parser $parser) {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->stringPrimary = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->stringSecondary = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->stringThird = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

}

AbstracPlatfrom.php包含:

/**
 * Returns the replace of a text field.
 *
 * @param string $column
 * @param string $x
 * @param string $y
 *
 * @return string
 */
public function getReplaceExpression($column,$x,$y)
{
    return "REPLACE($column,'$x','$y')";
} 

错误:

[
   {
      "message":"[Syntax Error] line 0, col 103: Error: Expected '.' or '(', got 'weight'",
      "class":"Doctrine\\ORM\\Query\\QueryException",
      "trace":[
         {
            "namespace":"",
            "short_class":"",
            "class":"",
            "type":"",
            "function":"",
            "file":"C:\\webserver\\symfony\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\Query\\QueryException.php",
            "line":44,
            "args":[

            ]
         },
         {
            "namespace":"Doctrine\\ORM\\Query",
            "short_class":"QueryException",
            "class":"Doctrine\\ORM\\Query\\QueryException",
            "type":"::",
            "function":"syntaxError",
            // ...

1 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,我想你应该在你的表格字段中添加一个前缀&#34; weight&#34; (例如&#34; a。&#34;):

$result->andWhere("replace(a.weight,X,C) <= :weight_from")
   ->setParameter('weight_from',$data['weight_from']);