Avoid PersistentCollection::isEmpty() to fully load the collection on extra lazy fetch.
This commit is contained in:
parent
7ceb9b0b50
commit
496f9a0176
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user