1
0
mirror of synced 2025-01-18 22:41:43 +03:00

DDC-952, DDC-734 Add DQL query hint to switch associations from lazy to eager for deferred initialization optimizations.

This commit is contained in:
Benjamin Eberlei 2011-03-08 22:22:54 +01:00
parent 112f9d1480
commit 60eb755fe9
2 changed files with 43 additions and 1 deletions

View File

@ -1936,6 +1936,18 @@ class UnitOfWork implements PropertyChangedListener
$relatedIdHash = implode(' ', $associatedId);
if (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])) {
$newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash];
// if this is an uninitialized proxy, we are deferring eager loads,
// this association is marked as eager fetch, and its an uninitialized proxy (wtf!)
// then we cann append this entity for eager loading!
if (isset($hints['fetchEager'][$class->name][$field]) &&
isset($hints['deferEagerLoad']) &&
!$targetClass->isIdentifierComposite &&
$newValue instanceof Proxy &&
$newValue->__isInitialized__ === false) {
$this->eagerLoadingEntities[$assoc['targetEntity']][] = $relatedIdHash;
}
} else {
if ($targetClass->subClasses) {
// If it might be a subtype, it can not be lazy
@ -1943,7 +1955,7 @@ class UnitOfWork implements PropertyChangedListener
->loadOneToOneEntity($assoc, $entity, null, $associatedId);
} else {
// Deferred eager load only works for single identifier classes
if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) {
if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER || isset($hints['eagerFetch'][$class->name][$field])) {
if (isset($hints['deferEagerLoad']) && !$targetClass->isIdentifierComposite) {
// TODO: Is there a faster approach?
$this->eagerLoadingEntities[$assoc['targetEntity']][] = current($id);

View File

@ -313,4 +313,34 @@ class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->assertTrue($result[0]->user instanceof \Doctrine\ORM\Proxy\Proxy);
$this->assertFalse($result[0]->user->__isInitialized__);
}
/**
* @group DDC-952
*/
public function testEnableFetchEagerMode()
{
for ($i = 0; $i < 10; $i++) {
$article = new CmsArticle;
$article->topic = "dr. dolittle";
$article->text = "Once upon a time ...";
$author = new CmsUser;
$author->name = "anonymous";
$author->username = "anon".$i;
$author->status = "here";
$article->user = $author;
$this->_em->persist($author);
$this->_em->persist($article);
}
$this->_em->flush();
$this->_em->clear();
$articles = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a')
->setHint('eagerFetch', array('Doctrine\Tests\Models\CMS\CmsArticle' => array('user' => true)))
->getResult();
$this->assertEquals(10, count($articles));
foreach ($articles AS $article) {
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $article);
}
}
}