2012-09-17 90 views
2

我在Doctrine中創建了一個自定義的'REPLACE'函數。 但我有一個錯誤,當我使用它。Symfony2,教義自定義DQL函數

$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", 
      // ... 
+0

您是否爲您的問題找到了解決方案? 我正在尋找一種方式來編寫DQL替換函數 – Kosmonaft

+0

不,但我決定使用$ this - > _ em-> createNativeQuery($ sql,$ rsm); +作爲一個簡單的條件。 「並鑄造(替換(重量,',','。')爲float)> =:weight_from」; – Dezigo

+2

如果你喜歡使用DQL,我已經找到了解決方案: http://stackoverflow.com/questions/21534920/doctrine-squery-ignoring-spaces – Kosmonaft

回答

2

我有一個類似的問題,我想你應該添加一個前綴到你的表字段「weight」(例如, 「a。」):

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