CASTING属性用于在Doctrine2 DQL查询中排序 [英] CASTING attributes for Ordering on a Doctrine2 DQL Query

查看:198
本文介绍了CASTING属性用于在Doctrine2 DQL查询中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取 Doctrine2实体,按照其ID排序,显然它是一个字符串,即使它只包含数字。
所以我想做的是这样的:

  SELECT entity1,cast(entity1.id AS integer )as orderId 
FROM Namespace\Bla\MyEntity
ORDER BY orderId

有没有办法在 Doctrine2 中执行这样的操作?
或者,如果我不能更改id的类型(由于客户的要求当然),那么获得我的结果的最佳做法是什么?

$ b $注意

:我不是在询问SQL代码,我要求一个Doctrine2解决方案,最好在DQL

解决方案

您应该能够添加您自己的函数以实现此功能。



类看起来像这样:

 命名空间MyProject\Query; 

使用Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class CastAsInteger extends FunctionNode
{
public $ stringPrimary;

public function getSql(SqlWalker $ sqlWalker)
{
return'CAST('。$ this-> stringPrimary-> dispatch($ sqlWalker)。'AS integer) ';
}

public function parse(Parser $ parser)
{
$ parser-> match(Lexer :: T_IDENTIFIER);
$ parser-> match(Lexer :: T_OPEN_PARENTHESIS);

$ this-> stringPrimary = $ parser-> StringPrimary();

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

您需要注册您的功能: p>

  $ config = $ em-> getConfiguration(); 
$ config-> addCustomNumericFunction('INT','MyProject\Query\CastAsInteger');

然后您可以使用它:

  SELECT e,INT(e.id)AS HIDDEN orderId FROM Namespace\Bla\ MyEntity e ORDER BY orderId 

PS:通过添加 HIDDEN 关键字,别名 orderId 不在结果中(仅用于排序)。


I am trying to get Doctrine2 Entities, ordered by their ID which apparently is a String even though it contains only Numbers. So what I would like to do is something like this:

SELECT entity1, cast (entity1.id AS integer) AS orderId
FROM Namespace\Bla\MyEntity 
ORDER BY orderId

Is there a way to do something like this in Doctrine2? Or, what would be the best practise to get my Result if i can't change the type of the id (due to customer requirements of course)?


Attention: I am not asking SQL Code, i am asking for a Doctrine2 Solution, preferably in DQL

解决方案

You should be able to add your own function to implement this feature.

The class would look something like this:

namespace MyProject\Query;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class CastAsInteger extends FunctionNode
{
    public $stringPrimary;

    public function getSql(SqlWalker $sqlWalker)
    {
        return 'CAST(' . $this->stringPrimary->dispatch($sqlWalker) . ' AS integer)';
    }

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->stringPrimary = $parser->StringPrimary();

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

You'll need to register your function:

$config = $em->getConfiguration();
$config->addCustomNumericFunction('INT', 'MyProject\Query\CastAsInteger');

Then you can use it:

SELECT e, INT(e.id) AS HIDDEN orderId FROM Namespace\Bla\MyEntity e ORDER BY orderId

PS: By adding the HIDDEN keyword, the alias orderId won't be in the results (and is only used for ordering).

这篇关于CASTING属性用于在Doctrine2 DQL查询中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆