Parameters in DQL select statement (Symfony2/Doctr

2019-04-01 16:26发布

问题:

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.