学说2.2双重类型



在实体存储库中:

$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不支持电源。我没有在qb-exptressions中找到它。

和...也许对某人有帮助。答案:

//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: AcmeMyBundleDQLPowerFunction #or power_num: ... it's an identifier

src/acme/mybundle/dql/powerfunction.php:

<?php
namespace AcmeMyBundleDQL;
use DoctrineORMQueryLexer;
class PowerFunction extends DoctrineORMQueryASTFunctionsFunctionNode
{
    public $numberExpression = null;
    public $powerExpression = 1;
    public function parse(DoctrineORMQueryParser $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(DoctrineORMQuerySqlWalker $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()

完成。

我通过此捆绑包解决了同一问题:

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

这是处理的方法。

1)安装库:

composer require oro/doctrine-extensions

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

doctrine:
    orm:
        dql:
            numeric_functions:
                pow: OroORMQueryASTFunctionsNumericPow

仅此而已。

现在学说知道如何处理SQL POW函数。

dql不是SQL。它不支持许多更晦涩的SQL功能,例如Power。

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

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

相关内容

  • 没有找到相关文章

最新更新