使用 Symfony 和 Doctrine 创建小页面以显示 table 的内容

Create small page to display content of the table with Symfony and Doctrine

我正在尝试使用 Symfony + 学说创建简单的页面来显示 table(客户:id、first_name、last_name)的内容。 我用列和 getters/setters 创建了 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>

和 Controller 将来自客户 table 的值传递给此视图。我正在尝试类似的东西:

namespace GS\OrderBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use GS\OrderBundle\Entity\Customer;
use Doctrine\ORM\EntityManager;

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 的内容?

您的代码有几个问题:

  1. 两个 return 语句
  2. 从一组客户而不是单个客户创建表单 实体
  3. 不使用您在模板中设置的变量名称

您的视图可能如下所示:

    <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)
            );
        }
    }