在我的Symfony2项目,我检索从Elasticsearch指数的有序集合实体的ID。 然后我通过这个列表Doctrine2检索实际的实体,通过的方式WHERE IN()
调用。
这不正确的顺序返回它们,所以我想我需要使用MySQL特定的FIELD()
函数。 我创建了一个自定义的功能DQL允许的功能。
所以,现在我使用下面的代码来构建一个学说查询对象,但参数没有被解析成select()
方法:
$itemIds = array(4,8,2,1);
$this->getRepository()
->createQueryBuilder('i')
->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')
->where('i.id IN (:ids)')
->setParameters(array(
'ids_string' => implode(',', $itemIds),
'ids' => $itemIds))
->orderBy('fixed_order', 'ASC')
->getQuery()
;
这种失败,出现错误"Invalid parameter number: number of bound variables does not match number of tokens"
,因此很明显,这不是“看”了:ids_string
中select()
方法。
我最初试图把FIELD()
函数在orderBy()
调用,但它看起来不像是能否解析定制DQL函数调用,和我想象我会遇到同样的问题,因为上面。
编辑1我知道我可以把基础数据直接进入select()
调用。
编辑2我已经放弃了,把裸露的数据到select()
调用(这是我想避免)。 这个工作,但随后以实现使用的科威特石油公司的建议成为必要HIDDEN
关键字,以防止主义返回array(Object i, array(fixed_order))
而不仅仅是Object i