【发布时间】:2013-10-27 01:32:29
【问题描述】:
我正在尝试显示数据库中的数据。
这是我的路线:
pages:
pattern: /pages/{id}
defaults:
_controller: DprocMainBundle:Index:show
这是该路线的方法:
public function showAction($id)
{
$page = $this->getDoctrine()
->getRepository('DprocMainBundle:Pages')
->find($id);
if (!$page) {
throw $this->createNotFoundException('No product found for id '.$id);
}
return $this->render('DprocMainBundle:Dproc:single.html.twig',array('pages' => $page));
}
print_r($page) 显示:
Dproc\MainBundle\Entity\Pages Object
(
[Id:protected] => 1
[page_title:protected] => A Foo Bar
[page_content:protected] => Test content for page
[page_category:protected] => 3dsmax
)
在 single.html.twig 中我试图显示该信息:
{% for page in pages %}
{{ page.page_title }}
{% endfor %}
什么都没显示,我做错了什么?
【问题讨论】:
-
可以迭代单个结果吗?为什么不 {{pages.page_title}} 没有'for'?
-
当然,但是 - DprocMainBundle:Dproc:single.html.twig 中不存在对象“Dproc\MainBundle\Entity\Pages”的方法“page_title”
-
我是通过 getter 方法完成的 {{ page.getPageTitle }}