1
0
mirror of synced 2024-12-14 07:06:04 +03:00

DDC-909 - Fix Result Cache with entities as parameters.

This commit is contained in:
Benjamin Eberlei 2010-12-03 17:34:56 +01:00
parent 0ba9321f06
commit 8e4197adc5
2 changed files with 74 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}