简体   繁体   English

DQL select语句中的参数(Symfony2 / Doctrine)

[英]Parameters in DQL select statement (Symfony2/Doctrine)

I'm trying to use external params in a DQL-s SELECT part, but it doesn't work due to an error. 我正在尝试在DQL-s SELECT部分​​中使用外部参数,但由于错误它不起作用。

What I'm trying: 我在尝试什么:

$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',
    ));

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). 我希望得到两列,结果是'column_one'的值和Select中的param值(在这种情况下为'静态值'作为参数)。

I get the following error: 我收到以下错误:

Error: Expected IdentificationVariable | 错误:期望的IdentificationVariable | ScalarExpression | ScalarExpression | AggregateExpression | AggregateExpression | FunctionDeclaration | 功能声明| PartialObjectExpression | PartialObjectExpression | "(" Subselect ")" | “(”Subselect“)”| CaseExpression, got ':param_doesnt_work' 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. 您只需将参数放在括号下即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM