如何合併來自同一實體的2個結果並按日期排序?合併來自同一個實體的2個結果
我此刻剛剛驗證碼:
$conversations1 = $this->getDoctrine()
->getManager()
->getRepository('AcmeMessageBundle:Conversation')
->getConversations($this->getUser()->getId(), $current);
$conversations2 = $this->getDoctrine()
->getManager()
->getRepository('AcmeAdosMessageBundle:Conversation')
->getConversationsInverse($this->getUser()->getId(),$current);
但是如果我合併的對話1個保護2日期不會正確排序。有沒有簡單的方法來合併和按日期排序?
我有任何想法如何做到這一點。
編輯我的解決方案
$conversations1 = $this->getDoctrine()
->getManager()
->getRepository('AcmeMessageBundle:Conversation')
->getMoreConversations($this->getUser()->getId(), $current, $conversation->getDate()->format('Y-m-d H:i:s'));
$conversations2 = $this->getDoctrine()
->getManager()
->getRepository('AcmeMessageBundle:Conversation')
->getMoreConversationsInverse($this->getUser()->getId(), $current, $conversation->getDate()->format('Y-m-d H:i:s'));
$conversations = array();
foreach (array_merge($conversations1, $conversations2) as $conversation) {
$conversations[$conversation->getDate()->format('Y-m-d H:i:s')] = $conversation;
}
krsort($conversations);
你能澄清你的意思是「合併」嗎?你想用另一個對象的屬性來更新一個對象的屬性嗎?你想要一組對話對象嗎?還有別的嗎? – 2014-08-30 15:58:19
不,不是,它只是用來顯示對話列表。但爲了獲得所有對話,我必須提出兩個疑問。對話需要按日期排序。所以如果我有理由做一個array_merge();這不是按日期排序。這將是這樣的秩序:conversations1_30may,conversation1_25may,conversation2_28may,conversation2_22may。我需要這樣的命令:conversations1_30may,conversation2_28may,conversation1_25may,conversation2_22may。所以我想問一下在教條中是否有辦法,或者是否有人爲此寫了一個腳本。 – Hotgeart 2014-08-30 16:40:40