-->

如何传递参数给Doctrine2自定义函数在查询生成器选择()方法?(How can I pass

2019-10-17 10:54发布

在我的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_stringselect()方法。

我最初试图把FIELD()函数在orderBy()调用,但它看起来不像是能否解析定制DQL函数调用,和我想象我会遇到同样的问题,因为上面。

编辑1我知道我可以把基础数据直接进入select()调用。

编辑2我已经放弃了,把裸露的数据到select()调用(这是我想避免)。 这个工作,但随后以实现使用的科威特石油公司的建议成为必要HIDDEN关键字,以防止主义返回array(Object i, array(fixed_order))而不仅仅是Object i

Answer 1:

从学说2.2,您可以使用HIDDEN的无水化他们avability场关键字秩序。

尝试:

->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')


Answer 2:

你会踢自己,当你注意到这个问题...

尝试重新读你的句子:“所以显然这不是‘看’了:ids_string在select()方法”。

再看看你的代码仔细看:“ID_STRING” =>破灭(“”,$ itemIds)



文章来源: How can I pass a parameter to a Doctrine2 custom function in the Query Builder select() method?