2017-06-09 14 views
3

私は3つのテーブル/エンティティを持っています。私は1つの形式で3つのテーブルすべてにデータを保持するフォームを作成しようとしています。エンティティは長いので、私はここに追加していません。Symfonyフォームの複数のテーブルを埋め込む

エンティティクライアント他の2つのエンティティに対してクライアントエンティティで配列コレクションを作成しました。

protected $Clientservice; 
    protected $Hostingaccount; 

    public function __construct() 
    { 
     $this->Clientservice = new ArrayCollection(); 
     $this->Hostingaccount= new ArrayCollection(); 
    } 
    public function getClientservice() 
    { 
     return $this->Clientservice; 
    } 
    public function getHostingaccount() 
{ 
    return $this->Hostingaccount; 
} 

    public function setClientservice($Clientservice) 
    { 
     $this->Clientservice = $Clientservice; 

     return $this; 
    } 
    public function setHostingaccount($Hostingaccount) 
    { 
     $this->Hostingaccount = $Hostingaccount; 

     return $this; 
    } 

そして、私は3つのフォーム有無:

:私はこのようなコードを持って私のコントローラで

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('Domain',TextType::class) 
     ->add('Domainip',IntegerType::class) 
     ->add('UserName',TextType::class) 
     ->add('Owner',TextType::class); 

} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => Hostingaccount::class 
    )); 
} 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('CustomerID',IntegerType::class) 
     ->add('Sex',TextType::class) 
     ->add('Name',TextType::class) 
     ->add('FirstName',TextType::class) 
     ->add('LastName',TextType::class) 
     ->add('Email', EmailType::class) 
     ->add('Invoiceemail', EmailType::class) 
     ->add('Iban', TextType::class) 
     ->add('Bic', TextType::class) 
     ->add('SEPAMandateID', TextType::class) 
     ->add('LogoUrl', TextType::class) 
     ->add('CreationDate', DateType::class) 
     ->add('Clientservice', CollectionType::class, array(
      'entry_type' => WhmAdminClientserviceType::class 
     )) 
     ->add('Hostingaccount', CollectionType::class, array(
      'entry_type' => WhmAdminHostingAccountType::class 
     )) 
     ->getForm(); 

} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => Client::class 
    )); 
} 

Clientserviceフォーム

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('Description',TextType::class); 

} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => Clientservice::class 
    )); 
} 

Hostingaccountフォームを

public function AddWhmAdminAction(Request $request) 
{ 

    $Client = new Client(); 

    $form = $this->createForm(WhmAdminType::class, $Client); 
    $form->add('submit', SubmitType::class, array(
     'label' => 'Create', 
     'attr' => array('class' => 'btn btn-default pull-left') 
    )); 
    $form->handleRequest($request); 
    if ($form->isSubmitted() && $form->isValid()) { 

     $em = $this->getDoctrine()->getManager(); 

     $em->persist($Client); 
     $em->flush(); 

     $this->get('session')->getFlashBag()->add('green', 'Product created!'); 
     return $this->redirectToRoute('whmAdmin'); 
     // flash msg 
    } 
    //return $form->createView(); 
    $build['form'] = $form->createView(); 
    return $this->render('whm/WhmAdminForm.html.twig', $build); 

} 

と私の見解で

{{ form_start(form) }} 

{% for flashMessage in app.session.flashbag.get('green') %} 

    {{ flashMessage }} 

{% endfor %} 


{{ form_end(form) }} 

私はhttp://symfony.com/doc/current/form/form_collections.htmlを踏襲し、これは私が得たどのくらいです。 私のフォームは、クライアントからのフィールドのみを表示します。 Clientserviceとhostingaccountのフィールドは表示されません。 clientserviceとhostingaccountのフィールドをClientと同じ形式で表示するにはどうすればよいですか。

答えて

2

ClientserviceHostingaccountはOneToOneまたはManyToOneの関係にある場合は、変更:

->add('Clientservice', CollectionType::class, array(
    'entry_type' => WhmAdminClientserviceType::class 
)) 
->add('Hostingaccount', CollectionType::class, array(
    'entry_type' => WhmAdminHostingAccountType::class 
)) 

をする:

->add('Clientservice', WhmAdminClientserviceType::class) 
->add('Hostingaccount', WhmAdminHostingAccountType::class) 

そうでない場合は、フォームビルダーを残し、コレクション型では約adding and removing itemsをお読みください。

+0

このエラーが発生します。フォームのビューデータはAppBundle \ Entity \ Clientserviceクラスのインスタンスであることが予想されますが、Doctrine \ Common \ Collections \ ArrayCollectionクラスのインスタンスです。このエラーは、 "data_class"オプションをnullに設定するか、Doctrine \ Common \ Collections \ ArrayCollectionクラスのインスタンスをAppBundle \ Entity \ Clientserviceのインスタンスに変換するビュートランスフォーマーを追加することで回避できます。しかし、私が "data_class"をnullに変更したとき、それは他のフィールドを示します。私がデータを保持するとき、クライアントにデータを保存するのではなく、クライアントのサービスにのみアクセスします。 – user8124685

+0

'Client'に対する' Clientservice'と 'Hostingaccount' ManyToOneの関係はありますか? – miikes

+0

私はこのようにしています/ ** * @ORM \ ManyToMany(targetEntity = "AppBundle \ Entity \ Clientservice"、カスケード= {"持続"}) */ 保護された$ Clientservice; /** * @ORM \ ManyToMany(targetEntity = "AppBundle \ Entity \ Hostingaccount"、カスケード= {"持続"}) */ protected $ Hostingaccount;内部クライアントエンティティ – user8124685

関連する問題