Doctrine2 orphanRemoval ManyToMany only if there are no other records connected

My goal is to delete a record in the executor of an object only if there are no other records related to the essence of the soundtrack.

I tried with orphanRemoval this way:

Soundtrack.php

/** * @Assert\NotBlank(message = "soundtrack.artists.blank") * @ORM\ManyToMany(targetEntity="Artist", inversedBy="soundtrack", cascade={"persist", "remove"}, orphanRemoval=true) * @ORM\JoinTable(name="soundtrack_artist") * @ORM\OrderBy({"name" = "ASC"}) **/ private $artists; 

Artist.php

 /** * @ORM\ManyToMany(targetEntity="Soundtrack", mappedBy="artists") */ private $soundtrack; 

but when I delete the soundtrack to the entity record, it also clears the object's artist record, even if it is associated with the soundtrack to the other records (I think this is what you should expect from orphanRemoval).

Is there a way to delete this record "orphaned" only if no other records are connected?

I also tried like this:

 **Soundtrack.php** /** * @Assert\NotBlank(message = "soundtrack.artists.blank") * @ORM\ManyToMany(targetEntity="Artist", inversedBy="soundtrack", cascade={"persist"}, orphanRemoval=true) * @ORM\JoinTable(name="soundtrack_artist") * @ORM\OrderBy({"name" = "ASC"}) **/ private $artists; 

but does not delete the executor of the entity records.

+4
source share
1 answer

orphanRemoval option explicitly thinks that the native object object is the only instance that references its children. To do this, you must disconnect the child from the parent (link to cancel) to remove the child. With Many-2-many associations, you must detach objects on both sides (both belonging and returning)

See Docs

When using the orphanRemoval = true option, Doctrine makes the assumption that the subjects are privately owned and will NOT be reused by other legal entities. If you neglect this assumption, your entities are deleted by the doctrine, even if you have assigned an orphaned object to another.

0
source

Source: https://habr.com/ru/post/1500180/


All Articles