【问题标题】:User defined function on zf2 + doctrine 2 not workingzf2 + 学说 2 上的用户定义函数不起作用
【发布时间】:2016-11-12 22:04:24
【问题描述】:

我已按照学说参考页面和其他几个来源的官方说明进行操作,但我不知道出了什么问题。 显然它没有加载我的用户定义函数。 我正在使用 ZF2 + Doctrine 2 和自动加载器设置。

这是我得到的:

错误:

[语法错误] 第 0 行,第 7 列:错误:预期标识变量 | 标量表达式 |聚合表达式 |功能声明 | 部分对象表达式 | "(" 子选择 ")" | CaseExpression,得到 '来自'

args : ["SELECT FROM CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) = :uf"] 0 : "从选择 CadastrosAuxiliares\Entity\Bairro b WHERE upper_tira_acento(b.uf) = :uf" 类:"Doctrine\ORM\Query\QueryException" 文件: “(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php”函数 :“dqlError”行:448 类型:“::”

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 构建器示例:

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

有人知道可能是什么问题吗?

提前感谢您的帮助。

【问题讨论】:

    标签: php zend-framework doctrine-orm zend-framework2 user-defined-functions


    【解决方案1】:

    抛出错误是因为您的 SELECT 子句中没有任何内容。不知道是什么原因造成的。

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

    应该是

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

    像这样尝试一次:

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

    【讨论】:

      猜你喜欢
      • 2013-08-14
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多