From ac85584e9bc6a8cb5e00aa71c67e876923056275 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 9 Nov 2010 23:15:14 +0100 Subject: [PATCH] DDC-870 - Fix several bugs with optimistic locking, conversion of types, multiple updating of values and inheritance related stuff. --- .../ORM/Persisters/BasicEntityPersister.php | 9 ++++++++- .../Persisters/JoinedSubclassPersister.php | 3 +++ .../ORM/Functional/Locking/OptimisticTest.php | 20 ++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index af45218f9..f249ac29a 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -251,6 +251,8 @@ class BasicEntityPersister $sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) . " WHERE " . implode(' = ? AND ', $identifier) . " = ?"; $value = $this->_conn->fetchColumn($sql, array_values((array)$id)); + + $value = Type::getType($class->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform); $this->_class->setFieldValue($entity, $versionField, $value); } @@ -277,6 +279,11 @@ class BasicEntityPersister $entity, $this->_class->getQuotedTableName($this->_platform), $updateData[$tableName], $this->_class->isVersioned ); + + if ($this->_class->isVersioned) { + $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $this->_assignDefaultVersionValue($this->_class, $entity, $id); + } } } @@ -313,7 +320,7 @@ class BasicEntityPersister if ($versioned) { $versionField = $this->_class->versionField; - $versionFieldType = $this->_class->getTypeOfField($versionField); + $versionFieldType = $this->_class->fieldMappings[$versionField]['type']; $versionColumn = $this->_class->getQuotedColumnName($versionField, $this->_platform); if ($versionFieldType == Type::INTEGER) { $set[] = $versionColumn . ' = ' . $versionColumn . ' + 1'; diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 4972ee6b2..4cbe11ee9 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -205,6 +205,9 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister // table were affected. if ($isVersioned && ! isset($updateData[$versionedTable])) { $this->_updateTable($entity, $versionedClass->getQuotedTableName($this->_platform), array(), true); + + $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); + $this->_assignDefaultVersionValue($this->_class, $entity, $id); } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php index 73c5cdcf1..498a8b0b8 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/OptimisticTest.php @@ -100,6 +100,20 @@ class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase } } + public function testMultipleFlushesDoIncrementalUpdates() + { + $test = new OptimisticStandard(); + + for ($i = 0; $i < 5; $i++) { + $test->name = 'test' . $i; + $this->_em->persist($test); + $this->_em->flush(); + + $this->assertType('int', $test->getVersion()); + $this->assertEquals($i + 1, $test->getVersion()); + } + } + public function testStandardInsertSetsInitialVersionValue() { $test = new OptimisticStandard(); @@ -107,6 +121,7 @@ class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($test); $this->_em->flush(); + $this->assertType('int', $test->getVersion()); $this->assertEquals(1, $test->getVersion()); return $test; @@ -139,10 +154,13 @@ class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase { $test = new OptimisticTimestamp(); $test->name = 'Testing'; + + $this->assertNull($test->version, "Pre-Condition"); + $this->_em->persist($test); $this->_em->flush(); - $this->assertTrue(strtotime($test->version) > 0); + $this->assertType('DateTime', $test->version); return $test; }