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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion 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->isNotInitializedProxy($managedCopy) || $this->isNotInitializedProxy($entity))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In conjunction with my other note, I think this conditional would be clearer as:

$class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity)

Copy link
Author

Choose a reason for hiding this comment

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

I totally agree ! It awfully clearer !

I made the changes

$reflField = $class->reflFields[$class->versionField];
$managedCopyVersion = $reflField->getValue($managedCopy);
$entityVersion = $reflField->getValue($entity);
Expand Down Expand Up @@ -1904,6 +1904,18 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
return $managedCopy;
}

/**
* Tests if an entity is a non initialized proxy class
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it may be clearer to flip this function and rewrite it as a positive-question, where you're asking if the entity has any data to work with.

private function isLoaded($entity){
    return !($entity instanceof Proxy) || $entity->__isInitialized();
}

Other possible names that come to mind are hasData() or isInitialized(). (Assuming that non-proxies are considered initialized too.)

*
* @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
*
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);
}
}