Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Fix DDC-2409
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioBatSilva committed Apr 28, 2013
1 parent d513e0f commit 5cdc73e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ private function doMerge($entity, array &$visited, $prevManagedCopy = null, $ass
// do not merge fields marked lazy that have not been fetched.
continue;
} else if ( ! $assoc2['isCascadeMerge']) {
if ($this->getEntityState($other, self::STATE_DETACHED) !== self::STATE_MANAGED) {
if (($otherState = $this->getEntityState($other)) !== self::STATE_MANAGED && $otherState !== self::STATE_NEW) {
$targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
$relatedId = $targetClass->getIdentifierValues($other);

Expand Down
72 changes: 72 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2409Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsArticle;

/**
* @group DDC-2409
*/
class DDC2409Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}

public function testIssue()
{
$em = $this->_em;
$uow = $em->getUnitOfWork();

$originalArticle = new CmsArticle();
$originalUser = new CmsUser();

$originalArticle->topic = 'Unit Test';
$originalArticle->text = 'How to write a test';

$originalUser->name = 'Doctrine Bot';
$originalUser->username = 'DoctrineBot';
$originalUser->status = 'active';

$originalUser->addArticle($originalArticle);

$em->persist($originalUser);
$em->persist($originalArticle);
$em->flush();
$em->clear();

$article = $em->find('Doctrine\Tests\Models\CMS\CmsArticle', $originalArticle->id);
$user = new CmsUser();

$user->name = 'Doctrine Bot 2.0';
$user->username = 'BotDoctrine2';
$user->status = 'new';

$article->setAuthor($user);

$this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalArticle));
$this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($originalUser));
$this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($article));
$this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user));

$em->detach($user);
$em->detach($article);

$userMerged = $em->merge($user);
$articleMerged = $em->merge($article);

$this->assertEquals(UnitOfWork::STATE_NEW, $uow->getEntityState($user));
$this->assertEquals(UnitOfWork::STATE_DETACHED, $uow->getEntityState($article));
$this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($userMerged));
$this->assertEquals(UnitOfWork::STATE_MANAGED, $uow->getEntityState($articleMerged));

$this->assertNotSame($user, $userMerged);
$this->assertNotSame($article, $articleMerged);
$this->assertNotSame($userMerged, $articleMerged->user);
$this->assertSame($user, $articleMerged->user);
}
}

0 comments on commit 5cdc73e

Please sign in to comment.