1
0
mirror of synced 2025-01-19 06:51:40 +03:00

Only create lazy collection for EXTRA_LAZY

This commit is contained in:
Michaël Gallego 2014-02-17 21:23:11 +01:00 committed by Marco Pivetta
parent 50832fd3bc
commit 11de4c2e72
2 changed files with 32 additions and 32 deletions

View File

@ -883,7 +883,7 @@ final class PersistentCollection implements Collection, Selectable
$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']); $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);
if ($this->association['fetch'] !== ClassMetadataInfo::FETCH_EAGER) { if ($this->association['fetch'] === ClassMetadataInfo::FETCH_EXTRA_LAZY) {
return new LazyCriteriaCollection($persister, $criteria); return new LazyCriteriaCollection($persister, $criteria);
} else { } else {
return new ArrayCollection($persister->loadCriteria($criteria)); return new ArrayCollection($persister->loadCriteria($criteria));

View File

@ -21,6 +21,10 @@ namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Criteria;
use Doctrine\Tests\Models\CMS\CmsArticle; use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsComment; use Doctrine\Tests\Models\CMS\CmsComment;
use Doctrine\Tests\Models\Company\CompanyAuction;
use Doctrine\Tests\Models\Company\CompanyEmployee;
use Doctrine\Tests\Models\Company\CompanyFlexUltraContract;
use Doctrine\Tests\Models\Company\CompanyOrganization;
/** /**
* @author Michaël Gallego <mic.gallego@gmail.com> * @author Michaël Gallego <mic.gallego@gmail.com>
@ -29,7 +33,7 @@ class PersistentCollectionCriteriaTest extends \Doctrine\Tests\OrmFunctionalTest
{ {
protected function setUp() protected function setUp()
{ {
$this->useModelSet('cms'); $this->useModelSet('company');
parent::setUp(); parent::setUp();
} }
@ -43,28 +47,24 @@ class PersistentCollectionCriteriaTest extends \Doctrine\Tests\OrmFunctionalTest
public function loadFixture() public function loadFixture()
{ {
$article = new CmsArticle(); $companyOrganization = new CompanyOrganization();
$article->topic = 'Criteria is awesome'; $this->_em->persist($companyOrganization);
$article->text = 'foo';
$this->_em->persist($article);
$comment1 = new CmsComment(); $event1 = new CompanyAuction();
$comment1->topic = 'I agree'; $event1->setData('Foo');
$comment1->text = 'bar'; $this->_em->persist($event1);
$this->_em->persist($comment1); $companyOrganization->addEvent($event1);
$article->addComment($comment1);
$comment2 = new CmsComment(); $event2 = new CompanyAuction();
$comment2->topic = 'I disagree'; $event2->setData('Bar');
$comment2->text = 'baz'; $this->_em->persist($event2);
$this->_em->persist($comment2); $companyOrganization->addEvent($event2);
$article->addComment($comment2);
$this->_em->flush(); $this->_em->flush();
unset($article); unset($companyOrganization);
unset($comment1); unset($event1);
unset($comment2); unset($event2);
$this->_em->clear(); $this->_em->clear();
} }
@ -72,24 +72,24 @@ class PersistentCollectionCriteriaTest extends \Doctrine\Tests\OrmFunctionalTest
public function testCanCountWithoutLoadingPersistentCollection() public function testCanCountWithoutLoadingPersistentCollection()
{ {
$this->loadFixture(); $this->loadFixture();
$repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsArticle'); $repository = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyOrganization');
$article = $repository->findOneBy(array('topic' => 'Criteria is awesome')); $organization = $repository->find(1);
$comments = $article->comments->matching(new Criteria()); $events = $organization->events->matching(new Criteria());
$this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $comments); $this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $events);
$this->assertFalse($comments->isInitialized()); $this->assertFalse($events->isInitialized());
$this->assertCount(2, $comments); $this->assertCount(2, $events);
$this->assertFalse($comments->isInitialized()); $this->assertFalse($events->isInitialized());
// Make sure it works with constraints // Make sure it works with constraints
$comments = $article->comments->matching(new Criteria( $events = $organization->events->matching(new Criteria(
Criteria::expr()->eq('text', 'bar') Criteria::expr()->eq('id', 2)
)); ));
$this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $comments); $this->assertInstanceOf('Doctrine\ORM\LazyCriteriaCollection', $events);
$this->assertFalse($comments->isInitialized()); $this->assertFalse($events->isInitialized());
$this->assertCount(1, $comments); $this->assertCount(1, $events);
$this->assertFalse($comments->isInitialized()); $this->assertFalse($events->isInitialized());
} }
} }