removeElement() and clear() doesn't work in doctrine 2 with array collection property

前端 未结 2 2067
小鲜肉
小鲜肉 2021-02-03 11:37

I\'m trying to get some simple CRUD done with doctrine 2 but when it\'s time to update a record with one property set as an array collection I don\'t seem to get removeElement()

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 11:44

    New answer

    In your countries entity, should you not have:

    @ManyToOne(targetEntity="User", inversedBy="countries") 
    

    instead of inversedBy="id"?

    Initial answer

    You need to set the countries field in your entity as remove cascade. For example, on a bidirectional one to many relationship:

    class Entity
    {
        /**
         * 
         * @OneToMany(targetEntity="Country", mappedBy="entity", cascade={"remove"})
         */
        private $countries;
    }
    

    This way, when saving your entity, doctrine will also save changes in collections attached to your entity (such as countries). Otherwise you have to explicitly remove the countries you want to remove before flushing, e.g.

    $this->em()->remove($aCountry);
    

    This is also valid for persist, merge and detach operations. More information here.

提交回复
热议问题