2012-09-17 3 views
2

J'ai créé une fonction "REPLACE" personnalisée dans Doctrine. Mais j'ai une erreur, quand je l'utilise.Symfony2, doctrine Fonction DQL personnalisée

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

Remplacer la fonction:

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 contient:

/** 
* 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')"; 
} 

Erreur:

[ 
    { 
     "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

Avez-vous trouvé une solution à votre problème? Je suis à la recherche d'un moyen d'écrire la fonction de remplacement DQL – Kosmonaft

+0

Non, mais j'ai décidé d'utiliser $ this -> _ em-> createNativeQuery ($ sql, $ rsm); + comme une condition simple. "ET cast (remplacer (weight, ',', '.') Comme float)> =: weight_from"; – Dezigo

+2

Si vous préférez utiliser DQL, je trouve déjà une solution: http://stackoverflow.com/questions/21534920/doctrine-squery-ignoring-spaces – Kosmonaft

Répondre

2

J'ai eu un problème similaire et je suppose que vous devriez simplement ajouter un préfixe à votre champ de table "poids" (par ex. "a."):

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