繁体   English   中英

Symfony表单集合

[英]Symfony form collection

我正在尝试在其他表单类型中添加formType。

客户类型:

class CustomerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstname', 'text', array(
                    'required' => 'required'
                ))
                ->add('middlename')
                ->add('lastname')
                ->add('email', 'email')
                ->add('groups', 'entity', array(
                    'class' => 'MV\CMSBundle\Entity\Group',
                    'property' => 'name',
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('g')
                                  ->orderBy('g.name', 'ASC');
                    }
                ))
                ->add('profile', 'collection', array(
                    'type' => new ProfileType()
                ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MV\CMSBundle\Entity\User',
        ));
    }

    public function getName()
    {
        return 'customer';
    }
}

ProfileType:

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('isActive', 'checkbox', array(
                    'required' => false
                ))
                ->add('phone')
                ->add('address')
                ->add('city')
                ->add('zipcode')
                ->add('country')
                ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MV\NameBundle\Entity\Profile',
        ));
    }

    public function getName()
    {
        return 'profile';
    }
}

然后我收到这条消息:

Expected argument of type "array or (\Traversable and \ArrayAccess)", "MV\NameBundle\Entity\Profile" given.

如果我评论那条线我得到:(只是为了检查会出错的地方;))

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class MV\NameBundle\Entity\Profile. You can avoid this error by setting the "data_class" option to "MV\NameBundle\Entity\Profile" or by adding a view transformer that transforms an instance of class MV\NameBundle\Entity\Profile to scalar, array or an instance of \ArrayAccess. 

我做错了什么?

Symfony2:2.1.8-DEV

目前尚不清楚您的Customer实体与您的Profile实体之间的映射是什么,但我猜有两种选择:


选项1:您具有OneToOne关系(一个客户只能拥有一个配置文件)。 因此,您根本不需要集合,只需要嵌入单个对象

->add('profile', new ProfileType());

选项2:您需要ManyToOne关系(一个客户可以拥有多个配置文件)。 在这种情况下,您需要使用嵌入表单集合 如果这是您想要的,请将$profile重命名$profile Customer实体中的$profiles ,并执行以下操作:

   //MV\CMSBundle\Entity\Customer  
   use Doctrine\Common\Collections\ArrayCollection;
   /**
   * Your mapping here. 
   * 
   * @ORM\ManyToOne(targetEntity="MV\NameBundle\Entity\Profile")
   */
   protected $profiles;

    public function __construct()
    {
         //This is why you had an error, this is missing from your entity
         //An object was sent instead of a collection.
         $this->profiles = new ArrayCollection();
    }

最后,更新您的数据库。

暂无
暂无

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

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