繁体   English   中英

在Symfony 2表单上找不到实体

[英]Entity not found on Symfony 2 form

我使用smyfony2 cli工具为实体生成了CRUD。 现在,我想使用CRUD中的编辑功能。

一,代码

/**
 * Displays a form to edit an existing Poi entity.
 *
 * @Route("/poi/{id}/edit", name="poi_edit", requirements={"id" = "\d+"})
 * @Method("GET")
 * @Template()
 */
public function editAction($id){
    $em = $this->getDoctrine()->getManager('default')->getRepository('Project\Bundle\ProjectBundle\Entity\Poi');

    $entity = $em->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Poi entity.');
    }

    $editForm = $this->createEditForm($entity);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView()
    );
}

/**
* Creates a form to edit a Poi entity.
*
* @param Poi $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Poi $entity)
{
    $form = $this->createForm(new PoiType(), $entity, array(
        'action' => $this->generateUrl('poi_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

但是我收到“找不到实体”错误。 当然,我首先想到的是NotFoundException的抛出,因此我将其注释掉,但仍然收到错误。

然后我调试了代码,将找到实体。 它也存在于数据库中。

我进行了进一步调试,发现错误在Symfony2 Form堆栈中的某个地方引发,该堆栈生成该窗体,该窗体在createEditForm操作中调用。

我明显想念什么吗? 我怎么了

我的Symfony2开发日志也只说抛出了NotFoundException ,找不到更多信息。

我还有一个带有生成的CRUD的实体,但是由于某些不应显示的字段,我自己在createEditForm构建了表单。 我需要自己创建表格还是做错明显的事情?

也是PoiType代码

class PoiType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('date')
            ->add('situation')
            ->add('description')
            ->add('isPrivate')
            ->add('image')
            ->add('audio')
            ->add('country')
            ->add('category')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Project\Bundle\ProjectBundle\Entity\Poi'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'project_bundle_projectbundle_poi';
    }
}

这是我得到的错误:

CRITICAL - Uncaught PHP Exception Doctrine\\ORM\\EntityNotFoundException: "Entity was not found." at /var/www/project-symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Proxy/ProxyFactory.php line 177

尝试在FormType中的data_class中添加反斜杠前缀,如下所示:

'data_class' => '\Project\Bundle\ProjectBundle\Entity\Poi',

谢谢你的这篇文章

暂无
暂无

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

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