Also, see the docs about what functions are already available:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-functions

-- 
Jasper N. Brouwer
(@jaspernbrouwer)


On 16 December 2014 at 21:55:37, Jasper N. Brouwer ([email protected]) wrote:
> A custom function is indeed the way to go. Personally I would go with 
> something like this: 
> 
> class InstrFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode 
> {
> public $stringExpression = null;
> public $patternExpression = null;
> public $startingPositionExpression = null;
> public $nthLocationExpression = null;
> 
> public function parse(\Doctrine\ORM\Query\Parser $parser)
> {
> $lexer = $parser->getLexer();
> 
> $parser->match(Lexer::T_IDENTIFIER);
> $parser->match(Lexer::T_OPEN_PARENTHESIS);
> $this->stringExpression = $parser->StringPrimary();
> $parser->match(Lexer::T_COMMA);
> $this->patternExpression = $parser->StringPrimary();
> 
> if ($lexer->isNextToken(Lexer::T_COMMA)) {
> $parser->match(Lexer::T_COMMA);
> $this->startingPositionExpression = $parser->SimpleArithmeticExpression(); 
> }
> 
> if ($lexer->isNextToken(Lexer::T_COMMA)) {
> $parser->match(Lexer::T_COMMA);
> $this->nthLocationExpression = $parser->SimpleArithmeticExpression();
> }
> 
> $parser->match(Lexer::T_CLOSE_PARENTHESIS);
> }
> 
> public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
> {
> $sql =
> 'INSTR(' . $sqlWalker->walkStringPrimary($this->stringExpression)
> . ', ' . $sqlWalker->walkStringPrimary($this->patternExpression);
> 
> if ($this->startingPositionExpression !== null) {
> $sql .= ', ' . 
> $sqlWalker->walkSimpleArithmeticExpression($this->startingPositionExpression);
>  
> 
> if ($this->nthLocationExpression !== null) {
> $sql .= ', ' . 
> $sqlWalker->walkSimpleArithmeticExpression($this->nthLocationExpression); 
> }
> }
> 
> $sql .= ')';
> 
> return $sql;
> }
> }
> 
> And register it as a numeric function:
> 
> $config->addCustomNumericFunction('INSTR', new InstrFunction());


-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to