使用具有manyToMany关系的持久化实体作为数据创建symfony表单时出现问题

时间:2014-05-04 20:44:46

标签: forms symfony

我有两个实体,我已经为其构建了表单类型。

class UserType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('createdAt')
            ->add('updatedAt')
            ->add('username')
            ->add('email')
            ->add('plainPassword')
            ->add('firstName')
            ->add('lastName')
            ->add('phoneNumber')
            ->add('address')
            ->add(
                'roles',
                'collection',
                array(
                    'type' => new UserRoleType(),
                    'data_class' => 'Mrm\AuthBundle\Entity\UserRole',
                    'required'     => false,
                    'allow_add'    => true,
                    'allow_delete' => true,
                    'by_reference' => false
                )
            )
        ;
    }

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

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

并且

class UserRoleType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('createdAt')
            ->add('updatedAt')
            ->add('name')
            ->add('description')
            ->add('label')
        ;
    }

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

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

正如您可能猜到的那样,User和UserRole之间存在ManyToMany关系。

如果我尝试提交新数据,一切都很完美。创建User和UserRole实体以及ManyToMany关系共同条目。

$manager = $this->get("doctrine.orm.entity_manager");

$user = new User();

$form = $this->createForm(new UserType(), $user);

$form->handleRequest($request);

if ($form->isValid()) {
    $manager->persist($user);
    $manager->flush();
} else {
    return View::create($form, 400);
}

但是当我尝试实现某些逻辑来支持更新时,就像这样(注意代码是用于测试目的而id是硬编码的。)

$manager = $this->get("doctrine.orm.entity_manager");

$repo = $manager->getRepository("MrmAuthBundle:User");    

$user = $repo->find(6);

$form = $this->createForm(new UserType(), $user);

$form->handleRequest($request);

if ($form->isValid()) {
    $manager->persist($user);
    $manager->flush();
} else {
    return View::create($form, 400);
}

" **The form's view data is expected to be an instance of class Mrm\AuthBundle\Entity\UserRole, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Mrm\AuthBundle\Entity\UserRole.**"错误。

我做错了什么,如何解决?

0 个答案:

没有答案