大家好我有些路线有问题。我有一个楼层有地板,地板有公寓。因此,我建造了有关公寓的路线,并列出了对象建筑物和楼层。
<?php
namespace George\ApartmentBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use George\ApartmentBundle\Entity\Apartment;
use George\ApartmentBundle\Form\ApartmentType;
/**
* Apartment controller.
*
* @Route("/admin/apartment")
*/
class ApartmentController extends Controller
{
/**
* Lists all Apartment entities.
*
* @Route("/{object}/{floor}", name="admin_apartment")
* @Method("GET")
* @Template()
*/
public function indexAction($object, $floor)
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('ApartmentBundle:Apartment')->findBy(array('object'=>$object,'floor'=>$floor));
return array(
'entities' => $entities,
'object' =>$object,
'floor' => $floor
);
}
/**
* Creates a new Apartment entity.
*
* @Route("/", name="admin_apartment_create")
* @Method("POST")
* @Template("ApartmentBundle:Apartment:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new Apartment();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
// echo '------------';
// \Doctrine\Common\Util\Debug::dump($entity);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('admin_apartment_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Apartment entity.
*
* @param Apartment $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Apartment $entity)
{
$manager = $this->getDoctrine()->getManager();
$form = $this->createForm(new ApartmentType($manager), $entity, array(
'action' => $this->generateUrl('admin_apartment_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Apartment entity.
*
* @Route("/new/{object}/{floor}", name="admin_apartment_new")
* @Method("GET")
* @Template()
*/
public function newAction($object, $floor)
{
$em = $this->getDoctrine()->getManager();
$entityObj = $em->getRepository('ObjectsBundle:Object')->find($object);
$entityFloor = $em->getRepository('FloorBundle:Floor')->find($floor);
$entity = new Apartment();
$entity->setObject($entityObj);
$entity->setFloor($entityFloor);
// echo 'uuuuuuuuuuuu';
// \Doctrine\Common\Util\Debug::dump($entity);
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
'floor' => $entityFloor->getId(),
'object' => $entityObj->getId()
);
}
/**
* Finds and displays a Apartment entity.
*
* @Route("/{id}", name="admin_apartment_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApartmentBundle:Apartment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Apartment entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Apartment entity.
*
* @Route("/edit/{id}", name="admin_apartment_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApartmentBundle:Apartment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Apartment entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a Apartment entity.
*
* @param Apartment $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Apartment $entity)
{
$form = $this->createForm(new ApartmentType(), $entity, array(
'action' => $this->generateUrl('admin_apartment_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Apartment entity.
*
* @Route("/{id}", name="admin_apartment_update")
* @Method("PUT")
* @Template("ApartmentBundle:Apartment:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApartmentBundle:Apartment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Apartment entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('admin_apartment_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Apartment entity.
*
* @Route("/{id}", name="admin_apartment_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('ApartmentBundle:Apartment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Apartment entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('admin_apartment'));
}
/**
* Creates a form to delete a Apartment entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_apartment_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}
所以看看公寓索引方法indexAction。在那里我得到了url对象的id(对象构建的id)和floor(地板的id),然后得到了所有的公寓。
$entities = $em->getRepository('ApartmentBundle:Apartment')->findBy(array('object'=>$object,'floor'=>$floor));
出于某些原因,我不明白为什么当您尝试使用生成的url editAction($ id)打开编辑操作时,它会尝试再次打开索引。
我知道他们确实按参数匹配,请参阅示例:
http://localhost:8000/admin/apartment/59/10 http://localhost:8000/admin/apartment/edit/1
最佳解决方案是什么?我需要在编辑中输入更多参数或?
先谢谢你们!
答案 0 :(得分:1)
您可以在索引之前将其移到简单解决方案的上方。或者您可以添加要求。请参阅http://symfony.com/doc/current/book/routing.html
中的详情