簡體   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