Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

When using a ManyToMany relationship no listener is notified about any change to the owning entity #256

Merged
merged 2 commits into from

3 participants

@fran6co

What I'm doing with this patch is marking the entity for update when there is a modification in the ManyToMany relationship so the listeners are notified about it.

The main reason for this is for hooking up services like Solr or other indexers to update the entities even for ManyToMany relationships.

This is the followup for #255

@fran6co fran6co When using a ManyToMany relationship no listener is notified about an…
…y change to the owning entity.

What I'm doing with this patch is marking the entity for update when there is a modification in the ManyToMany relationship so the listeners are notified about it.

The main reason for this is for hooking up services like Solr or other indexers to update the entities even for ManyToMany relationships.
e7a6d87
@guilhermeblanco

Are you able to create a testcase that cover this lack of listener's notification?
This change you made seems perfect, but we need to be sure how this may impact other sections of our codebase too.

@fran6co

The test cases run successfully now, not sure how else to make sure that this change doesn't impact in a weird way in the rest of the codebase. I'm going to create some test cases to check that my change is triggering the proper event for entities with a ManyToMany relationship.

@fran6co

The test case fails without my patch

@beberlei beberlei merged commit 9cddaf3 into from
@beberlei
Owner

And merged back into 2.1 and 2.2, see http://www.doctrine-project.org/jira/browse/DDC-1678

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 12, 2012
  1. @fran6co

    When using a ManyToMany relationship no listener is notified about an…

    fran6co authored
    …y change to the owning entity.
    
    What I'm doing with this patch is marking the entity for update when there is a modification in the ManyToMany relationship so the listeners are notified about it.
    
    The main reason for this is for hooking up services like Solr or other indexers to update the entities even for ManyToMany relationships.
Commits on Jan 13, 2012
  1. @fran6co
This page is out of date. Refresh to see the latest.
View
9 lib/Doctrine/ORM/UnitOfWork.php
@@ -620,6 +620,15 @@ public function computeChangeSet(ClassMetadata $class, $entity)
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;
+ }
}
}
}
View
76 tests/Doctrine/Tests/ORM/Functional/ManyToManyEventTest.php
@@ -0,0 +1,76 @@
+<?php
+namespace Doctrine\Tests\ORM\Functional;
+use Doctrine\Tests\Models\CMS\CmsUser;
+use Doctrine\Tests\Models\CMS\CmsGroup;
+use Doctrine\ORM\Events;
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ * ManyToManyEventTest
+ *
+ * @author Francisco Facioni <fran6co@gmail.com>
+ */
+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;
+ }
+}
+
+
Something went wrong with that request. Please try again.