【问题标题】:getId returns empty string in Symfony2getId 在 Symfony2 中返回空字符串
【发布时间】: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;
}

我该如何解决这个问题?

【问题讨论】:

  • 您的对象是否已插入数据库?
  • 对象已插入,但每次重定向到查看页面时都会出现此错误。

标签: php symfony


【解决方案1】:

您的实体似乎没有保存在数据库中。你能查一下吗?

另外,你写的代码中似乎是一个错字“$form = $this->createCreateForm($entity);”,而不是 $this->createForm

否则我使用下面的代码(类似于你的)没有问题

/**
 * @Route("/new", name="item_new")
 * @Template()
 *
 * @return array
 */
public function newAction(Request $request)
{
    $em = $this->get('doctrine.orm.entity_manager');
    $item = new Item();

    $form = $this->createForm(new ItemType(), $item, array(
        'action' => $this->generateUrl('item_new'),
        'method' => 'POST',
    ));

    $form->handleRequest($request);
    if ($form->isValid()) {
        $em->persist($item);
        $em->flush();
        return $this->redirect($this->generateUrl('item_list'));
    }

    return array('form' => $form->createView());
}

【讨论】:

  • 这不是错字。我确实有这种方法。
【解决方案2】:

修复它。我继承了一个名为Post 的类,它有一个$id 变量。原来我忘了删除 Question 类中的 $id 变量,这就是为什么 Symfony 对返回的 id 感到困惑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 2020-07-11
    相关资源
    最近更新 更多