B
B
BurBurunduk2018-02-12 21:08:22
symfony
BurBurunduk, 2018-02-12 21:08:22

Symfony + Doctrine how to display 5 latest records from database?

How to display the last 5 records from a mySQL database in Symfony + Doctrine?
The controller has this code that displays all the records at once:

public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $posts = $em->getRepository('BlogBundle:Post')->findAll();

        return $this->render('post/index.html.twig', array(
            'posts' => $posts,
        ));
    }

How can you display exactly 5 records?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bears, 2018-02-12
@BurBurunduk

$em->getRepository('BlogBundle:Post')->findBy([condition], [sort => asc|desc], 5);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question