Skip to content

Commit

Permalink
Merge branch 'DDC-2173'
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jan 6, 2013
2 parents ce290bc + 512a001 commit c20cfed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
8 changes: 8 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Upgrade to 2.4

## OnFlush and PreFlush event always called

Before 2.4 the preFlush and onFlush events were only called when there were
actually entities that changed. Now these events are called no matter if there
are entities in the UoW or changes are found.

# Upgrade to 2.3

## EntityManager#find() not calls EntityRepository#find() anymore
Expand Down
27 changes: 19 additions & 8 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ public function commit($entity = null)
$this->collectionUpdates ||
$this->collectionDeletions ||
$this->orphanRemovals)) {
$this->dispatchOnFlushEvent();
$this->dispatchPostFlushEvent();

return; // Nothing to do.
}

Expand All @@ -296,10 +299,7 @@ public function commit($entity = null)
}
}

// Raise onFlush
if ($this->evm->hasListeners(Events::onFlush)) {
$this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em));
}
$this->dispatchOnFlushEvent();

// Now we need a commit order to maintain referential integrity
$commitOrder = $this->getCommitOrder();
Expand Down Expand Up @@ -354,10 +354,7 @@ public function commit($entity = null)
$coll->takeSnapshot();
}

// Raise postFlush
if ($this->evm->hasListeners(Events::postFlush)) {
$this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em));
}
$this->dispatchPostFlushEvent();

// Clear up
$this->entityInsertions =
Expand Down Expand Up @@ -3153,4 +3150,18 @@ public function isReadOnly($object)

return isset($this->readOnlyObjects[spl_object_hash($object)]);
}

private function dispatchOnFlushEvent()
{
if ($this->evm->hasListeners(Events::onFlush)) {
$this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em));
}
}

private function dispatchPostFlushEvent()
{
if ($this->evm->hasListeners(Events::postFlush)) {
$this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em));
}
}
}
34 changes: 34 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/FlushEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ public function testPersistNewEntitiesOnPreFlush()
//echo "SECOND FLUSH";
//$this->_em->flush();
}

/**
* @group DDC-2173
*/
public function testPreAndOnFlushCalledAlways()
{
$listener = new OnFlushCalledListener();
$this->_em->getEventManager()->addEventListener(Events::onFlush, $listener);
$this->_em->getEventManager()->addEventListener(Events::preFlush, $listener);

$this->_em->flush();

$this->assertEquals(1, $listener->preFlush);
$this->assertEquals(1, $listener->onFlush);

$this->_em->flush();

$this->assertEquals(2, $listener->preFlush);
$this->assertEquals(2, $listener->onFlush);
}
}

class OnFlushListener
Expand Down Expand Up @@ -91,4 +111,18 @@ public function onFlush(OnFlushEventArgs $args)
}
}

class OnFlushCalledListener
{
public $preFlush = 0;
public $onFlush = 0;

public function preFlush($args)
{
$this->preFlush++;
}

public function onFlush($args)
{
$this->onFlush++;
}
}

0 comments on commit c20cfed

Please sign in to comment.