2017-12-15 8 views
3

私のsymfonyフォームは、Mailエンティティを表します。このエンティティは、Attachmentという別のエンティティと1対多のリレーションシップを持っています。したがって、MailTypeフォームは、そのAttachmentTypeフォームを埋め込むためCollectionTypeフィールドが含まれていますSymfony CollectionType:新しいエントリをマージする

$builder 
    ->add('attachments', CollectionType::class, [ 
     'entry_type' => AttachmentType::class, 
     'allow_add' => true, 
     'allow_delete' => false, 
     'by_reference' => false, 
    ]); 

私の見解は私のsymfonyのバックエンドに新しい添付ファイルを送信します。したがって、フォームデータをデータベースに保存する際には、メールの新しい添付ファイルを追加するだけで、既存の添付ファイルには触れないようにします。

残念ながら、symfonyの/ Doctrineは異なる挙動を示す:

existing attachments (in DB): [old1, old2, old3] 
new attachments (contained by HTTP request): [new1, new2] 
desired result in DB: [old1, old2, old3, new1, new2] 
actual result in DB: [new1, new2, old3] 

どのように私はこれを達成することができます:n添付ファイルはフォームデータに含まれている場合は、nまず既存の添付ファイルは、これらの新しい添付ファイルによって上書きされますか?私はby_reference => falseaddAttachmentメソッドを呼び出すと考えていたので、これもすぐに使えると思っていました。

マイMailエンティティコード:

class Mail { 
    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\Attachment", mappedBy="mail", cascade={"persist", "remove"}) 
    */ 
    protected $attachments; 

    ... 

    public function addAttachment(\AppBundle\Entity\ttachment $attachment) { 
     $attachment->setMail($this); 
     $this->attachments[] = $attachment; 
     return $this; 
    } 
} 

私のコントローラのコードフォームの処理:

// $mail = find mail in database 
    $form = $this->createForm(MailType::class, $mail); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $mail = $form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($mail); 
     $em->flush(); 
    } 
+0

あなたは追加のArrayCollectionの道を試みたことがありますか?次のようになります:$ this-> attachments-> add($ attachment); – ASOlivieri

+0

コンストラクタで$ attachmentsを新しいArrayCollection()としてインスタンス化していますか? – ASOlivieri

+0

symfonyが '$ this-> attachments-> add($ attachmetn)'を呼び出すのは ''by_reference' => false'をしません?はい、コンストラクタで$ attachmentsを新しいArrayCollectionとしてインスタンス化しました。申し訳ありませんが、上記を逃した – fishbone

答えて

0

あなたが欲しいものを行うにはいくつかの方法があります。あなたのメールエンティティにおけるその後

$builder 
     ->add('attachments', CollectionType::class, [ 
      'entry_type' => AttachmentType::class, 
      'allow_add' => true, 
      'allow_delete' => true, 
      'by_reference' => false, 
      'data' => [new Attachment()] // or add more or [] 
     ]); 

public function addAttachment(Attachment $attachment) { 
    $attachment->setMail($this); 
    $this->attachments[] = $attachment; 
    return $this; 
} 

public function removeAttachment(Attachment $attachment) { 
    return $this; 
} 

あなたには、いくつかの他の機能と、あなたのためremoveAttachmentを使用している場合、最も簡単なのは、あなたのフォームフィールドに空のデータまたは新しい添付ファイルの実体を与えることであろう実際にアタッチメントを削除するには、property_pathフォームフィールドの設定を利用することができます。

'property_path' => 'appendAttachments' 

addAppendAttachmentremoveAppendAttachment作成:

public function addAppendAttachment(Attachment $attachment) { 
    $attachment->setMail($this); 
    $this->attachments[] = $attachment; 
    return $this; 
} 

public function removeAppendAttachment(Attachment $attachment) { 
    return $this; 
} 
関連する問題