Symfony2:表单的视图数据应该是实体的实例但是是数组

Symfony2: The form's view data is expected to be an instance of entity but is array

有两个操作(新建和编辑)使用相同的表单。该表单应创建一个新实体 (MyBundle\Entity\Project) 或编辑现有实体。每个项目可以分配给一个团队(MyBundle\Entity\Team),一个团队可以持有多个项目。

Entity/Project.php:

/* ... */
/**
     * @var Team
     *
     * @ORM\ManyToOne(targetEntity="Team", inversedBy="projects")
     * @ORM\JoinColumn(name="team", referencedColumnName="id")
     */
    private $team;
/* ... */

Entity/Team.php:

/* ... */
/**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Project", mappedBy="team")
     * @ORM\OrderBy({"title" = "ASC"})
     **/
    private $projects;
/* ... */

Controller/ProjectController.php:

public function newAction()
    {
        $project = new Project();
        $form = $this->createForm(
            new new ProjectType($project),
            array(
                'action' => $this->generateUrl('mybundle_project_create'),
            )
        );

        /* ... */
    }

public function editAction($id, Request $request)
    {
        /** @var \Doctrine\ORM\EntityManager $em */
        $em = $this->getDoctrine()->getManager();
        $project = $em->getRepository('MyBundle:Project')->findOneById($id);

        $form = $this->createForm(
            new ProjectType($project),
            $project,
            array(
                'action' => $this->generateUrl('mybundle_project_edit', array('id' => $project->getId())),
            )
        );

Form/ProjectType.php:

private $project;

    public function __construct($project = null)
    {
        $this->project = $project;
    }

/**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            /* ... */
            ->add('team', 'entity', array(
                'class' => MyBundle:Team',
                'property' => 'name',
                'label' => 'Team',
                'query_builder' => function (TeamRepository $er) {
                    return $er->createQueryBuilder('e')
                        ->orderBy('e.name', 'ASC');
                },
                'expanded' => false,
                'multiple' => false,
                'required' => false
            ))
            ->add('save', 'submit');
    }

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

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

当我访问编辑表单时,下拉列表正确显示了分配的团队。但是当我尝试创建一个新项目时,它给了我以下错误信息:

The form's view data is expected to be an instance of class MyBundle\Entity\Project, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of MyBundle\Entity\Project.

ProjectController 的 newAction 正在使用 2 个参数而不是 3 个参数调用方法 createForm,它应该是

$form = $this->createForm(
    new ProjectType($project),
    $project,
    array(
        'action' => $this->generateUrl('mybundle_project_create'),
    )
);

而不是

$form = $this->createForm(
    new ProjectType($this->getDoctrine()->getEntityManager(), $project),
    array(
        'action' => $this->generateUrl('mybundle_project_create'),
    )
);