Symfony / doctrine - 未定义参数的DQL顺序

时间:2018-03-09 09:58:04

标签: symfony doctrine dql

任何人都可以告诉我为什么这段代码不起作用:

$sort = 'u.username'; //test

$dql = 'SELECT u, p FROM GPAuthBundle:User u
    JOIN u.profile p
    WHERE ( u.username LIKE :username )
    AND ( u.username LIKE :search OR p.name LIKE :search )
    ORDER BY :sort ASC';

$parameters = array(
    'username' => '%'.$request->query->get('username').'%',
    'search' => '%'.$request->query->get('search').'%',
    'sort' => $sort
);

$query = $manager->createQuery($dql)->setParameters($parameters);

错误:

  

[语义错误]第0行,第170行附近':sort ASC':错误:':sort'未定义。

1 个答案:

答案 0 :(得分:0)

您无法在ORDER中使用,因为setParameters()会引用引号:ORDER BY "myvalue" ASC而您想要ORDER BY myvalue ASC

使用PHP var,例如:

ORDER BY ' . $sort . ' ASC