Unit test & fix for merge versionned entity
This commit is contained in:
parent
65f5777e60
commit
216c466233
@ -1870,7 +1870,7 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($class->isVersioned) {
|
if ($class->isVersioned && !($this->isNotInitializedProxy($managedCopy) || $this->isNotInitializedProxy($entity))) {
|
||||||
$reflField = $class->reflFields[$class->versionField];
|
$reflField = $class->reflFields[$class->versionField];
|
||||||
$managedCopyVersion = $reflField->getValue($managedCopy);
|
$managedCopyVersion = $reflField->getValue($managedCopy);
|
||||||
$entityVersion = $reflField->getValue($entity);
|
$entityVersion = $reflField->getValue($entity);
|
||||||
@ -1908,6 +1908,18 @@ class UnitOfWork implements PropertyChangedListener
|
|||||||
return $managedCopy;
|
return $managedCopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if an entity is a non initialized proxy class
|
||||||
|
*
|
||||||
|
* @param $entity
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isNotInitializedProxy($entity)
|
||||||
|
{
|
||||||
|
return $entity instanceof Proxy && !$entity->__isInitialized();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets/adds associated managed copies into the previous entity's association field
|
* Sets/adds associated managed copies into the previous entity's association field
|
||||||
*
|
*
|
||||||
|
47
tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php
Normal file
47
tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\Models\VersionedOneToMany;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Entity
|
||||||
|
* @Table(name="article")
|
||||||
|
*/
|
||||||
|
class Article
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(name="id", type="integer")
|
||||||
|
* @GeneratedValue(strategy="AUTO")
|
||||||
|
*/
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(name="name")
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToOne(targetEntity="Category", inversedBy="category", cascade={"merge", "persist"})
|
||||||
|
*/
|
||||||
|
public $category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Version column
|
||||||
|
*
|
||||||
|
* @Column(type="integer", name="version")
|
||||||
|
* @Version
|
||||||
|
*/
|
||||||
|
public $version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Category constructor.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->tags = new ArrayCollection();
|
||||||
|
}
|
||||||
|
}
|
49
tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php
Normal file
49
tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\Models\VersionedOneToMany;
|
||||||
|
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Entity
|
||||||
|
* @Table(name="category")
|
||||||
|
*/
|
||||||
|
class Category
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(name="id", type="integer")
|
||||||
|
* @GeneratedValue(strategy="AUTO")
|
||||||
|
*/
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OneToMany(targetEntity="Article", mappedBy="category", cascade={"merge", "persist"})
|
||||||
|
*/
|
||||||
|
public $articles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(name="name")
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Version column
|
||||||
|
*
|
||||||
|
* @Column(type="integer", name="version")
|
||||||
|
* @Version
|
||||||
|
*/
|
||||||
|
public $version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Category constructor.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->articles = new ArrayCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\ORM\Functional;
|
||||||
|
|
||||||
|
use Doctrine\ORM\OptimisticLockException;
|
||||||
|
use Doctrine\ORM\ORMException;
|
||||||
|
use Doctrine\Tests\Models\VersionedOneToMany\Article;
|
||||||
|
use Doctrine\Tests\Models\VersionedOneToMany\Category;
|
||||||
|
use Doctrine\Tests\Models\VersionedOneToMany\Tag;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @group MergeVersionedOneToMany
|
||||||
|
*/
|
||||||
|
class MergeVersionedOneToManyTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->_schemaTool->createSchema(
|
||||||
|
[
|
||||||
|
$this->_em->getClassMetadata(Category::class),
|
||||||
|
$this->_em->getClassMetadata(Article::class),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
} catch (ORMException $e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test case tests that a versionable entity, that has a oneToOne relationship as it's id can be created
|
||||||
|
* without this bug fix (DDC-3318), you could not do this
|
||||||
|
*/
|
||||||
|
public function testSetVersionOnCreate()
|
||||||
|
{
|
||||||
|
$category = new Category();
|
||||||
|
$category->name = 'Category';
|
||||||
|
|
||||||
|
$article = new Article();
|
||||||
|
$article->name = 'Article';
|
||||||
|
$article->category = $category;
|
||||||
|
|
||||||
|
$this->_em->persist($article);
|
||||||
|
$this->_em->flush();
|
||||||
|
$this->_em->clear();
|
||||||
|
|
||||||
|
$mergeSucceed = false;
|
||||||
|
try {
|
||||||
|
$articleMerged = $this->_em->merge($article);
|
||||||
|
$mergeSucceed = true;
|
||||||
|
} catch (OptimisticLockException $e) {
|
||||||
|
}
|
||||||
|
$this->assertTrue($mergeSucceed);
|
||||||
|
|
||||||
|
$articleMerged->name = 'Article Merged';
|
||||||
|
|
||||||
|
$flushSucceed = false;
|
||||||
|
try {
|
||||||
|
$this->_em->flush();
|
||||||
|
$flushSucceed = true;
|
||||||
|
} catch (OptimisticLockException $e) {
|
||||||
|
}
|
||||||
|
$this->assertTrue($flushSucceed);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user