Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge entity associated to versioned entity #1573

Merged
merged 4 commits into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
}
}

if ($class->isVersioned) {
if ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity)) {
$reflField = $class->reflFields[$class->versionField];
$managedCopyVersion = $reflField->getValue($managedCopy);
$entityVersion = $reflField->getValue($entity);
Expand All @@ -1879,7 +1879,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass

$visited[$oid] = $managedCopy; // mark visited

if (!($entity instanceof Proxy && ! $entity->__isInitialized())) {
if ($this->isLoaded($entity)) {
if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized()) {
$managedCopy->__load();
}
Expand All @@ -1904,6 +1904,18 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
return $managedCopy;
}

/**
* Tests if an entity is loaded (Not a proxy or a non initialized proxy)
*
* @param $entity
*
* @return bool
*/
private function isLoaded($entity)
{
return !($entity instanceof Proxy) || $entity->__isInitialized();
}

/**
* Sets/adds associated managed copies into the previous entity's association field
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a public $tags earlier on?

}
}
45 changes: 45 additions & 0 deletions tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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;

/**
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove this empty line

* @group MergeVersionedOneToMany
*/
class MergeVersionedOneToManyTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();

try {
$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata('Doctrine\Tests\Models\VersionedOneToMany\Category'),
$this->_em->getClassMetadata('Doctrine\Tests\Models\VersionedOneToMany\Article'),
]
);
} catch (ORMException $e) {
}
}

/**
* This test case asserts that a detached and unmodified entity could be merge without firing
* OptimisticLockException.
*/
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();

$articleMerged = $this->_em->merge($article);

$articleMerged->name = 'Article Merged';

$this->_em->flush();
$this->assertEquals(2, $articleMerged->version);
}
}