-1

在实体存储库中:

$qb = $this->createQueryBuilder('c');
//....
$qb->addSelect('POWER('.$qb->expr()->abs(
                $qb->expr()->diff('c.latitude', $filter['latitude'])
            ).',2) AS ddst';
//....
return $qb->getQuery(); //to Pagerfanta with DoctrineORMAdapter

错误:

QueryException: [Syntax Error] line 0, col 11: Error: Expected known function, got 'POWER'

QueryException: SELECT c, (POWER(ABS(c.delivery_latitude - 47.227163),2) AS ddst 
FROM MyEntity c ORDER BY ddst ASC, c.created_at DESC (this is dql error)

有什么不对?Dql 不支持 POWER。我没有在 qb-expressions 中找到它。

4

3 回答 3

1

而且......也许它会对某人有所帮助。回答:

//app/config/config.yml

doctrine:
    dbal:
        #.....    
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        # auto_mapping: true #comment this line if isset
        entity_managers:
            default:
                auto_mapping: true #from orm to here or custom mapping
                dql:
                    numeric_functions:
                        power: Acme\MyBundle\DQL\PowerFunction #or power_num: ... it's an identifier

src/Acme/MyBundle/DQL/PowerFunction.php:

<?php
namespace Acme\MyBundle\DQL;

use Doctrine\ORM\Query\Lexer;

class PowerFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
{
    public $numberExpression = null;
    public $powerExpression = 1;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        //Check for correct
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->numberExpression = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->powerExpression = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'POWER(' .
            $this->numberExpression->dispatch($sqlWalker) . ', ' .
            $this->powerExpression->dispatch($sqlWalker) . ')';
    }
}

并使用(在 MyEntityRepository 中):

$qb = $this->createQueryBuilder('c');
//some code
$qb->addSelect('power('.$yourNumber.',2) AS powered_num');
//'power' must be in lowercase!!!; if idetifier in config for example, 'power_num', then write 'power_num($yournumber,2)'

//some code ...

return $qb->getQuery(); //or getResult()

完毕。

于 2012-10-18T02:40:10.623 回答
1

由于这个Bundle,我解决了同样的问题:

https://github.com/orocrm/doctrine-extensions

以下是处理方法。

1)安装库:

composer require oro/doctrine-extensions

2) 将 DQL 函数添加到您的学说配置中:

doctrine:
    orm:
        dql:
            numeric_functions:
                pow: Oro\ORM\Query\AST\Functions\Numeric\Pow

就这样。

现在 Doctrine 知道如何处理 SQL POW 函数了。

于 2015-03-08T10:25:16.087 回答
0

DQL 不是 SQL。它不支持很多比较晦涩的 SQL 函数,比如 POWER。

如果需要,您可以创建本机 SQL 查询。有关更多信息,请参阅此文档:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html

于 2012-10-17T12:54:13.427 回答