DDC-833 - Fix some nasty bug occouring when re-creating an entity that was a proxy before. Also found another nasty issue with refreshing entity that had an already loaded many-to-many or one-to-many association.
This commit is contained in:
parent
07016f6da5
commit
89d0a52c4f
3 changed files with 113 additions and 7 deletions
|
@ -674,6 +674,9 @@ class BasicEntityPersister
|
||||||
}
|
}
|
||||||
} else if ($value instanceof PersistentCollection && $value->isInitialized()) {
|
} else if ($value instanceof PersistentCollection && $value->isInitialized()) {
|
||||||
$value->setInitialized(false);
|
$value->setInitialized(false);
|
||||||
|
// no matter if dirty or non-dirty entities are already loaded, smoke them out!
|
||||||
|
// the beauty of it being, they are still in the identity map
|
||||||
|
$value->unwrap()->clear();
|
||||||
$newData[$field] = $value;
|
$newData[$field] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1929,14 +1929,12 @@ class UnitOfWork implements PropertyChangedListener
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Inject collection
|
// Inject collection
|
||||||
$reflField = $class->reflFields[$field];
|
$pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection);
|
||||||
$pColl = new PersistentCollection(
|
|
||||||
$this->em, $targetClass,
|
|
||||||
//TODO: getValue might be superfluous once DDC-79 is implemented.
|
|
||||||
$reflField->getValue($entity) ?: new ArrayCollection
|
|
||||||
);
|
|
||||||
$pColl->setOwner($entity, $assoc);
|
$pColl->setOwner($entity, $assoc);
|
||||||
|
|
||||||
|
$reflField = $class->reflFields[$field];
|
||||||
$reflField->setValue($entity, $pColl);
|
$reflField->setValue($entity, $pColl);
|
||||||
|
|
||||||
if ($assoc['fetch'] == ClassMetadata::FETCH_LAZY) {
|
if ($assoc['fetch'] == ClassMetadata::FETCH_LAZY) {
|
||||||
$pColl->setInitialized(false);
|
$pColl->setInitialized(false);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Doctrine\Tests\ORM\Functional;
|
namespace Doctrine\Tests\ORM\Functional;
|
||||||
|
|
||||||
use Doctrine\ORM\Tools\SchemaTool;
|
use Doctrine\ORM\Tools\SchemaTool;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
use Doctrine\Tests\Models\CMS\CmsUser;
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
||||||
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
||||||
use Doctrine\Tests\Models\CMS\CmsAddress;
|
use Doctrine\Tests\Models\CMS\CmsAddress;
|
||||||
|
@ -263,7 +264,111 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
||||||
$this->_em->refresh($user);
|
$this->_em->refresh($user);
|
||||||
$this->assertEquals('developer', $user->status);
|
$this->assertEquals('developer', $user->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-833
|
||||||
|
*/
|
||||||
|
public function testRefreshResetsCollection()
|
||||||
|
{
|
||||||
|
$user = new CmsUser;
|
||||||
|
$user->name = 'Guilherme';
|
||||||
|
$user->username = 'gblanco';
|
||||||
|
$user->status = 'developer';
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph1 = new CmsPhonenumber;
|
||||||
|
$ph1->phonenumber = "12345";
|
||||||
|
$user->addPhonenumber($ph1);
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph2 = new CmsPhonenumber;
|
||||||
|
$ph2->phonenumber = "54321";
|
||||||
|
|
||||||
|
$this->_em->persist($user);
|
||||||
|
$this->_em->persist($ph1);
|
||||||
|
$this->_em->persist($ph2);
|
||||||
|
$this->_em->flush();
|
||||||
|
|
||||||
|
$user->addPhonenumber($ph2);
|
||||||
|
|
||||||
|
$this->assertEquals(2, count($user->phonenumbers));
|
||||||
|
$this->_em->refresh($user);
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($user->phonenumbers));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-833
|
||||||
|
*/
|
||||||
|
public function testDqlRefreshResetsCollection()
|
||||||
|
{
|
||||||
|
$user = new CmsUser;
|
||||||
|
$user->name = 'Guilherme';
|
||||||
|
$user->username = 'gblanco';
|
||||||
|
$user->status = 'developer';
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph1 = new CmsPhonenumber;
|
||||||
|
$ph1->phonenumber = "12345";
|
||||||
|
$user->addPhonenumber($ph1);
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph2 = new CmsPhonenumber;
|
||||||
|
$ph2->phonenumber = "54321";
|
||||||
|
|
||||||
|
$this->_em->persist($user);
|
||||||
|
$this->_em->persist($ph1);
|
||||||
|
$this->_em->persist($ph2);
|
||||||
|
$this->_em->flush();
|
||||||
|
|
||||||
|
$user->addPhonenumber($ph2);
|
||||||
|
|
||||||
|
$this->assertEquals(2, count($user->phonenumbers));
|
||||||
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
|
||||||
|
$user = $this->_em->createQuery($dql)
|
||||||
|
->setParameter(1, $user->id)
|
||||||
|
->setHint(Query::HINT_REFRESH, true)
|
||||||
|
->getSingleResult();
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($user->phonenumbers));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-833
|
||||||
|
*/
|
||||||
|
public function testCreateEntityOfProxy()
|
||||||
|
{
|
||||||
|
$user = new CmsUser;
|
||||||
|
$user->name = 'Guilherme';
|
||||||
|
$user->username = 'gblanco';
|
||||||
|
$user->status = 'developer';
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph1 = new CmsPhonenumber;
|
||||||
|
$ph1->phonenumber = "12345";
|
||||||
|
$user->addPhonenumber($ph1);
|
||||||
|
|
||||||
|
// Add a phonenumber
|
||||||
|
$ph2 = new CmsPhonenumber;
|
||||||
|
$ph2->phonenumber = "54321";
|
||||||
|
|
||||||
|
$this->_em->persist($user);
|
||||||
|
$this->_em->persist($ph1);
|
||||||
|
$this->_em->persist($ph2);
|
||||||
|
$this->_em->flush();
|
||||||
|
$this->_em->clear();
|
||||||
|
|
||||||
|
$userId = $user->id;
|
||||||
|
$user = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
|
||||||
|
|
||||||
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
|
||||||
|
$user = $this->_em->createQuery($dql)
|
||||||
|
->setParameter(1, $userId)
|
||||||
|
->getSingleResult();
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($user->phonenumbers));
|
||||||
|
}
|
||||||
|
|
||||||
public function testAddToCollectionDoesNotInitialize()
|
public function testAddToCollectionDoesNotInitialize()
|
||||||
{
|
{
|
||||||
$user = new CmsUser;
|
$user = new CmsUser;
|
||||||
|
|
Loading…
Add table
Reference in a new issue