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

--  
Jasper N. Brouwer
(@jaspernbrouwer)


On 16 December 2014 at 19:10:37, Pablo ([email protected]) wrote:
> Thank Jasper, I'm starting and their tips were of great value.
>  
> I need it because I will use the "instr" function that dbl has not
> implemented natively.
>  
> I found a library that apparently would solve my problem out, what u think
> of it?
>  
> https://github.com/IAkumaI/doctrine-functions


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