Symfony2按字段创建QueryQuery顺序 [英] Symfony2 createQuery order by field

查看:111
本文介绍了Symfony2按字段创建QueryQuery顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询writen在phpmyadmin,它的工作gr8。

Hi i have this query writen in phpmyadmin and it works gr8.

SELECT u.* FROM users AS u WHERE
        u.id = 14469 OR
        u.id = 685

        ORDER BY u.id, field(u.id, 14469, 685)

但是我需要在symfony2中写。怎么样?因为这是给我一个错误:

But i need to write it in symfony2. How it will looks like? Because this is throwing me an error:

    $query=$this->_em->createQuery("SELECT u FROM UserBundle:User u WHERE
        u.id = 14469 OR
        u.id = 685

        ORDER BY u.id, field(u.id, 14469, 685)
    ");

在渲染模板期间抛出异常([Syntax Error] line 0,col 122:错误:字符串的预期结束,得到'('')

An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 122: Error: Expected end of string, got '('")

或者它不允许,我必须安装并使用一些教义扩展?

Or its not allowed and i have to install and use some doctrine extension?

推荐答案

FIELD()函数是MySQL具体的,因此不是Doctrine 2库的一部分。
您可以使用自定义DQL函数,该函数已由Doctrine(Benjamin Eberlei)的主要开发人员创建。 https: /github.com/beberlei/DoctrineExtensions/

The FIELD() function is MySQL specific and therefore not part of the Doctrine 2 library. You can use a custom DQL function, which is already created by the lead developer of Doctrine (Benjamin Eberlei). https://github.com/beberlei/DoctrineExtensions/.

另外我确定你需要定义 FIELD()函数作为隐藏,因为您不能在DQL中按顺序使用函数,将其标记为隐藏将阻止函数输出在结果集中水合。以下行:

Also I'm pretty sure you need to define the FIELD() function as hidden because you can't use functions in the order by in DQL. Marking it as hidden will prevent the function output to be hydrated in the resultset. Something in the line of:

$query=$this->_em->createQuery("SELECT u, field(u.id, 14469, 685) as HIDDEN field FROM UserBundle:User u WHERE
    u.id = 14469 OR
    u.id = 685
    ORDER BY u.id, field
");

这篇关于Symfony2按字段创建QueryQuery顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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