简体   繁体   English

getId在Symfony2中返回空字符串

[英]getId returns empty string in Symfony2

I have made a CRUD for my question object. 我为我的问题对象做了一个CRUD。 An error occurred once I tested it. 一旦测试,就会发生错误。

It seems that the getId() is returning an empty string of some sort. 似乎getId()返回某种空字符串。

The default behavior of the autogenerated CRUD is to redirect the user to the view page after successfully creating the entity. 自动生成的CRUD的默认行为是在成功创建实体后将用户重定向到视图页面。 But in this case, it returns an error 但是在这种情况下,它将返回错误

" Parameter "id" for route "question_show" must match "[^/]++" ("" given) to generate a corresponding URL. " 路线“ question_show ”的参数“ id”必须与“ [^ /] ++”(给定的“”“相匹配,以生成相应的URL。

Here is my controller code: 这是我的控制器代码:

/**
 * 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(),
    );
}

Here is the view action: 这是查看操作:

/**
 * 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;
}

How can I fix this? 我怎样才能解决这个问题?

It seems that your entity is not persisted in the database. 您的实体似乎未持久存储在数据库中。 Could you check it? 你能检查一下吗?

Also, it seems to be a typo in your code your wrote "$form = $this->createCreateForm($entity);", instead of $this->createForm 另外,您写的“ $ form = $ this-> createCreateForm($ entity);”而不是$ this-> createForm在您的代码中似乎是一个错字

Otherwise I have used the code below (similar to yours) with no problem 否则我会毫无问题地使用下面的代码(类似于您的代码)

/**
 * @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());
}

Fixed it. 修复。 I was inheriting a class named Post which has an $id variable. 我正在继承一个名为Post的类,该类具有$id变量。 Turns out that I forgot to delete the $id variable in my Question class which is why Symfony gets confused on what id to return. 原来我忘了删除我的Question类中的$id变量,这就是为什么Symfony对返回的id感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM