Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Functional;
|
|
|
|
|
|
|
|
|
2015-01-17 07:58:35 +01:00
|
|
|
use Doctrine\DBAL\DriverManager;
|
2015-01-16 22:12:42 +01:00
|
|
|
use Doctrine\DBAL\Logging\DebugStack;
|
|
|
|
use Doctrine\DBAL\Logging\SQLLogger;
|
|
|
|
use Doctrine\ORM\Configuration;
|
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\Tools\SchemaTool;
|
2015-01-16 22:46:31 +01:00
|
|
|
use Doctrine\Tests\Models\Generic\DateTimeModel;
|
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
|
2015-01-16 22:47:00 +01:00
|
|
|
class MergeProxiesTest extends OrmFunctionalTestCase
|
2015-01-16 22:46:31 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
protected function setUp()
|
|
|
|
{
|
2015-01-17 07:58:35 +01:00
|
|
|
$this->useModelSet('generic');
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
|
2015-01-17 07:58:35 +01:00
|
|
|
parent::setUp();
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 22:43:52 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*/
|
2015-01-16 21:14:13 +01:00
|
|
|
public function testMergeDetachedUnInitializedProxy()
|
|
|
|
{
|
2015-01-16 22:46:31 +01:00
|
|
|
$detachedUninitialized = $this->_em->getReference(DateTimeModel::CLASSNAME, 123);
|
2015-01-16 21:14:13 +01:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$managed = $this->_em->getReference(DateTimeModel::CLASSNAME, 123);
|
2015-01-16 21:14:13 +01:00
|
|
|
|
|
|
|
$this->assertSame($managed, $this->_em->merge($detachedUninitialized));
|
|
|
|
|
|
|
|
$this->assertFalse($managed->__isInitialized());
|
|
|
|
$this->assertFalse($detachedUninitialized->__isInitialized());
|
|
|
|
}
|
|
|
|
|
2015-01-16 22:43:52 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*/
|
2015-01-16 21:09:53 +01:00
|
|
|
public function testMergeUnserializedUnInitializedProxy()
|
|
|
|
{
|
2015-01-16 22:46:31 +01:00
|
|
|
$detachedUninitialized = $this->_em->getReference(DateTimeModel::CLASSNAME, 123);
|
2015-01-16 21:09:53 +01:00
|
|
|
|
|
|
|
$this->_em->clear();
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$managed = $this->_em->getReference(DateTimeModel::CLASSNAME, 123);
|
2015-01-16 21:14:13 +01:00
|
|
|
|
2015-01-16 21:09:53 +01:00
|
|
|
$this->assertSame(
|
2015-01-16 21:14:13 +01:00
|
|
|
$managed,
|
|
|
|
$this->_em->merge(unserialize(serialize($this->_em->merge($detachedUninitialized))))
|
2015-01-16 21:09:53 +01:00
|
|
|
);
|
2015-01-16 21:14:13 +01:00
|
|
|
|
|
|
|
$this->assertFalse($managed->__isInitialized());
|
|
|
|
$this->assertFalse($detachedUninitialized->__isInitialized());
|
2015-01-16 21:09:53 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 22:43:52 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*/
|
2015-01-16 21:15:39 +01:00
|
|
|
public function testMergeManagedProxy()
|
|
|
|
{
|
2015-01-16 22:46:31 +01:00
|
|
|
$managed = $this->_em->getReference(DateTimeModel::CLASSNAME, 123);
|
2015-01-16 21:15:39 +01:00
|
|
|
|
|
|
|
$this->assertSame($managed, $this->_em->merge($managed));
|
|
|
|
|
|
|
|
$this->assertFalse($managed->__isInitialized());
|
|
|
|
}
|
|
|
|
|
2015-01-20 15:48:00 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*
|
|
|
|
* Bug discovered while working on DDC-2704 - merging towards un-initialized proxies does not initialize them,
|
|
|
|
* causing merged data to be lost when they are actually initialized
|
|
|
|
*/
|
|
|
|
public function testMergeWithExistingUninitializedManagedProxy()
|
|
|
|
{
|
|
|
|
$date = new DateTimeModel();
|
|
|
|
|
|
|
|
$this->_em->persist($date);
|
|
|
|
$this->_em->flush($date);
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$managed = $this->_em->getReference(DateTimeModel::CLASSNAME, $date->id);
|
|
|
|
|
|
|
|
$this->assertInstanceOf('Doctrine\Common\Proxy\Proxy', $managed);
|
|
|
|
$this->assertFalse($managed->__isInitialized());
|
|
|
|
|
|
|
|
$date->date = $dateTime = new \DateTime();
|
|
|
|
|
|
|
|
$this->assertSame($managed, $this->_em->merge($date));
|
|
|
|
$this->assertTrue($managed->__isInitialized());
|
|
|
|
$this->assertSame($dateTime, $managed->date, 'Data was merged into the proxy after initialization');
|
|
|
|
}
|
|
|
|
|
2015-01-16 22:43:52 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*/
|
2015-01-16 22:20:58 +01:00
|
|
|
public function testMergingProxyFromDifferentEntityManagerWithExistingManagedInstanceDoesNotReplaceInitializer()
|
2015-01-16 22:12:42 +01:00
|
|
|
{
|
|
|
|
$em1 = $this->createEntityManager($logger1 = new DebugStack());
|
|
|
|
$em2 = $this->createEntityManager($logger2 = new DebugStack());
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$file1 = new DateTimeModel();
|
|
|
|
$file2 = new DateTimeModel();
|
2015-01-16 22:12:42 +01:00
|
|
|
|
|
|
|
$em1->persist($file1);
|
2015-01-16 22:20:58 +01:00
|
|
|
$em2->persist($file2);
|
2015-01-16 22:12:42 +01:00
|
|
|
$em1->flush();
|
2015-01-16 22:20:58 +01:00
|
|
|
$em2->flush();
|
2015-01-16 22:12:42 +01:00
|
|
|
$em1->clear();
|
2015-01-16 22:20:58 +01:00
|
|
|
$em2->clear();
|
2015-01-16 22:12:42 +01:00
|
|
|
|
|
|
|
$queryCount1 = count($logger1->queries);
|
2015-01-16 22:20:58 +01:00
|
|
|
$queryCount2 = count($logger2->queries);
|
2015-01-16 22:12:42 +01:00
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$proxy1 = $em1->getReference(DateTimeModel::CLASSNAME, $file1->id);
|
|
|
|
$proxy2 = $em2->getReference(DateTimeModel::CLASSNAME, $file1->id);
|
2015-01-16 22:20:58 +01:00
|
|
|
$merged2 = $em2->merge($proxy1);
|
2015-01-16 22:12:42 +01:00
|
|
|
|
|
|
|
$this->assertNotSame($proxy1, $merged2);
|
|
|
|
$this->assertSame($proxy2, $merged2);
|
|
|
|
|
|
|
|
$this->assertFalse($proxy1->__isInitialized());
|
|
|
|
$this->assertFalse($proxy2->__isInitialized());
|
|
|
|
|
|
|
|
$proxy1->__load();
|
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount1 + 1,
|
|
|
|
$logger1->queries,
|
|
|
|
'Loading the first proxy was done through the first entity manager'
|
|
|
|
);
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount2,
|
|
|
|
$logger2->queries,
|
|
|
|
'No queries were executed on the second entity manager, as it is unrelated with the first proxy'
|
|
|
|
);
|
|
|
|
|
|
|
|
$proxy2->__load();
|
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount1 + 1,
|
|
|
|
$logger1->queries,
|
|
|
|
'Loading the second proxy does not affect the first entity manager'
|
|
|
|
);
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount2 + 1,
|
|
|
|
$logger2->queries,
|
|
|
|
'Loading of the second proxy instance was done through the second entity manager'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-16 22:43:52 +01:00
|
|
|
/**
|
|
|
|
* @group DDC-1392
|
|
|
|
* @group DDC-1734
|
|
|
|
* @group DDC-3368
|
|
|
|
* @group #1172
|
|
|
|
*/
|
2015-01-16 22:25:00 +01:00
|
|
|
public function testMergingUnInitializedProxyDoesNotInitializeIt()
|
|
|
|
{
|
|
|
|
$em1 = $this->createEntityManager($logger1 = new DebugStack());
|
|
|
|
$em2 = $this->createEntityManager($logger2 = new DebugStack());
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$file1 = new DateTimeModel();
|
|
|
|
$file2 = new DateTimeModel();
|
2015-01-16 22:25:00 +01:00
|
|
|
|
|
|
|
$em1->persist($file1);
|
|
|
|
$em2->persist($file2);
|
|
|
|
$em1->flush();
|
|
|
|
$em2->flush();
|
|
|
|
$em1->clear();
|
|
|
|
$em2->clear();
|
|
|
|
|
|
|
|
$queryCount1 = count($logger1->queries);
|
|
|
|
$queryCount2 = count($logger1->queries);
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$unManagedProxy = $em1->getReference(DateTimeModel::CLASSNAME, $file1->id);
|
2015-01-16 22:25:00 +01:00
|
|
|
$mergedInstance = $em2->merge($unManagedProxy);
|
|
|
|
|
|
|
|
$this->assertNotInstanceOf('Doctrine\Common\Proxy\Proxy', $mergedInstance);
|
|
|
|
$this->assertNotSame($unManagedProxy, $mergedInstance);
|
|
|
|
$this->assertFalse($unManagedProxy->__isInitialized());
|
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount1,
|
|
|
|
$logger1->queries,
|
|
|
|
'Loading the merged instance affected only the first entity manager'
|
|
|
|
);
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount1 + 1,
|
|
|
|
$logger2->queries,
|
|
|
|
'Loading the merged instance was done via the second entity manager'
|
|
|
|
);
|
|
|
|
|
|
|
|
$unManagedProxy->__load();
|
|
|
|
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount1 + 1,
|
|
|
|
$logger1->queries,
|
|
|
|
'Loading the first proxy was done through the first entity manager'
|
|
|
|
);
|
|
|
|
$this->assertCount(
|
|
|
|
$queryCount2 + 1,
|
|
|
|
$logger2->queries,
|
|
|
|
'No queries were executed on the second entity manager, as it is unrelated with the first proxy'
|
|
|
|
);
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 22:12:42 +01:00
|
|
|
/**
|
|
|
|
* @param SQLLogger $logger
|
|
|
|
*
|
|
|
|
* @return EntityManager
|
|
|
|
*/
|
|
|
|
private function createEntityManager(SQLLogger $logger)
|
|
|
|
{
|
|
|
|
$config = new Configuration();
|
|
|
|
|
2015-01-16 22:46:31 +01:00
|
|
|
$config->setProxyDir(realpath(__DIR__ . '/../../Proxies'));
|
2015-01-16 22:12:42 +01:00
|
|
|
$config->setProxyNamespace('Doctrine\Tests\Proxies');
|
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(
|
|
|
|
array(realpath(__DIR__ . '/../../Models/Cache')),
|
|
|
|
true
|
|
|
|
));
|
2015-01-18 02:32:02 +01:00
|
|
|
$config->setSQLLogger($logger);
|
2015-01-16 22:12:42 +01:00
|
|
|
|
2015-01-17 07:58:35 +01:00
|
|
|
// always runs on sqlite to prevent multi-connection race-conditions with the test suite
|
|
|
|
// multi-connection is not relevant for the purpose of checking locking here, but merely
|
|
|
|
// to stub out DB-level access and intercept it
|
2015-01-18 02:32:02 +01:00
|
|
|
$connection = DriverManager::getConnection(
|
|
|
|
array(
|
|
|
|
'driver' => 'pdo_sqlite',
|
|
|
|
'memory' => true
|
|
|
|
),
|
|
|
|
$config
|
|
|
|
);
|
2015-01-16 22:12:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
$entityManager = EntityManager::create($connection, $config);
|
|
|
|
|
2015-01-18 02:32:02 +01:00
|
|
|
(new SchemaTool($entityManager))->createSchema([$entityManager->getClassMetadata(DateTimeModel::CLASSNAME)]);
|
2015-01-16 22:12:42 +01:00
|
|
|
|
|
|
|
return $entityManager;
|
|
|
|
}
|
Don't load detached proxies when merging them.
Ticket DDC-1392 fixed an issue where uninitialized proxies could not be merged
because the merge routine couldn't get the identifier from them. The soution
was to initialize the proxy.
Ticket DDC-1734 fixed the merging of *unserialized* uninitialized proxies by
resetting their internals, so these proxies were able to initialize, as required
by the fix for DDC-1392.
Somehow, in the meanwhile, the fix for DDC-1392 is not needed anymore:
reverting the patch will not break the associated test (but it does break the
test for DDC-1734). This means it is not needed anymore to initialize the proxy
when merging.
Uninitialized proxies that get merged should not be loaded at all. Since they
are not initialized, the entity data for sure hasn't changed, so it can be
safely ignored. Actually, the only thing the data is needed for while merging,
is to copy it into the managed entity, but that one is already supposed to be
up to date. By not initializing the proxy, a potential database roundtrip is
saved, and the fix for DDC-1734 is not needed anymore.
Besides optimizing the merge, this patch also solves an issue with merging.
Currently, when a detached uninitialized proxy is merged while there is already a
corresponding managed entity (proxy or not), the ORM returns a blank entity
instead of returning the already managed entity. This patch makes sure that
already existing managed entities are re-used.
2014-11-06 07:16:20 +01:00
|
|
|
}
|