@Steve
I cannot reproduce your issue.
Attached is a test script.
Your code is very weird btw, why are you getting and setting groups collection? It is passed by reference so you can just have something like $role->addGroup() and $role->removeGroup() and encapsulate the logic?
Also your tests are pretty useless, you check if two variables which are the same reference to the same collection are the same. Which should always be true.
@Lee
Can you provide more details? I cant verify this without more details.
Same problem here. I wanted to write some unit tests, checking the entity relations and ran into exactly the same problem. Maybe my code can provide some more information (Group entity is the owning side, role entity is the inverse side):
WHAT DOES NOT WORK:
/** * Test ArrayCollection */ $group = new Group('Group Test'); $em->persist($group); $em->flush(); $groups = new ArrayCollection(); $groups->add($group); $this->role->setGroups($groups); $this->assertEquals($groups, $this->role->getGroups()); /** * Test PersistentCollection */ $em->persist($this->role); $em->flush(); $groups = $this->role->getGroups(); $groups->removeElement($group); // first remove element before adding a new one $group = new Group('Group Test 2'); $em->persist($group); $em->flush(); $groups->add($group); $this->role->setGroups($groups); $this->assertEquals($groups, $this->role->getGroups());WHAT WORKS:
/** * Test ArrayCollection */ $group = new Group('Group Test'); $em->persist($group); $em->flush(); $groups = new ArrayCollection(); $groups->add($group); $this->role->setGroups($groups); $this->assertEquals($groups, $this->role->getGroups()); /** * Test PersistentCollection */ $em->persist($this->role); $em->flush(); $groups = $this->role->getGroups(); $group2 = new Group('Group Test 2'); $em->persist($group2); $em->flush(); $groups->add($group2); // first adding a new element before removing one $groups->removeElement($group); $this->role->setGroups($groups); $this->assertEquals($groups, $this->role->getGroups());Hope this helps in any way... I tried figuring it out on my own but I am too drunk right now xD