Raphael
Raphael

Reputation: 11

User defined function on zf2 + doctrine 2 not working

I've followed official instructions on doctrine's ref page and on several other sources, but I cannot figure out what's wrong. Apparently it is not loading my user-defined function. I'm using ZF2 + Doctrine 2 with autoloader setup.

Here's what I've got:

The error:

[Syntax Error] line 0, col 7: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got 'FROM'

args : ["SELECT FROM CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) = :uf"] 0 : "SELECT FROM CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) = :uf" class : "Doctrine\ORM\Query\QueryException" file : "(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php" function : "dqlError" line : 448 type : "::"

doctrine.global.php

<?php
return array(
    'doctrine' => array(
        'configuration' => array(
            'orm_default' => array(
                'string_functions'   => array(
                    'upper_tira_acento' =>   'Application\DoctrineFunction\UpperTiraAcento'
                ),
            )
        )
    )
);

UpperTiraAcento.php

<?php 
namespace Application\DoctrineFunction;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\SqlWalker;

class UpperTiraAcento extends FunctionNode
{
    public $parameters = array();

    const STRING_PARAM = 'string';

    /**
     * {@inheritdoc}
     */
    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->parameters[self::STRING_PARAM] = $parser->StringPrimary();

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        $string = $sqlWalker->walkStringPrimary($this->parameters[self::STRING_PARAM]);

        return sprintf('dbo.upper_tira_acento(%s)',$string);
    }
}

DQL builder example:

$qbBairro = $em->createQueryBuilder()->from('CadastrosAuxiliares\Entity\Bairro', 'b');
$qbBairro->andWhere("upper_tira_acento(b.uf) = :uf")->setParameter('uf', $estado->getId());

Does anyone have any idea of what could possibly be the issue?

Thanks in advance for any help.

Upvotes: 0

Views: 113

Answers (1)

Wilt
Wilt

Reputation: 44422

The error is thrown because there is nothing in your SELECT clause. Not sure what is causing this.

SELECT FROM CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) 

should be

SELECT b FROM CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) 
       ^

Try once like this:

$em->createQueryBuilder('b')
//                       ^
   ->from('CadastrosAuxiliares\Entity\Bairro', 'b');
   ->andWhere("upper_tira_acento(b.uf) = :uf")->setParameter(
       'uf', $estado->getId()
   );

Upvotes: 0

Related Questions