簡體   English   中英

ZF2 +准則2上的用戶定義功能不起作用

[英]User defined function on zf2 + doctrine 2 not working

我已經遵循了教義的參考資料頁和其他來源上的官方說明,但是我無法弄清楚出了什么問題。 顯然它沒有加載我的用戶定義函數。 我正在使用ZF2 + Doctrine 2進行自動加載程序設置。

這是我得到的:

錯誤:

[語法錯誤]行0,第7列:錯誤:預期的標識變量| 標量表達| AggregateExpression | 函數聲明| PartialObjectExpression | “(”子選擇“)” | CaseExpression,顯示為“ FROM”

args:[“從CadastrosAuxiliares \\ Entity \\ Bairro b選擇,而upper_tira_acento(b.uf)=:uf”] 0:“從CadastrosAuxiliares \\ Entity \\ Bairro b選擇,而upper_tira_acento(b.uf)=:uf” class:“ 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());

有誰知道可能是什么問題?

在此先感謝您的幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM