任何人都可以告诉我为什么这段代码不起作用:
$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'未定义。
答案 0 :(得分:0)
您无法在ORDER中使用,因为setParameters()
会引用引号:ORDER BY "myvalue" ASC
而您想要ORDER BY myvalue ASC
使用PHP var,例如:
ORDER BY ' . $sort . ' ASC