DQL select语句中的参数(Symfony2 / Doctrine) [英] Parameters in DQL select statement (Symfony2/Doctrine)

查看:164
本文介绍了DQL select语句中的参数(Symfony2 / Doctrine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在DQL的SELECT部分​​中使用外部参数,但是由于错误而无法使用。

I'm trying to use external params in a DQL-s SELECT part, but it doesn't work due to an error.

我正在尝试:

$query = $this->getEntityManager()
    ->createQuery("
        SELECT me.column_one, :param_doesnt_work param
        FROM CompanyMyBundle:MyEntity me
        WHERE me.column_one = :param_one
        AND me.column_two = :param_two
    ")->setParameters(array(
        'param_doesnt_work' => 'A static value',
        'param_one' => 'some param',
        'param_two' => 'another param',
    ));

我想获得两列结果,column_one的值和选择中的参数(在这种情况下为静态值作为参数)。

I would like to get two columns as a result, the value of 'column_one' and the value of the param in the Select ('A static value' in this case As param).

我收到以下错误:

错误:预期的IdentificationVariable |标量表达|聚合表达|函数声明| PartialObjectExpression | (Subselect)| CaseExpression,得到':param_doesnt_work'

甚至可以在那里使用参数,还是有完全不同的解决方案?没有找到任何例子。

Is it even possible to use parameters there, or there is a completly different solution for this? Couldn't find any example.

推荐答案

我刚刚遇到同样的问题。

I just had the same problem.

这是我找到的解决方案:

Here is the solution I found :

$query = $this->getEntityManager()
->createQuery("
    SELECT me.column_one, (:param_doesnt_work param)
    FROM CompanyMyBundle:MyEntity me
    WHERE me.column_one = :param_one
    AND me.column_two = :param_two
")->setParameters(array(
    'param_doesnt_work' => 'A static value',
    'param_one' => 'some param',
    'param_two' => 'another param',
));

您只需将参数放在括号下。

You just have to put your parameter under parentheses.

这篇关于DQL select语句中的参数(Symfony2 / Doctrine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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