Skip to content

Commit

Permalink
Merge pull request doctrine#1074 from zimmermanj42/DDC-3160
Browse files Browse the repository at this point in the history
[DDC-3160] Alternate fix for DDC-2996 bug
  • Loading branch information
Ocramius committed Jul 6, 2014
2 parents 8c0166d + 6a48675 commit a8035f2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/Doctrine/ORM/UnitOfWork.php
Expand Up @@ -943,12 +943,13 @@ public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity)
}

if ($changeSet) {
$this->entityChangeSets[$oid] = (isset($this->entityChangeSets[$oid]))
? array_merge($this->entityChangeSets[$oid], $changeSet)
: $changeSet;

if (isset($this->entityChangeSets[$oid])) {
$this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet);
} else if ( ! isset($this->entityInsertions[$oid])) {
$this->entityChangeSets[$oid] = $changeSet;
$this->entityUpdates[$oid] = $entity;
}
$this->originalEntityData[$oid] = $actualData;
$this->entityUpdates[$oid] = $entity;
}
}

Expand Down Expand Up @@ -2405,12 +2406,12 @@ public function clear($entityName = null)
}
} else {
$visited = array();

foreach ($this->identityMap as $className => $entities) {
if ($className !== $entityName) {
continue;
}

foreach ($entities as $entity) {
$this->doDetach($entity, $visited, false);
}
Expand Down Expand Up @@ -2522,7 +2523,7 @@ public function createEntity($className, array $data, &$hints = array())

$id = array($class->identifier[0] => $id);
}

$idHash = implode(' ', $id);

if (isset($this->identityMap[$class->rootEntityName][$idHash])) {
Expand Down
70 changes: 70 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3160Test.php
@@ -0,0 +1,70 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* FlushEventTest
*
* @author robo
*/
class DDC3160Test extends OrmFunctionalTestCase
{
protected function setUp() {
$this->useModelSet('cms');
parent::setUp();
}

/**
* @group DDC-3160
*/
public function testNoUpdateOnInsert()
{
$listener = new DDC3160OnFlushListener();
$this->_em->getEventManager()->addEventListener(Events::onFlush, $listener);

$user = new CmsUser;
$user->username = 'romanb';
$user->name = 'Roman';
$user->status = 'Dev';

$this->_em->persist($user);
$this->_em->flush();

$this->_em->refresh($user);

$this->assertEquals('romanc', $user->username);
$this->assertEquals(1, $listener->inserts);
$this->assertEquals(0, $listener->updates);
}
}

class DDC3160OnFlushListener
{
public $inserts = 0;
public $updates = 0;

public function onFlush(OnFlushEventArgs $args)
{
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
$this->inserts++;
if ($entity instanceof CmsUser) {
$entity->username = 'romanc';
$cm = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($cm, $entity);
}
}

foreach ($uow->getScheduledEntityUpdates() as $entity) {
$this->updates++;
}
}
}

0 comments on commit a8035f2

Please sign in to comment.