How can I merge 2 results from the same entity and sort by date ?
I've just this code at the moment :
$conversations1 = $this->getDoctrine()
->getManager()
->getRepository('AcmeMessageBundle:Conversation')
->getConversations($this->getUser()->getId(), $current);
$conversations2 = $this->getDoctrine()
->getManager()
->getRepository('AcmeAdosMessageBundle:Conversation')
->getConversationsInverse($this->getUser()->getId(),$current);
But if I merge conversations 1 and conservation 2 the dates won't sort correctly. Is there an easy way to merge and sort by date ?
I've any idea how to do that.
Edit my solution
$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);
I think you can merge the 2 arrays into an ArrayCollection
and then sort that (by way of making into a iterator and back again).
$merged = new ArrayCollection(
array_merge(
$conversations1,
$conversations2
)
);
$iterator = $merged->getIterator();
$iterator->uasort(function ($a, $b) {
return ($a->getDate() < $b->getDate()) ? -1 : 1;
});
$conversations = new ArrayCollection(iterator_to_array($iterator));
It's hardly the prettiest way of going about things but it might just work.
Note This was cobbled together from a bunch of different answers so I'm not sure of the quality of the actual results.