php Symfony 2 Doctrine 按 id 的有序数组查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28563738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony 2 Doctrine find by ordered array of id
提问by Benjamin
I'm looking for a way to use Doctrine
in Symfony 2
to find items using an ordered array of id.
我正在寻找一种方式来使用Doctrine
中Symfony 2
发现使用ID的有序阵列项目。
I have a Card entity with id (primary key) and title.
我有一个带有 id(主键)和标题的 Card 实体。
I have a ListCards entity with id (primary key) and a listCards (an array of ids encoded : ["16", "2", "84"]
)
我有ID(主键)和一个listCards一个ListCards实体(编码ID数组:["16", "2", "84"]
)
I first fetch the list and then I need to find cards with those ids in that order.
我首先获取列表,然后我需要按该顺序查找具有这些 ID 的卡片。
I try something like :
我尝试类似:
$idsArray = ["16", "2", "84"];
$cardRepository->findby($idsArray);
butDoctrine
fetch my cards in ASC
order.
但Doctrine
要按ASC
顺序取我的卡。
ORDER BY FIEDS
sql method doesn't seem to be supported by doctrine.
ORDER BY FIEDS
sql 方法似乎不受学说支持。
Is there any simple solution for that kind of sorting ?
这种排序有什么简单的解决方案吗?
Thank you (and sorry for my bad english).
谢谢(并抱歉我的英语不好)。
回答by Max Lipsky
You can use it like:
你可以像这样使用它:
$cardRepository->findBy( array('id' => $idsArray), array('id' => 'DESC') );
Check also the official doctrine documentationfor more details on how to use ordering, limit and offset as second to fourth parameters in the findBy
method.
有关如何使用排序、限制和偏移量作为方法中的第二到第四个参数的更多详细信息,另请查看官方准则文档findBy
。
回答by Lajos Arpad
You can create a helper table, where you store ordered group elements, having the following data: (group_id, card_id, order)
您可以创建一个辅助表,在其中存储有序的组元素,其中包含以下数据: (group_id, card_id, order)
You search by group
_id, order by order
and read the card_id
.
您可以通过group
_id搜索、排序order
并阅读card_id
.