我正在尝试创建简单的页面来显示表格的内容(客户:id,first_name,last_name)与Symfony +学说。我创建了带有列和getter/setter的GS\OrderBundle\Entity\Customer 。为其创建了路由。我想创建一个视图,如下所示:
<table>
{% for c in form %}
{% set id = c.get('value') %}
<tr>
<td>{{ form_widget(c) }}</td>
<td>{{ c[id].firstName }}</td>
<td>{{ c[id].lastName }}</td>
</tr>
{% endfor %}
</table>
和控制器将值从客户表传递到此视图。我正在尝试类似的东西:
namespace GSOrderBundleController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyBundleFrameworkBundleControllerController;
use GSOrderBundleEntityCustomer;
use DoctrineORMEntityManager;
class CustomerListController extends Controller
{
public function listAction()
{
$repository = $this->getDoctrine()
->getRepository('GSOrderBundle:Customer');
$customer = $repository->findAll();
$form = $this->createFormBuilder($customer)->getForm();
return $this->render(
'customerTable.html.twig',
array('form' => $form->createView())
);
return new Response($names);
}
}
在symfony网站上,我只找到了设置数据或输出单行数据的示例。您能否提供任何如何在视图中显示表格内容的简单示例?
您的代码存在多个问题:
- 两个返回语句
- 从一组客户而不是单个客户创建表单实体
- 不使用在模板中设置的变量名称
以下是您的视图可能的外观:
<table>
{% for c in customers %}
<tr>
<td>{{ c.id }}</td>
<td>{{ c.firstName }}</td>
<td>{{ c.lastName }}</td>
</tr>
{% endfor %}
</table>
这是一个应该为您解决问题的控制器:
class CustomerListController extends Controller
{
public function listAction()
{
$repository = $this->getDoctrine()
->getRepository('GSOrderBundle:Customer');
$customers = $repository->findAll();
return $this->render(
'customerTable.html.twig',
array('customers' => $customers)
);
}
}