Skip to content

Commit

Permalink
Merge branch 'master' of github.com:doctrine/doctrine2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Dec 3, 2010
2 parents 054f26c + c6a6aaf commit 687548c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,22 @@ protected function _getResultCacheId()
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);
}
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,16 @@ private function errorIfClosed()
}
}

/**
* 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.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Doctrine/Tests/ORM/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ function setUp()
$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());
Expand Down
60 changes: 60 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -146,4 +147,63 @@ public function testResultCacheDependsOnHydrationMode($query)

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

0 comments on commit 687548c

Please sign in to comment.