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

Can cache empty collections #950

Merged
merged 5 commits into from
Feb 12, 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
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Cache/Persister/AbstractEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public function loadManyToManyCollection(array $assoc, $sourceEntity, Persistent

$list = $this->persister->loadManyToManyCollection($assoc, $sourceEntity, $coll);

if ($hasCache && ! empty($list)) {
if ($hasCache) {
$persister->storeCollectionCache($key, $list);

if ($this->cacheLogger) {
Expand Down Expand Up @@ -543,7 +543,7 @@ public function loadOneToManyCollection(array $assoc, $sourceEntity, PersistentC

$list = $this->persister->loadOneToManyCollection($assoc, $sourceEntity, $coll);

if ($hasCache && ! empty($list)) {
if ($hasCache) {
$persister->storeCollectionCache($key, $list);

if ($this->cacheLogger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function loadFixturesTraveler()

$this->_em->flush();
}

protected function loadFixturesTravelersWithProfile()
{
$t1 = new Traveler("Test traveler 1");
Expand All @@ -139,7 +139,7 @@ protected function loadFixturesTravelersWithProfile()

$this->travelersWithProfile[] = $t1;
$this->travelersWithProfile[] = $t2;

$this->_em->flush();
}

Expand All @@ -165,6 +165,7 @@ protected function loadFixturesTravels()
{
$t1 = new Travel($this->travelers[0]);
$t2 = new Travel($this->travelers[1]);
$t3 = new Travel($this->travelers[1]);

$t1->addVisitedCity($this->cities[0]);
$t1->addVisitedCity($this->cities[1]);
Expand All @@ -175,9 +176,11 @@ protected function loadFixturesTravels()

$this->_em->persist($t1);
$this->_em->persist($t2);
$this->_em->persist($t3);

$this->travels[] = $t1;
$this->travels[] = $t2;
$this->travels[] = $t3;

$this->_em->flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function testStoreManyToManyAssociationWhitCascade()
$this->_em->flush();
$this->_em->clear();

$this->assertTrue($this->cache->containsEntity(Traveler::CLASSNAME, $travel->getId()));
$this->assertTrue($this->cache->containsEntity(Travel::CLASSNAME, $travel->getId()));
$this->assertTrue($this->cache->containsEntity(Traveler::CLASSNAME, $traveler->getId()));
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $this->cities[0]->getId()));
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $this->cities[1]->getId()));
Expand Down Expand Up @@ -214,4 +214,33 @@ public function testReadOnlyCollection()
$this->_em->persist($travel);
$this->_em->flush();
}

public function testManyToManyWithEmptyRelation()
{
$this->loadFixturesCountries();
$this->loadFixturesStates();
$this->loadFixturesCities();
$this->loadFixturesTraveler();
$this->loadFixturesTravels();
$this->_em->clear();

$this->evictRegions();

$queryCount = $this->getCurrentQueryCount();

$entitiId = $this->travels[2]->getId(); //empty travel
$entity = $this->_em->find(Travel::CLASSNAME, $entitiId);

$this->assertEquals(0, $entity->getVisitedCities()->count());
$this->assertEquals($queryCount+2, $this->getCurrentQueryCount());

$this->_em->clear();

$entity = $this->_em->find(Travel::CLASSNAME, $entitiId);

$queryCount = $this->getCurrentQueryCount();
$this->assertEquals(0, $entity->getVisitedCities()->count());
$this->assertEquals($queryCount, $this->getCurrentQueryCount());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testPutAndLoadOneToManyRelation()

$s3 = $this->_em->find(State::CLASSNAME, $this->states[0]->getId());
$s4 = $this->_em->find(State::CLASSNAME, $this->states[1]->getId());

//trigger lazy load from cache
$this->assertCount(2, $s3->getCities());
$this->assertCount(2, $s4->getCities());
Expand Down Expand Up @@ -133,12 +133,12 @@ public function testLoadOnoToManyCollectionFromDatabaseWhenEntityMissing()

//trigger lazy load from database
$this->assertCount(2, $this->_em->find(State::CLASSNAME, $this->states[0]->getId())->getCities());

$this->assertTrue($this->cache->containsEntity(State::CLASSNAME, $this->states[0]->getId()));
$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, 'cities', $this->states[0]->getId()));
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $this->states[0]->getCities()->get(0)->getId()));
$this->assertTrue($this->cache->containsEntity(City::CLASSNAME, $this->states[0]->getCities()->get(1)->getId()));

$queryCount = $this->getCurrentQueryCount();
$stateId = $this->states[0]->getId();
$state = $this->_em->find(State::CLASSNAME, $stateId);
Expand Down Expand Up @@ -284,6 +284,35 @@ public function testOneToManyRemove()
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionHitCount($this->getCollectionRegion(State::CLASSNAME, 'cities')));
}

public function testOneToManyWithEmptyRelation()
{
$this->loadFixturesCountries();
$this->loadFixturesStates();
$this->loadFixturesCities();

$this->secondLevelCacheLogger->clearStats();
$this->cache->evictEntityRegion(City::CLASSNAME);
$this->cache->evictEntityRegion(State::CLASSNAME);
$this->cache->evictCollectionRegion(State::CLASSNAME, 'cities');
$this->_em->clear();

$entitiId = $this->states[2]->getId(); // bavaria (cities count = 0)
$queryCount = $this->getCurrentQueryCount();
$entity = $this->_em->find(State::CLASSNAME, $entitiId);

$this->assertEquals(0, $entity->getCities()->count());
$this->assertEquals($queryCount + 2, $this->getCurrentQueryCount());

$this->_em->clear();

$queryCount = $this->getCurrentQueryCount();
$entity = $this->_em->find(State::CLASSNAME, $entitiId);

$this->assertEquals(0, $entity->getCities()->count());
$this->assertEquals($queryCount, $this->getCurrentQueryCount());

}

public function testOneToManyCount()
{
$this->loadFixturesCountries();
Expand Down