代码之家  ›  专栏  ›  技术社区  ›  young B

Symfony2表单工作不好(未删除相关oneToMany实体)

  •  0
  • young B  · 技术社区  · 8 年前

    我有一个 Article Tags 我想创建要编辑的表单 文章 标签 (还有很多其他关系,但目前这并不重要) 在我的例子中,添加新标签非常有效。但如果我想删除一些标签,或编辑现有的,这不会发生,我不会得到一个错误。据我所知 $em->flush() 我还检查了我的引擎——它是InnoDB。我已经创建了外键。

    用于编辑我使用的标记 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\u操作还是显式指定此操作?谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   young B    8 年前

    我注意到getTags()函数返回ArrayCollection。 所以我在ArrayCollection实例中进行了更改。 但当我开始使用$this时->标签

    一旦ArrayCollection被实体管理器持久化和管理 它成为一个PersistentCollection,其行为完全符合 阵列集合 (got from here???)

    哪个Doctor\ORM\PersistentCollection实例代替了$this->Article类中的getTags(),一切开始都很好。