简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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