我有索引操作,打印1)表单用于创建新实体; 2)所有实体的清单:
原文链接:https://www.f2er.com/php/137341.htmlpublic function indexAction() { $em = $this->getDoctrine()->getEntityManager(); $entities = $em->getRepository('MyBundle:Entity')->findAll(); return array( 'entities' => $entities,); }
枝条:
{% block content %} {% render "MyBundle:Entity:new" %} {% render "MyBundle:Entity:list" %} {% endblock %}
实体控制器中的newAction是标准形式的控制器:
public function newAction() { $entity = new Entity(); $form = $this->createForm(new EntityType(),$entity); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $form->bindRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($entity); $em->flush(); // NOT WORK return $this->redirect($this->generateUrl('entity_show',array('id' => $entity->getId()))); } } return array( 'form' => $form->createView(),); }
An exception has been thrown during the rendering of a template ("Error when rendering "http://example.com/app_dev.PHP/url/" (Status code is 302).") in MyBundle:Default:index.html.twig at line 2.