Symfony and Doctrine - cannot remove item from collection (Many-to-Many relation with extra field)

大城市里の小女人 提交于 2019-12-10 11:09:43

问题


Good day everyone! I have a question about collection persistence in Symfony and Doctrine

The short version

I can add an item to collection (persist) via form, but can not remove (remove).

The logics

I need a possibility to add users to business trips. Each added user must have text description (like a objective for business trip).

In fact, i have 3 entities:

  • BusinessTrip
  • BusinessTripUser (stores linked User id, BusinessTrip id and text field "description")
  • User (vendor bundle entity)

The problem

As i already said above - i cant remove BusinessTripUser from collection $users in BusinessTrip. I mentioned that if i remove unidirectional relation between BusinessTripUser and User - everything work fine. It looks like this relation prevents orphanRemoval mechanism delete the orphaned BusinessTripUser enity.

Relations between entities

class BusinessTrip
{
    /**
     * @ORM\OneToMany(targetEntity="OQ\BusinessTripBundle\Entity\BusinessTripUser", mappedBy="businessTrip", cascade={"persist","remove"}, orphanRemoval=true)
     */
    protected $users;
}

 

class BusinessTripUser
    /**
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="OQ\BusinessTripBundle\Entity\BusinessTrip", inversedBy="users")
     * @ORM\JoinColumn(name="business_trip_id", referencedColumnName="id", nullable=false)
     */
    protected $businessTrip;
}

Symfony v2.7.3, Doctrine v2.5.1

来源:https://stackoverflow.com/questions/33756301/symfony-and-doctrine-cannot-remove-item-from-collection-many-to-many-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!