Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid PersistentCollection::isEmpty() to fully load the collection. #912

Merged
merged 1 commit into from
Feb 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -599,9 +599,7 @@ public function add($value)
*/
public function isEmpty()
{
$this->initialize();

return $this->coll->isEmpty();
return $this->coll->isEmpty() && $this->count() === 0;
}

/**
Expand Down
39 changes: 36 additions & 3 deletions tests/Doctrine/Tests/ORM/Functional/PersistentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand All @@ -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;

Expand All @@ -81,18 +109,23 @@ public function getCollection()
return clone $this->collection;
}

/**
* @return \Doctrine\Common\Collections\Collection
*/
public function getRawCollection()
{
return $this->collection;
}
}

/**
* @Entity
*/
class PersistentCollectionContent extends PersistentObject
{

/**
* @Id @Column(type="integer") @GeneratedValue
* @var int
*/
protected $id;

}