我正在为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")
好吧,我的猜测是,首先你没有调用你向我们显示的url,因为正如日志所说,调用showAction()
而不是getBySaloonAction()
,这与你的错误有关,是完全合理的。
如果您注意到日志错误,您将以某种方式将guest_search_by_saloon
(字符串)作为id
(整数)传递,并且数据库查询使用$id = 'guest_search_by_saloon'
运行,这将导致错误。