4

My purpose is to remove the record in the entity artist only if there are no other records of the entity connected soundtrack.

I tried with orphanRemoval in 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 an entity record soundtrack, also clears the record of the entity artist even if it is linked to other records soundtrack (I think this is what you should expect from orphanRemoval).

Is there a way to remove that record an "orphan" only when no other records connected?

I also tried just 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 records entity artist ..

Lughino
  • 4,060
  • 11
  • 31
  • 61
  • 1
    Interestingly the `@ManyToMany` annotation doesn't officially support an `orphanRemoval` option (see [latest docs (as of Nov. 2013)](http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#annref-manytomany)) but does take it into account, albeit with the stated relentlessness. Would like to see an answer to this too. – flu Nov 15 '13 at 14:22
  • Post your code from the controller. How does it look like? – Jekis Mar 12 '14 at 13:08

1 Answers1

1

orphanRemoval option explicitly thinks, that owning side object is the only instance that references its children. To make it works you should detach child from parent (unset reference) to make child deleted. With Many-2-many associations you should detach entities on both side (owned and inversed)

See Docs

When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities. If you neglect this assumption your entities will get deleted by Doctrine even if you assigned the orphaned entity to another one.

vctls
  • 736
  • 7
  • 25
ScayTrase
  • 1,810
  • 23
  • 36