From 1ec5632a189b2a11b439ddae90dabdc2152be917 Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Thu, 19 Sep 2013 12:13:12 +0200 Subject: [PATCH 1/4] Multiple invokation of listeners on PreFlush event Only lifecycle callbacks and entity listeners should be triggered here. The preFlush listener event is already triggered at the beginning of commit() --- lib/Doctrine/ORM/UnitOfWork.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index 5de769f63..f8775e478 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -530,7 +530,7 @@ class UnitOfWork implements PropertyChangedListener $class = $this->em->getClassMetadata(get_class($entity)); } - $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush); + $invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER; if ($invoke !== ListenersInvoker::INVOKE_NONE) { $this->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($this->em), $invoke); From d4a08f7ab76658e38a93d7705ad2881f911aecbd Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Thu, 19 Sep 2013 14:16:33 +0200 Subject: [PATCH 2/4] Added unit test --- .../ORM/Functional/Ticket/DDC2692Test.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php new file mode 100644 index 000000000..2cdbfb0fe --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php @@ -0,0 +1,65 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2692Foo'), + )); + } catch(\Exception $e) { + return; + } + $this->_em->clear(); + } + + public function testListenerCalledOneOnPreFlush() + { + $listener = $this->getMock('Doctrine\Tests\ORM\Functional\Ticket\Listener', array('preFlush')); + $listener->expects($this->once())->method('preFlush'); + + $this->_em->getEventManager()->addEventSubscriber($listener); + + $this->_em->persist(new DDC2692Foo); + $this->_em->persist(new DDC2692Foo); + + $this->_em->flush(); + $this->_em->clear(); + } +} +/** + * @Entity @Table(name="ddc_2692_foo") + */ +class DDC2692Foo +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; +} + +class Listener implements EventSubscriber { + + public function getSubscribedEvents() { + return array(\Doctrine\ORM\Events::preFlush); + } + + public function preFlush(PreFlushEventArgs $args) { + } +} + + From 619c6a03ce503a17a5c54fd8c98656f4f1d24817 Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Thu, 19 Sep 2013 15:16:51 +0200 Subject: [PATCH 3/4] removed unused use statements, fixed typo and group tag --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php index 2cdbfb0fe..6e2dff1f3 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php @@ -4,11 +4,9 @@ namespace Doctrine\Tests\ORM\Functional\Ticket; use Doctrine\Common\EventSubscriber; use Doctrine\ORM\Event\PreFlushEventArgs; -use Doctrine\ORM\Query\ResultSetMappingBuilder; -use Doctrine\Common\Collections\ArrayCollection; /** - * @group + * @group DDC-2692 */ class DDC2692Test extends \Doctrine\Tests\OrmFunctionalTestCase { @@ -29,7 +27,7 @@ class DDC2692Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); } - public function testListenerCalledOneOnPreFlush() + public function testIsListenerCalledOnlyOnceOnPreFlush() { $listener = $this->getMock('Doctrine\Tests\ORM\Functional\Ticket\Listener', array('preFlush')); $listener->expects($this->once())->method('preFlush'); From adf2b7cce7dd2c833974def851f1202c280d80f8 Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Fri, 20 Sep 2013 09:20:58 +0200 Subject: [PATCH 4/4] Listener class prefix --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php index 6e2dff1f3..dfdb207fc 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2692Test.php @@ -29,7 +29,7 @@ class DDC2692Test extends \Doctrine\Tests\OrmFunctionalTestCase public function testIsListenerCalledOnlyOnceOnPreFlush() { - $listener = $this->getMock('Doctrine\Tests\ORM\Functional\Ticket\Listener', array('preFlush')); + $listener = $this->getMock('Doctrine\Tests\ORM\Functional\Ticket\DDC2692Listener', array('preFlush')); $listener->expects($this->once())->method('preFlush'); $this->_em->getEventManager()->addEventSubscriber($listener); @@ -50,7 +50,7 @@ class DDC2692Foo public $id; } -class Listener implements EventSubscriber { +class DDC2692Listener implements EventSubscriber { public function getSubscribedEvents() { return array(\Doctrine\ORM\Events::preFlush);