我使用symfony2制作了一个Web应用程序,在该应用程序中,注册用户可以查询文件并将其可视化。我正在尝试将信息从控制器传递到模板。
当我传递单个对象的信息时,它可以正常工作。您可以在此处查看控制器:
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()));
}
但是,如果我想显示整个列表怎么办?如果我改变
->find($id);
和
->findAll();
我当然会报错。
( Call to a member function getId() on a non-object).
如何显示整个列表?
谢谢您的帮助
首先将所有产品数据传递到您的树枝视图
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));
}
然后,您可以在视图中列出产品及其信息,如下所示:
{% 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 %}
编辑
findAll()
将给出所有结果以获得您需要使用的最新10 findBy
findBy(
array(), // $where
array('id' => 'DESC'), // $orderBy
10, // $limit
0 // $offset
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句