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 2 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
14 changes: 13 additions & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
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
47 changes: 47 additions & 0 deletions tests/Doctrine/Tests/Models/VersionedOneToMany/Article.php
@@ -0,0 +1,47 @@
<?php

namespace Doctrine\Tests\Models\VersionedOneToMany;

use Doctrine\Common\Collections\ArrayCollection;

/**
*
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

* @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.
*
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

*/
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?

}
}
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Models/VersionedOneToMany/Category.php
@@ -0,0 +1,49 @@
<?php

namespace Doctrine\Tests\Models\VersionedOneToMany;

use Doctrine\Common\Collections\ArrayCollection;

/**
*
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

* @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.
*
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

*/
public function __construct()
{
$this->articles = 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.

Please, remove this empty lines too

}
@@ -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 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
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 excess whitespace in beginning of this comment.

Copy link
Author

Choose a reason for hiding this comment

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

All white-spaces & lines are fixed. And I modified the comment as it is here just by a copy paste.

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