【发布时间】:2014-03-30 16:56:08
【问题描述】:
我为我的问题对象创建了一个 CRUD。测试后出现错误。
似乎 getId() 正在返回某种空字符串。
自动生成的 CRUD 的默认行为是在成功创建实体后将用户重定向到视图页面。但是在这种情况下,它会返回一个错误
"路由 "question_show" 的参数 "id" 必须匹配 "[^/]++" ("" given) 以生成对应的 URL。"
这是我的控制器代码:
/**
* Creates a new Question entity.
*
* @Route("/ask", name="question_create")
* @Method("POST")
* @Template("VerySoftAskMeBundle:Question:ask.html.twig")
*/
public function createAction(Request $request) {
$entity = new Question();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('question_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
这是查看操作:
/**
* Finds and displays a Question entity.
*
* @Route("/{id}", name="question_show")
* @Method("GET")
* @Template()
*/
public function showAction($id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VerySoftAskMeBundle:Question')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Question entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to create a Question entity.
*
* @param Question $entity The entity
*
* @return Form The form
*/
private function createCreateForm(Question $entity) {
$form = $this->createForm(new QuestionType(), $entity, array(
'action' => $this->generateUrl('question_create'),
'method' => 'POST',
'em' => $this->getDoctrine()->getEntityManager()
));
$form->add('submit', 'submit', array('label' => 'Ask'));
return $form;
}
我该如何解决这个问题?
【问题讨论】:
-
您的对象是否已插入数据库?
-
对象已插入,但每次重定向到查看页面时都会出现此错误。