Skip to content

Commit

Permalink
[DDC-1129] Fix bug in version changeset computation aswell as inline …
Browse files Browse the repository at this point in the history
…ClassMetadata::isCollectionValuedAssociation to increase performance by 2-5%
  • Loading branch information
beberlei committed May 1, 2011
1 parent cc26080 commit 5d7063b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/Doctrine/ORM/UnitOfWork.php
Expand Up @@ -399,8 +399,11 @@ public function computeChangeSet(ClassMetadata $class, $entity)
$actualData = array();
foreach ($class->reflFields as $name => $refProp) {
$value = $refProp->getValue($entity);
if ($class->isCollectionValuedAssociation($name) && $value !== null
if (isset($class->associationMappings[$name])
&& ($class->associationMappings[$name]['type'] & ClassMetadata::TO_MANY)
&& $value !== null
&& ! ($value instanceof PersistentCollection)) {

// If $value is not a Collection then use an ArrayCollection.
if ( ! $value instanceof Collection) {
$value = new ArrayCollection($value);
Expand All @@ -419,7 +422,7 @@ public function computeChangeSet(ClassMetadata $class, $entity)
$coll->setDirty( ! $coll->isEmpty());
$class->reflFields[$name]->setValue($entity, $coll);
$actualData[$name] = $coll;
} else if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
} else if ( (! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField) ) {
$actualData[$name] = $value;
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1129Test.php
@@ -0,0 +1,46 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
require_once __DIR__ . '/../../../TestInit.php';

/**
* @group DDC-1129
*/
class DDC1129Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}

public function testVersionFieldIgnoredInChangesetComputation()
{
$article = new \Doctrine\Tests\Models\CMS\CmsArticle();
$article->text = "I don't know.";
$article->topic = "Who is John Galt?";

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

$this->assertEquals(1, $article->version);

$class = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsArticle');
$uow = $this->_em->getUnitOfWork();

$uow->computeChangeSet($class, $article);
$changeSet = $uow->getEntityChangeSet($article);
$this->assertEquals(0, count($changeSet), "No changesets should be computed.");

$article->text = "This is John Galt speaking.";
$this->_em->flush();

$this->assertEquals(2, $article->version);

$uow->computeChangeSet($class, $article);
$changeSet = $uow->getEntityChangeSet($article);
$this->assertEquals(0, count($changeSet), "No changesets should be computed.");
}
}

0 comments on commit 5d7063b

Please sign in to comment.