2017-08-03 19 views
0

私はArticleTagsエンティティを持っています。 Articleと関連するTagsを編集するフォームを作成したいと思います。 (他にもたくさんの関係がありますが、これは重要ではありません) 私の場合、新しいタグを追加することは効果的です。しかし、私はいくつかのタグを削除したい場合、または既存の編集、これは起こらないと私はエラーを取得していないよ。私が知っているように$em->flush()はこれをすべて解決する必要がありますが、そうではありません。 また、私は自分のエンジンをチェックしました。それはInnoDBです。私はすでにForeign Keysを作成しています。Symfony2フォームが正常に動作しません(関連oneToManyエンティティは削除されません)

編集用にはTextType::classを使用しています。だから私はタグのいずれかを削除する場合は、このリストから削除する必要があります。 click to see tag input

フォーム要素

$builder->add($builder->create('custom_tags', TextType::class, [])->addModelTransformer(function(){/*to array*/}, function(){/*to string*/}); 

ドキュメントが言うように、私は、このエンティティをタイアップ:

条エンティティ:

/** 
    * Article entity 
    * 
    * @ORM\Table(name="Article") 
    * @ORM\Entity 
    */ 
    class Article 
    { 
     /** 
     * Article can have zero or more tags 
     * 
     * @OneToMany(targetEntity="Tag", mappedBy="article", 
     *  cascade={"persist", "remove"}, orphanRemoval=true) 
     */ 
     private $tags; 

     public function __construct() { 
      $this->tags = new ArrayCollection(); 
     } 

     public function setTags($tags) { 
      $currTags = $this->getTags(); 
      .... 
      // calculating the difference between currTags and ModifiedTags 
      //here I got ArrayCollection with new tags entities list, without already deleted tags 
      $this->tags = $tags; 
      return $this; 
     } 

     public function getTags() { 
      return $this->tags->matching(Criteria::create()->orderBy(["id" => Criteria::DESC]); 
     } 
     ... 

タグエンティティ

/** 
* Tag 
* 
* @ORM\Table(name="Tag") 
* @ORM\Entity 
*/ 
class Tag 
{ 
/** 
    * @var Article 
    * 
    * Many Tags can be present in one article. 
    * 
    * @ManyToOne(targetEntity="Article", inversedBy="tags") 
    * @JoinColumn(name="article_id", referencedColumnName="id") 
    */ 
    private $article; 

    ... 

私は誰もがいくつかのアドバイスを与えることができる ...情報を見つけるために、この問題を解決するために多くの時間を費やし、私は逃したか、いくつかのリンクを与えているものを私に示して? remove_actionを手動で実現するか、このアクションを明示的に指定する必要がありますか?ありがとう!

+0

'setArticle($ article)'関数を 'Tag'エンティティに追加しましたか? – DrKey

+0

あなたのdoctrine設定ファイルで "on cascade delete"を使うべきでしょうか? – Mz1907

+0

@ Mintho433、どういう意味ですか?どこで設定すればいいですか? 私の言うことはしませんでしたが、私のフォームで記事フィールドを編集すると、タグレコードはテーブルから削除されます... –

答えて

0

getTags()関数がArrayCollectionを返すことに気付きました。 これは問題ありません。しかし、curTagとModifiedTagsの違いを計算するためにsetTags()のgetTags()も呼びます。 私はArrayCollectionインスタンスを変更していました。 しかし、ときに私がArrayCollectionのは、エンティティマネージャ によって持続し、管理されたら、それはそれが正確にどのインスタンス のArrayCollection (got from here???)

を持って振る舞うPersistentCollectionなり

ます$ this-を使用>タグになりましたDoctrine \ ORM \ PersistentCollectionの代わりに、Articleクラスの$ this-> getTags() - すべてうまく開始されました。 これで、Doctrine \ ORM \ PersistentCollectionとArrayCollectionの違いについてお読みください。

関連する問題