Question

I would like to get the ten most recent entries between two entities (post, news).

In my controller:

$posts = $em->getRepository('AcmePostBundle:Post')->getTenLatest();

$news = $em->getRepository('AcmeNewsBundle:News')->getTenLatest();

How do I merge the two results? Something like:

$latest = $posts->merge($news);

And then, sort them by a date field, limit 10?

Was it helpful?

Solution

I was able to compare the dates of posts and news and add the newest to a new array. Then send the new array to the rss feed bundle.

$news = $em->getRepository('ACMENewsBundle:News')->getLatest();

$posts = $em->getRepository('ACMEPostsBundle:Posts')->getLatest();

$latest = [];
$latest_news = 0;
$latest_post = 0;

for ($i = 0; $i < 7; $i++) {
    if ($news[$latest_news]->getUpdated() > $posts[$latest_article]->getUpdated()) {
            $latest[$i] = $lessons[$latest_news];
            $latest_news++;
        } else {
            $latest[$i] = $posts[$latest_post];
            $latest_post++;
        }
    }

    $feed = $this->get('eko_feed.feed.manager')->get('article');
    $feed->addFromArray($latest);

    return new Response($feed->render('rss'));

OTHER TIPS

You don't. Because they're 2 different entities.

You pass them to the template renderer (default twig) as

array('news'=> $news, 'posts' => $posts)

if not using @Template()

return $this->render('AcmeBlogBundle:Blog:index.twig',array('news'=> $news, 'posts' => $posts));

if using @Template()

return array('news'=> $news, 'posts' => $posts);

You can access them with {{ news.fieldname }} or {{ posts.fieldname }}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top