我正在为symfony 2框架工作,我在Controller中调用自定义Action时遇到了问题。 我已将我的应用程序连接到数据库(在xampp上),并且对于每个表我都有Entity类。现在我为这个实体之一(GuestController.php)生成一个CRUD控制器(带有教义),我需要在其中创建一个新的Action,然后调用它。 这是行动代码:
/**
*
* @Route("/", name="my_action")
*
*/
public function customAction() {
return new Response('<html><body>Hello</body></html>');
}
现在,如果我尝试使用此链接调用它
http://localhost/TEST/web/app_dev.php/guest/guest_search_by_saloon
我获得了
Unable to find Guest entity.
出了什么问题?
这是完整的控制器类
/**
* Guest controller.
*
* @Route("/guest")
*/
class GuestController extends Controller {
/**
* Lists all Guest entities.
*
* @Route("/", name="guest")
* @Method("GET")
* @Template()
*/
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('mainDbBundle:Guest')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new Guest entity.
*
* @Route("/", name="guest_create")
* @Method("POST")
* @Template("mainDbBundle:Guest:new.html.twig")
*/
public function createAction(Request $request) {
$entity = new Guest();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('guest_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Guest entity.
*
* @param Guest $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Guest $entity) {
$form = $this->createForm(new GuestType(), $entity, array(
'action' => $this->generateUrl('guest_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Guest entity.
*
* @Route("/new", name="guest_new")
* @Method("GET")
* @Template()
*/
public function newAction() {
$entity = new Guest();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a Guest entity.
*
* @Route("/{id}", name="guest_show")
* @Method("GET")
* @Template()
*/
public function showAction($id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('mainDbBundle:Guest')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Guest entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing Guest entity.
*
* @Route("/{id}/edit", name="guest_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('mainDbBundle:Guest')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Guest 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 Guest entity.
*
* @param Guest $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Guest $entity) {
$form = $this->createForm(new GuestType(), $entity, array(
'action' => $this->generateUrl('guest_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Guest entity.
*
* @Route("/{id}", name="guest_update")
* @Method("PUT")
* @Template("mainDbBundle:Guest:edit.html.twig")
*/
public function updateAction(Request $request, $id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('mainDbBundle:Guest')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Guest entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('guest_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Guest entity.
*
* @Route("/{id}", name="guest_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('mainDbBundle:Guest')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Guest entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('guest'));
}
/**
* Creates a form to delete a Guest 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('guest_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
/**
*
* @Route("/", name="guest_search_by_saloon")
*
*/
public function getBySaloonAction() {
return new Response('<html><body>Ciao !</body></html>');
}
}
这就是日志所说的:
NFO - Matched route "guest_show" (parameters: "_controller": "main\dbBundle\Controller\GuestController::showAction", "id": "guest_search_by_saloon", "_route": "guest_show")
答案 0 :(得分:0)
好的,我的猜测是,首先你没有给url打电话给你,因为正如日志所说的那样,showAction()
被调用而不是getBySaloonAction()
而这与你的错误相关,是完全明智的。
如果您注意到日志错误,则以某种方式将guest_search_by_saloon
(字符串)作为id
(整数)传递,并使用$id = 'guest_search_by_saloon'
运行db查询,这会导致错误。< / p>