0

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?

1
  • @jamie0726 Somehow that wasn't working for me.
    – hipnosis
    Commented Dec 7, 2013 at 15:31

2 Answers 2

1

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'));
0

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 }}

1
  • Twig would be easy. The problem is I am outputting this result to an RSS feed bundle, not Twig. I guess I'll have to write some comparison logic between the two results to merge them?
    – hipnosis
    Commented Dec 7, 2013 at 15:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.