设置参数的学说 2 查询生成器不起作用 [英] doctrine 2 querybuilder with set parameters not working
本文介绍了设置参数的学说 2 查询生成器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的查询:
公共函数getDetails($userid, $orderby, $sort){
public function getDetails($userid, $orderby, $sort){
$query = $this->_em->createQueryBuilder()
->select('u')
->from('EntitiesUsers', 'u')
->where('u.userid= ?1')
->orderBy('u.?3', '?3')
->setParameter(1, $userid)
->setParameter(2, $orderby)
->setParameter(3, $sort)
->getQuery()
->getResult();
}
它一直出错:消息:[Semantical Error] line 0, col 83 near '?3 DESC': Error: '?3' is not defined.
it keeps erroring: Message: [Semantical Error] line 0, col 83 near '?3 DESC': Error: '?3' is not defined.
如何从该函数的属性中获取 orderby 到查询?
how do i get the orderby from the properties in that function to the query?
推荐答案
您不能将占位符用于 DQL 查询的动态构建.您必须自己编写代码:
You can't use placeholders for dinamical build of DQL query. You'll have to code it by your own:
$sortBy = in_array($sortBy, array(...)) ? $sortBy : 'id';
$sortDir = $sortDir == 'ASC' ? 'ASC' : 'DESC';
$this->em->createQueryBuilder()
...
->orderBy('u.' . $sortBy, $sortDir)
这篇关于设置参数的学说 2 查询生成器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文