From 496f9a01760909180eb1fe3ee0ba19f294cc1657 Mon Sep 17 00:00:00 2001 From: Benjamin Morel Date: Fri, 17 Jan 2014 11:03:07 +0000 Subject: [PATCH] Avoid PersistentCollection::isEmpty() to fully load the collection on extra lazy fetch. --- lib/Doctrine/ORM/PersistentCollection.php | 6 +-- .../Functional/PersistentCollectionTest.php | 39 +++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 8d0fef75786..532a12aa129 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -118,7 +118,7 @@ final class PersistentCollection implements Collection, Selectable * * @param EntityManager $em The EntityManager the collection will be associated with. * @param ClassMetadata $class The class descriptor of the entity type of this collection. - * @param array $coll The collection elements. + * @param Collection $coll The collection elements. */ public function __construct(EntityManager $em, $class, $coll) { @@ -599,9 +599,7 @@ public function add($value) */ public function isEmpty() { - $this->initialize(); - - return $this->coll->isEmpty(); + return $this->coll->isEmpty() && $this->count() === 0; } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php b/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php index 2125966fce3..375f7d3f9f7 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php @@ -41,6 +41,34 @@ public function testPersist() $this->assertEquals(2, $collectionHolder->getCollection()->count()); } + /** + * Tests that PersistentCollection::isEmpty() does not initialize the collection when FETCH_EXTRA_LAZY is used. + */ + public function testExtraLazyIsEmptyDoesNotInitializeCollection() + { + $collectionHolder = new PersistentCollectionHolder(); + + $this->_em->persist($collectionHolder); + $this->_em->flush(); + $this->_em->clear(); + + $collectionHolder = $this->_em->find(__NAMESPACE__ . '\PersistentCollectionHolder', $collectionHolder->getId()); + $collection = $collectionHolder->getRawCollection(); + + $this->assertTrue($collection->isEmpty()); + $this->assertFalse($collection->isInitialized()); + + $collectionHolder->addElement(new PersistentCollectionContent()); + + $this->_em->flush(); + $this->_em->clear(); + + $collectionHolder = $this->_em->find(__NAMESPACE__ . '\PersistentCollectionHolder', $collectionHolder->getId()); + $collection = $collectionHolder->getRawCollection(); + + $this->assertFalse($collection->isEmpty()); + $this->assertFalse($collection->isInitialized()); + } } /** @@ -56,7 +84,7 @@ class PersistentCollectionHolder extends PersistentObject /** * @var \Doctrine\Common\Collections\Collection - * @ManyToMany(targetEntity="PersistentCollectionContent", cascade={"all"}) + * @ManyToMany(targetEntity="PersistentCollectionContent", cascade={"all"}, fetch="EXTRA_LAZY") */ protected $collection; @@ -81,6 +109,13 @@ public function getCollection() return clone $this->collection; } + /** + * @return \Doctrine\Common\Collections\Collection + */ + public function getRawCollection() + { + return $this->collection; + } } /** @@ -88,11 +123,9 @@ public function getCollection() */ class PersistentCollectionContent extends PersistentObject { - /** * @Id @Column(type="integer") @GeneratedValue * @var int */ protected $id; - }