7

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

4

1 に答える 1

13

Customerエンティティとエンティティの間のマッピングが何であるかは明確ではありませんがProfile、2 つのオプションがあると思います。


オプション 1: 1 対 1の関係があります (1 人の顧客が持つことができるプロファイルは 1 つだけです)。したがって、コレクションはまったく必要ありませんが、単一のオブジェクトを埋め込むだけで済みます。

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

オプション 2: ManyToOne の関係が必要です (1 人の顧客が複数のプロファイルを持つことができます)。その場合、フォームのコレクションを埋め込むために使用する必要があります。これが必要な場合は、Customer エンティティで名前$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();
    }

最後に、データベースを更新します。

于 2013-02-03T15:39:58.837 に答える