diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 146789397..0e55cfa80 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -637,6 +637,15 @@ class UnitOfWork implements PropertyChangedListener foreach ($class->associationMappings as $field => $assoc) { if (($val = $class->reflFields[$field]->getValue($entity)) !== null) { $this->computeAssociationChanges($assoc, $val); + if (!isset($this->entityChangeSets[$oid]) && + $assoc['isOwningSide'] && + $assoc['type'] == ClassMetadata::MANY_TO_MANY && + $val instanceof PersistentCollection && + $val->isDirty()) { + $this->entityChangeSets[$oid] = array(); + $this->originalEntityData[$oid] = $actualData; + $this->entityUpdates[$oid] = $entity; + } } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyEventTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyEventTest.php new file mode 100644 index 000000000..47c96b1fe --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyEventTest.php @@ -0,0 +1,76 @@ + + */ +class ManyToManyEventTest extends \Doctrine\Tests\OrmFunctionalTestCase +{ + /** + * @var PostUpdateListener + */ + private $listener; + + protected function setUp() + { + $this->useModelSet('cms'); + parent::setUp(); + $this->listener = new PostUpdateListener(); + $evm = $this->_em->getEventManager(); + $evm->addEventListener(Events::postUpdate, $this->listener); + } + + public function testListenerShouldBeNotifiedOnlyWhenUpdating() + { + $user = $this->createNewValidUser(); + $this->_em->persist($user); + $this->_em->flush(); + $this->assertFalse($this->listener->wasNotified); + + $group = new CmsGroup(); + $group->name = "admins"; + $user->addGroup($group); + $this->_em->persist($user); + $this->_em->flush(); + + $this->assertTrue($this->listener->wasNotified); + } + + /** + * @return CmsUser + */ + private function createNewValidUser() + { + $user = new CmsUser(); + $user->username = 'fran6co'; + $user->name = 'Francisco Facioni'; + $group = new CmsGroup(); + $group->name = "users"; + $user->addGroup($group); + return $user; + } +} + +class PostUpdateListener +{ + /** + * @var bool + */ + public $wasNotified = false; + + /** + * @param $args + */ + public function postUpdate($args) + { + $this->wasNotified = true; + } +} + +