Details
Description
When merging a DETACHED entity into the repository with ManyToMany associations that have changed since the last flush, the join table is not updated with the new associations.
Many Movies have many Artists with cascade merge:
class Movie {
/** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */
public $id;
/** @Column(length=50, type="string") */
public $title;
/**
* @ManyToMany(targetEntity="Artist", cascade={"merge"})
*/
public $artists;
public function __construct() {
$this->artists = new ArrayCollection();
}
}
<?php
class Artist {
/** @Id @Column(type="integer") @GeneratedValue(strategy="IDENTITY") */
public $id;
/** @Column(length=50, type="string") */
public $name;
/** @ManyToMany(targetEntity="Movie", mappedBy="artists", cascade={"merge"}) */
public $movies;
public function __construct() {
$this->movies = new ArrayCollection();
}
}
Assume that the database contains:
Movie: id=1, title="Movie 1"
Artist: id=1, name="Artist 1"
Artist: id=2, name="Artist 2"
and that there is a entry (1, 1) in movie_artist so that there is a many-many relationship between Movie 1 and Artist 1.
I now want to merge a detched version of a movie into the repository, with an extra relationship between Movie 1 and Artist 2:
$m1 = new \vo\Movie(); $m1->id = 1; $m1->title = "Movie 1"; $a1 = new \vo\Artist(); $a1->id = 1; $a1->name = "Artist 1"; $a2 = new \vo\Artist(); $a2->id = 2; $a2->name = "Artist 2"; $m1->artists->add($a1); $a1->movies->add($m1); // Add the new association $m1->artists->add($a2); $a2->movies->add($m1); // This is a cascade merge so it will merge the entities down the tree $m1 = $em->merge($m1);
At this point $m1 should contains merged entities reflecting the same as what is in the database, plus the extra association between Movie 1 and Artist 2, so after a flush() movie_artist should contain (1,1),(1,2)
I now run:
$em->flush();
but movie_artist remains unchanged (still just (1,1)) and $em->getUnitOfWork()->computeChangeSets() is empty before the flush().
The same behaviour is exhibited when merging $m1 after deleting entities from its many-to-many associations; movie_artist does not change.
Here is a failing test case for this issue.