1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Avoid PersistentCollection::isEmpty() to fully load the collection on extra lazy fetch.

This commit is contained in:
Benjamin Morel 2014-01-17 11:03:07 +00:00
parent 7ceb9b0b50
commit 496f9a0176
2 changed files with 38 additions and 7 deletions

View File

@ -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 @@ final class PersistentCollection implements Collection, Selectable
*/
public function isEmpty()
{
$this->initialize();
return $this->coll->isEmpty();
return $this->coll->isEmpty() && $this->count() === 0;
}
/**

View File

@ -41,6 +41,34 @@ class PersistentCollectionTest extends \Doctrine\Tests\OrmFunctionalTestCase
$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 @@ class PersistentCollectionHolder extends PersistentObject
return clone $this->collection;
}
/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getRawCollection()
{
return $this->collection;
}
}
/**
@ -88,11 +123,9 @@ class PersistentCollectionHolder extends PersistentObject
*/
class PersistentCollectionContent extends PersistentObject
{
/**
* @Id @Column(type="integer") @GeneratedValue
* @var int
*/
protected $id;
}