我正试图创建一个简单的页面,用Symfony+原则显示表的内容(customer:id,first_name,last_name)。
我用列和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>

和控制器将值从customer表传递到此视图。我在尝试类似的事情:
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>
    {% 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)
            );
        }
    }

10-01 02:30
查看更多