Parameters in DQL select statement (Symfony2/Doctrine)

怎甘沉沦 提交于 2019-12-21 05:34:12

问题


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

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).

I get the following error:

Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got ':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.


回答1:


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.



来源:https://stackoverflow.com/questions/22042472/parameters-in-dql-select-statement-symfony2-doctrine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!