Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Merged
merged 2 commits into from about 2 years ago

3 participants

Francisco Facioni Guilherme Blanco Benjamin Eberlei
Francisco Facioni

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

Francisco Facioni 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
Guilherme Blanco

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.

Francisco Facioni

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.

Francisco Facioni

The test case fails without my patch

Benjamin Eberlei beberlei merged commit 9cddaf3 into from March 03, 2012
Benjamin Eberlei beberlei closed this March 03, 2012
Benjamin Eberlei
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

Showing 2 unique commits by 1 author.

Jan 12, 2012
Francisco Facioni 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
Jan 13, 2012
Francisco Facioni UnitTest for ManyToMany update notification bab14bf
This page is out of date. Refresh to see the latest.
9  lib/Doctrine/ORM/UnitOfWork.php
@@ -620,6 +620,15 @@ public function computeChangeSet(ClassMetadata $class, $entity)
620 620
         foreach ($class->associationMappings as $field => $assoc) {
621 621
             if (($val = $class->reflFields[$field]->getValue($entity)) !== null) {
622 622
                 $this->computeAssociationChanges($assoc, $val);
  623
+                if (!isset($this->entityChangeSets[$oid]) &&
  624
+                    $assoc['isOwningSide'] &&
  625
+                    $assoc['type'] == ClassMetadata::MANY_TO_MANY &&
  626
+                    $val instanceof PersistentCollection &&
  627
+                    $val->isDirty()) {
  628
+                    $this->entityChangeSets[$oid]   = array();
  629
+                    $this->originalEntityData[$oid] = $actualData;
  630
+                    $this->entityUpdates[$oid]      = $entity;
  631
+                }
623 632
             }
624 633
         }
625 634
     }
76  tests/Doctrine/Tests/ORM/Functional/ManyToManyEventTest.php
... ...
@@ -0,0 +1,76 @@
  1
+<?php
  2
+namespace Doctrine\Tests\ORM\Functional;
  3
+use Doctrine\Tests\Models\CMS\CmsUser;
  4
+use Doctrine\Tests\Models\CMS\CmsGroup;
  5
+use Doctrine\ORM\Events;
  6
+require_once __DIR__ . '/../../TestInit.php';
  7
+
  8
+/**
  9
+ * ManyToManyEventTest
  10
+ *
  11
+ * @author Francisco Facioni <fran6co@gmail.com>
  12
+ */
  13
+class ManyToManyEventTest extends \Doctrine\Tests\OrmFunctionalTestCase
  14
+{
  15
+    /**
  16
+     * @var PostUpdateListener
  17
+     */
  18
+    private $listener;
  19
+
  20
+    protected function setUp()
  21
+    {
  22
+        $this->useModelSet('cms');
  23
+        parent::setUp();
  24
+        $this->listener = new PostUpdateListener();
  25
+        $evm = $this->_em->getEventManager();
  26
+        $evm->addEventListener(Events::postUpdate, $this->listener);
  27
+    }
  28
+
  29
+    public function testListenerShouldBeNotifiedOnlyWhenUpdating()
  30
+    {
  31
+        $user = $this->createNewValidUser();
  32
+        $this->_em->persist($user);
  33
+        $this->_em->flush();
  34
+        $this->assertFalse($this->listener->wasNotified);
  35
+
  36
+        $group = new CmsGroup();
  37
+        $group->name = "admins";
  38
+        $user->addGroup($group);
  39
+        $this->_em->persist($user);
  40
+        $this->_em->flush();
  41
+
  42
+        $this->assertTrue($this->listener->wasNotified);
  43
+    }
  44
+
  45
+    /**
  46
+     * @return CmsUser
  47
+     */
  48
+    private function createNewValidUser()
  49
+    {
  50
+        $user = new CmsUser();
  51
+        $user->username = 'fran6co';
  52
+        $user->name = 'Francisco Facioni';
  53
+        $group = new CmsGroup();
  54
+        $group->name = "users";
  55
+        $user->addGroup($group);
  56
+        return $user;
  57
+    }
  58
+}
  59
+
  60
+class PostUpdateListener
  61
+{
  62
+    /**
  63
+     * @var bool
  64
+     */
  65
+    public $wasNotified = false;
  66
+
  67
+    /**
  68
+     * @param $args
  69
+     */
  70
+    public function postUpdate($args)
  71
+    {
  72
+        $this->wasNotified = true;
  73
+    }
  74
+}
  75
+
  76
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.