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?

hipnosis
  • 618
  • 1
  • 8
  • 13

2 Answers2

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'));
hipnosis
  • 618
  • 1
  • 8
  • 13
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 }}

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • 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 Dec 07 '13 at 15:33