设置参数的学说 2 查询生成器不起作用
这是我的查询:
public function getDetails($userid, $orderby, $sort){
$query = $this->_em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->where('u.userid= ?1')
->orderBy('u.?3', '?3')
->setParameter(1, $userid)
->setParameter(2, $orderby)
->setParameter(3, $sort)
->getQuery()
->getResult();
}
它不断出错: Message: [Semantical Error] line 0, col 83 close '?3 DESC': Error: '?3' is not Defined 。
如何从该函数的属性中获取 orderby 到查询?
this is my query:
public function getDetails($userid, $orderby, $sort){
$query = $this->_em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->where('u.userid= ?1')
->orderBy('u.?3', '?3')
->setParameter(1, $userid)
->setParameter(2, $orderby)
->setParameter(3, $sort)
->getQuery()
->getResult();
}
it keeps erroring: Message: [Semantical Error] line 0, col 83 near '?3 DESC': Error: '?3' is not defined.
how do i get the orderby from the properties in that function to the query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用占位符来动态构建 DQL 查询。您必须自己编写代码:
You can't use placeholders for dinamical build of DQL query. You'll have to code it by your own:
您无法将参数绑定到 QueryBuilder,只能绑定到 Query,因此只需交换行,首先从构建器中获取查询,然后用参数填充它并获取结果。
在 2.4 中它是固定的,并且您可以将参数绑定到 QueryBuilder。
更新:我错过了字段名称中占位符的时刻,SQL 不支持此类构造。
You cant bind parameters to QueryBuilder, only to Query, so just swap lines, first get query out of builder, then fill it with parameters and get result.
In doctrine 2.4 its fixed, and you can bind parameters to QueryBuilder.
Update: i've missed moment with placeholder in field name, SQL do not support such constructions.