【问题标题】:Symfony form collectionSymfony 表单集合
【发布时间】:2013-02-04 00:18:14
【问题描述】:

我正在尝试在其他表单类型中添加一个 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';
    }
}

个人资料类型:

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

【问题讨论】:

  • 向我们展示 MV\CMSBundle\Entity\User 类。似乎 MV\CMSBundle\Entity\User::getProfile 返回对象而不是集合...

标签: php symfony doctrine entity symfony-2.1


【解决方案1】:

不清楚您的Customer 实体和Profile 实体之间的映射是什么,但我想有两个选项:


选项 1:您拥有 OneToOne 关系(一个客户只能拥有一个个人资料)。因此,您根本不需要收藏,只需要embed a single object。

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

选项 2:您需要多对一关系(一个客户可以有多个个人资料)。在这种情况下,您需要使用embed a collection of forms。如果这是您想要的,请在您的客户实体中将 $profile 重命名为 $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();
    }

最后,更新你的数据库。

【讨论】:

  • 你先生是个传奇!
  • 你太棒了。当我需要的只是单个对象的简单嵌入时,我试图使用表单集合。
  • 我很久以前就发现了这个,并意识到我已经投了赞成票。我重新找到了它,并希望我能再次投票。这是一个虚拟的吻。 x
  • 我不得不回到我在映射关系方面遇到的类似问题,并再次看到这篇文章,因为它帮助我在过去 6 个月中解决了两个不同的问题。谢谢...再次!
猜你喜欢
  • 1970-01-01
  • 2015-08-04
  • 2015-09-02
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
相关资源
最近更新 更多