From 8e4197adc5f09545df3a8a1a8e05e3d7a5ca0ea5 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 3 Dec 2010 17:34:56 +0100 Subject: [PATCH 1/2] DDC-909 - Fix Result Cache with entities as parameters. --- lib/Doctrine/ORM/AbstractQuery.php | 15 ++++- .../Tests/ORM/Functional/ResultCacheTest.php | 60 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index e578a7e70..562a5d6fb 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -562,9 +562,22 @@ abstract class AbstractQuery if ($this->_resultCacheId) { return $this->_resultCacheId; } else { + $params = $this->_params; + foreach ($params AS $key => $value) { + if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) { + if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) { + $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value); + } else { + $class = $this->_em->getClassMetadata(get_class($value)); + $idValues = $class->getIdentifierValues($value); + } + $params[$key] = $idValues; + } + } + $sql = $this->getSql(); ksort($this->_hints); - return md5(implode(";", (array)$sql) . var_export($this->_params, true) . + return md5(implode(";", (array)$sql) . var_export($params, true) . var_export($this->_hints, true)."&hydrationMode=".$this->_hydrationMode); } } diff --git a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php index 3c5f00982..68616f99b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Tests\Models\CMS\CmsUser; +use Doctrine\Tests\Models\CMS\CmsArticle; use Doctrine\Common\Cache\ArrayCache; require_once __DIR__ . '/../../TestInit.php'; @@ -146,4 +147,63 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($cacheCount + 1, count($cache->getIds())); } + + /** + * @group DDC-909 + */ + public function testResultCacheWithObjectParameter() + { + $user1 = new CmsUser; + $user1->name = 'Roman'; + $user1->username = 'romanb'; + $user1->status = 'dev'; + + $user2 = new CmsUser; + $user2->name = 'Benjamin'; + $user2->username = 'beberlei'; + $user2->status = 'dev'; + + $article = new CmsArticle(); + $article->text = "foo"; + $article->topic = "baz"; + $article->user = $user1; + + $this->_em->persist($article); + $this->_em->persist($user1); + $this->_em->persist($user2); + $this->_em->flush(); + + $query = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); + $query->setParameter(1, $user1); + + $cache = new ArrayCache(); + + $query->setResultCacheDriver($cache)->useResultCache(true); + + $articles = $query->getResult(); + + $this->assertEquals(1, count($articles)); + $this->assertEquals('baz', $articles[0]->topic); + + $this->_em->clear(); + + $query2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); + $query2->setParameter(1, $user1); + + $query2->setResultCacheDriver($cache)->useResultCache(true); + + $articles = $query2->getResult(); + + $this->assertEquals(1, count($articles)); + $this->assertEquals('baz', $articles[0]->topic); + + $query3 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.user = ?1'); + $query3->setParameter(1, $user2); + + $query3->setResultCacheDriver($cache)->useResultCache(true); + + $articles = $query3->getResult(); + + $this->assertEquals(0, count($articles)); + } } \ No newline at end of file From c6a6aaf493671820856341d76d9c475a45ff2127 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Fri, 3 Dec 2010 17:44:24 +0100 Subject: [PATCH 2/2] DDC-899 - Add method to check if EntityManager is still open. --- lib/Doctrine/ORM/EntityManager.php | 10 ++++++++++ tests/Doctrine/Tests/ORM/EntityManagerTest.php | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0ab1541ba..3af9cf3c3 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -619,6 +619,16 @@ class EntityManager } } + /** + * Check if the Entity manager is open or closed. + * + * @return bool + */ + public function isOpen() + { + return (!$this->closed); + } + /** * Gets the UnitOfWork used by the EntityManager to coordinate operations. * diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index ad5f41de7..a2fe796d6 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -14,6 +14,16 @@ class EntityManagerTest extends \Doctrine\Tests\OrmTestCase $this->_em = $this->_getTestEntityManager(); } + /** + * @group DDC-899 + */ + public function testIsOpen() + { + $this->assertTrue($this->_em->isOpen()); + $this->_em->close(); + $this->assertFalse($this->_em->isOpen()); + } + public function testGetConnection() { $this->assertType('\Doctrine\DBAL\Connection', $this->_em->getConnection());