diff --git a/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php b/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php index 887d67012..6b850dec8 100644 --- a/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php +++ b/lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php @@ -81,15 +81,20 @@ class DefaultEntityHydrator implements EntityHydrator continue; } - if ( ! isset($assoc['cache']) || ! ($assoc['type'] & ClassMetadata::TO_ONE)) { - $targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']); + if (! ($assoc['type'] & ClassMetadata::TO_ONE)) { + unset($data[$name]); + continue; + } + if ( ! isset($assoc['cache'])) { + $targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']); $associationIds = $this->identifierFlattener->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($data[$name])); unset($data[$name]); foreach ($associationIds as $fieldName => $fieldValue) { $data[$assoc['targetToSourceKeyColumns'][$targetClassMetadata->getColumnName($fieldName)]] = $fieldValue; } + continue; } diff --git a/tests/Doctrine/Tests/Models/Cache/Login.php b/tests/Doctrine/Tests/Models/Cache/Login.php new file mode 100644 index 000000000..e2776c8da --- /dev/null +++ b/tests/Doctrine/Tests/Models/Cache/Login.php @@ -0,0 +1,63 @@ +name = $name; + } + + /** + * @param Token $token + */ + public function setToken(Token $token) + { + $this->token = $token; + } + + public function getId() + { + return $this->id; + } + + public function setId($id) + { + $this->id = $id; + } + + public function getName() + { + return $this->name; + } + + public function setName($nae) + { + $this->name = $nae; + } +} diff --git a/tests/Doctrine/Tests/Models/Cache/Token.php b/tests/Doctrine/Tests/Models/Cache/Token.php index c71f1bf79..7d070b091 100644 --- a/tests/Doctrine/Tests/Models/Cache/Token.php +++ b/tests/Doctrine/Tests/Models/Cache/Token.php @@ -1,6 +1,7 @@ token = $token; + $this->logins = new ArrayCollection(); $this->client = $client; $this->expiresAt = new \DateTime(date('Y-m-d H:i:s', strtotime("+7 day"))); } + /** + * @return array + */ + public function getLogins() + { + return $this->logins; + } + + /** + * @param Login $login + */ + public function addLogin(Login $login) + { + $this->logins[] = $login; + $login->setToken($this); + } + public function getToken() { return $this->token; diff --git a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheOneToManyTest.php b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheOneToManyTest.php index 4b978b1d6..9ed1ea458 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheOneToManyTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SecondLevelCacheOneToManyTest.php @@ -3,7 +3,9 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\Cache\City; +use Doctrine\Tests\Models\Cache\Login; use Doctrine\Tests\Models\Cache\State; +use Doctrine\Tests\Models\Cache\Token; use Doctrine\Tests\Models\Cache\Travel; use Doctrine\Tests\Models\Cache\Traveler; @@ -381,4 +383,34 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest $this->assertEquals(4, $result->getTravels()->count()); } + + public function testPutAndLoadNonCacheableOneToMany() + { + $this->assertNull($this->cache->getEntityCacheRegion(Login::CLASSNAME)); + $this->assertInstanceOf('Doctrine\ORM\Cache\Region', $this->cache->getEntityCacheRegion(Token::CLASSNAME)); + + $l1 = new Login('session1'); + $l2 = new Login('session2'); + $token = new Token('token-hash'); + + $this->_em->persist($token); + $this->_em->flush(); + $token->addLogin($l1); + $token->addLogin($l2); + $this->_em->flush(); + $this->_em->clear(); + + $queryCount = $this->getCurrentQueryCount(); + + $this->assertTrue($this->cache->containsEntity(Token::CLASSNAME, $token->getToken())); + + $entity = $this->_em->find(Token::CLASSNAME, $token->getToken()); + + $this->assertInstanceOf(Token::CLASSNAME, $entity); + $this->assertEquals('token-hash', $entity->getToken()); + $this->assertEquals($queryCount, $this->getCurrentQueryCount()); + + $this->assertCount(2, $entity->getLogins()); + $this->assertEquals($queryCount + 1 , $this->getCurrentQueryCount()); + } } diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index bb05c352f..e1e7e9bfc 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -181,6 +181,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase 'Doctrine\Tests\Models\Cache\Bar', 'Doctrine\Tests\Models\Cache\Flight', 'Doctrine\Tests\Models\Cache\Token', + 'Doctrine\Tests\Models\Cache\Login', 'Doctrine\Tests\Models\Cache\Client', 'Doctrine\Tests\Models\Cache\AttractionInfo', 'Doctrine\Tests\Models\Cache\AttractionContactInfo', @@ -417,6 +418,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM cache_state'); $conn->executeUpdate('DELETE FROM cache_country'); $conn->executeUpdate('DELETE FROM cache_token'); + $conn->executeUpdate('DELETE FROM cache_login'); $conn->executeUpdate('DELETE FROM cache_client'); }