I made a web-application with symfony2, where a registrated user can query for a file and visualize it. I'm trying to pass the information from the controller to the template.
When I pass the information of a single object, it works properly. You can see the controller here:
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException(
'Nessun prodotto trovato per l\'id '.$id
);
}
return $this->render('AcmeGroundStationBundle::showdata.html.twig', array('Id' => $product->getId(), 'Name' => $product->getName(), 'UploadTime'=> $product- >getUploadTime()));
}
But what can I do if I want to display the whole list? If I change the
->find($id);
with
->findAll();
of course I get error.
( Call to a member function getId() on a non-object).
How can I display the whole list?
Thank you for your help
First pass all the products data to your twig view
public function showAction()
{
$products = $this->getDoctrine()
->getRepository('AcmeBundle:Product')
->findAll();
if (empty($products)) {
throw $this->createNotFoundException(
'No products found'
);
}
return $this->render('AcmeGroundStationBundle::showdata.html.twig',
array('products' => $products));
}
Then in your view you can list products with their information as
{% if products is defined and products is not empty %}
{% for p in products %}
id : {{ p.getId() }} <br>
Name: {{ p.getName() }} <br>
Upload Time: {{ p.getUploadTime() }} <br>
{% endfor %}
{% endif %}
EDIT
findAll()
will give all the results to get the latest 10 you need use findBy
findBy(
array(), // $where
array('id' => 'DESC'), // $orderBy
10, // $limit
0 // $offset
);