Skip to content

Commit

Permalink
Merge branch 'DDc-960' into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jan 23, 2011
2 parents ecdc0fa + 203fb59 commit 837d3f0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 17 deletions.
31 changes: 21 additions & 10 deletions lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
Expand Up @@ -223,7 +223,7 @@ public function executeInserts()
}

if ($this->_class->isVersioned) {
$this->_assignDefaultVersionValue($this->_class, $entity, $id);
$this->assignDefaultVersionValue($entity, $id);
}
}

Expand All @@ -238,22 +238,33 @@ public function executeInserts()
* 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)
{
$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 = $this->_class->versionField;
$identifier = $this->_class->getIdentifierColumnNames();
$versionFieldColumnName = $this->_class->getColumnName($versionField);
$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);
}

/**
Expand Down Expand Up @@ -282,7 +293,7 @@ public function update($entity)

if ($this->_class->isVersioned) {
$id = $this->_em->getUnitOfWork()->getEntityIdentifier($entity);
$this->_assignDefaultVersionValue($this->_class, $entity, $id);
$this->assignDefaultVersionValue($entity, $id);
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php
Expand Up @@ -107,10 +107,6 @@ public function executeInserts()
return;
}

if ($this->_class->isVersioned) {
$versionedClass = $this->_getVersionedClassMetadata();
}

$postInsertIds = array();
$idGen = $this->_class->idGenerator;
$isPostInsertId = $idGen->isPostInsertGenerator();
Expand Down Expand Up @@ -176,8 +172,8 @@ public function executeInserts()
$stmt->closeCursor();
}

if (isset($versionedClass)) {
$this->_assignDefaultVersionValue($versionedClass, $entity, $id);
if ($this->_class->isVersioned) {
$this->assignDefaultVersionValue($entity, $id);
}

$this->_queuedInserts = array();
Expand Down Expand Up @@ -207,7 +203,7 @@ public function update($entity)
$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);
}
}
}
Expand Down Expand Up @@ -419,4 +415,13 @@ protected function _getInsertColumnList()

return $columns;
}

/**
* {@inheritdoc}
*/
protected function assignDefaultVersionValue($entity, $id)
{
$value = $this->fetchVersionValue($this->_getVersionedClassMetadata(), $id);
$this->_class->setFieldValue($entity, $this->_class->versionField, $value);
}
}
92 changes: 92 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC960Test.php
@@ -0,0 +1,92 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;

require_once __DIR__ . '/../../../TestInit.php';

class DDC960Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_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;
}
}

0 comments on commit 837d3f0

Please sign in to comment.