在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;
}

但是我收到一个"Entity not found"错误。当然,我首先想到的是NotFoundException的抛出,所以我把它注释掉了,但我仍然得到了错误。

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

我进一步调试并发现错误是在生成表单的Symfony2表单堆栈中抛出的,该表单在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',