From 5d333045b9692dd4c28dcb61a41d6c025f3de3f7 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 23 Jan 2011 12:48:28 +0100 Subject: [PATCH] DDC-960 - Bugfix in how Persisters generate Fetch last version of Entity SQL. --- .../ORM/Persisters/BasicEntityPersister.php | 31 +++++-- .../Persisters/JoinedSubclassPersister.php | 19 ++-- .../ORM/Functional/Ticket/DDC960Test.php | 92 +++++++++++++++++++ 3 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC960Test.php diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index e626d17a2..61e5bb9f2 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -223,7 +223,7 @@ class BasicEntityPersister } if ($this->_class->isVersioned) { - $this->_assignDefaultVersionValue($this->_class, $entity, $id); + $this->assignDefaultVersionValue($entity, $id); } } @@ -238,22 +238,33 @@ class BasicEntityPersister * by the preceding INSERT statement and assigns it back in to the * entities version field. * - * @param Doctrine\ORM\Mapping\ClassMetadata $class * @param object $entity * @param mixed $id */ - protected function _assignDefaultVersionValue($class, $entity, $id) + protected function assignDefaultVersionValue($entity, $id) { - $versionField = $this->_class->versionField; - $identifier = $this->_class->getIdentifierColumnNames(); - $versionFieldColumnName = $this->_class->getColumnName($versionField); + $value = $this->fetchVersionValue($this->_class, $id); + $this->_class->setFieldValue($entity, $this->_class->versionField, $value); + } + + /** + * Fetch the current version value of a versioned entity. + * + * @param Doctrine\ORM\Mapping\ClassMetadata $versionedClass + * @param mixed $id + * @return mixed + */ + protected function fetchVersionValue($versionedClass, $id) + { + $versionField = $versionedClass->versionField; + $identifier = $versionedClass->getIdentifierColumnNames(); + $versionFieldColumnName = $versionedClass->getColumnName($versionField); //FIXME: Order with composite keys might not be correct - $sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) + $sql = "SELECT " . $versionFieldColumnName . " FROM " . $versionedClass->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); + return Type::getType($versionedClass->fieldMappings[$versionField]['type'])->convertToPHPValue($value, $this->_platform); } /** @@ -282,7 +293,7 @@ class BasicEntityPersister if ($this->_class->isVersioned) { $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); - $this->_assignDefaultVersionValue($this->_class, $entity, $id); + $this->assignDefaultVersionValue($entity, $id); } } } diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 9ae242a45..8aa784001 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -108,10 +108,6 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister return; } - if ($this->_class->isVersioned) { - $versionedClass = $this->_getVersionedClassMetadata(); - } - $postInsertIds = array(); $idGen = $this->_class->idGenerator; $isPostInsertId = $idGen->isPostInsertGenerator(); @@ -177,8 +173,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $stmt->closeCursor(); } - if (isset($versionedClass)) { - $this->_assignDefaultVersionValue($versionedClass, $entity, $id); + if ($this->_class->isVersioned) { + $this->assignDefaultVersionValue($entity, $id); } $this->_queuedInserts = array(); @@ -208,7 +204,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister $this->_updateTable($entity, $versionedClass->getQuotedTableName($this->_platform), array(), true); $id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity); - $this->_assignDefaultVersionValue($this->_class, $entity, $id); + $this->assignDefaultVersionValue($entity, $id); } } } @@ -428,4 +424,13 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister return $columns; } + + /** + * {@inheritdoc} + */ + protected function assignDefaultVersionValue($entity, $id) + { + $value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id); + $this->_class->setFieldValue($entity, $this->_class->versionField, $value); + } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC960Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC960Test.php new file mode 100644 index 000000000..99a29a57a --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC960Test.php @@ -0,0 +1,92 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC960Root'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC960Child') + )); + } catch(\Exception $e) { + + } + } + + /** + * @group DDC-960 + */ + public function testUpdateRootVersion() + { + $child = new DDC960Child('Test'); + $this->_em->persist($child); + $this->_em->flush(); + + $child->setName("Test2"); + + $this->_em->flush(); + + $this->assertEquals(2, $child->getVersion()); + } +} + +/** + * @Entity + * @InheritanceType("JOINED") + * @DiscriminatorMap({ + * "root" = "DDC960Root", + * "child" = "DDC960Child" + * }) + */ +class DDC960Root +{ + /** + * @Id @GeneratedValue @Column(type="integer") + */ + private $id; + + /** + * @Column(type="integer") @Version + */ + private $version; + + public function getId() + { + return $this->id; + } + + public function getVersion() + { + return $this->version; + } +} + +/** + * @Entity + */ +class DDC960Child extends DDC960Root +{ + /** + * @column(type="string") + * @var string + */ + private $name; + + public function __construct($name) + { + $this->name = $name; + } + + public function setName($name) + { + $this->name = $name; + } +} \ No newline at end of file