Parameters in DQL select statement (Symfony2/Doctrine)
Asked Answered
D

1

5

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.

Danger answered 26/2, 2014 at 13:11 Comment(0)
S
8

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.

Sexton answered 30/4, 2014 at 9:32 Comment(2)
The little details missing from the doc :). Thank you very much!Danger
When I used createQueryBuilder, I had to move the "param" outside the parentheses, like this: ->select('me.ID, (:param_works) param')Other

© 2022 - 2024 — McMap. All rights reserved.