Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed DDC-217, the result cache is caching the sql result

  • Loading branch information...
commit 00d1aa2192b86e51872fbc833b2436354f6fe162 1 parent 4b85d7a
@bogdanalbei authored
View
27 lib/Doctrine/ORM/AbstractQuery.php
@@ -20,7 +20,8 @@
namespace Doctrine\ORM;
use Doctrine\DBAL\Types\Type,
- Doctrine\ORM\Query\QueryException;
+ Doctrine\ORM\Query\QueryException,
+ Doctrine\ORM\Internal\Hydration\PDOMock;
/**
* Base contract for ORM queries. Base class for Query and NativeQuery.
@@ -570,16 +571,30 @@ public function execute($params = array(), $hydrationMode = null)
// Cache miss.
$stmt = $this->_doExecute();
+ //mock the PDO statement to save the db results into the cache
+ $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
+ $stmt->closeCursor();
+ $stmtMock = new PDOMock($data);
+
$result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
- $stmt, $this->_resultSetMapping, $this->_hints
+ $stmtMock, $this->_resultSetMapping, $this->_hints
);
- $cacheDriver->save($hash, array($key => $result), $this->_resultCacheTTL);
+ $cacheDriver->save($hash, array($key => $stmtMock->fetchAll(null)), $this->_resultCacheTTL);
return $result;
} else {
// Cache hit.
- return $cached[$key];
+
+ $this->_doExecute(FALSE/*do not execute the db call*/);
+ $stmtMock = new PDOMock($cached[$key]);
+
+ $result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
+ $stmtMock, $this->_resultSetMapping, $this->_hints
+ );
+
+ return $result;
+
}
}
@@ -645,10 +660,10 @@ protected function getResultCacheId()
/**
* Executes the query and returns a the resulting Statement object.
- *
+ * @param $executeDbCall Execute actual db call or not
* @return Doctrine\DBAL\Driver\Statement The executed database statement that holds the results.
*/
- abstract protected function _doExecute();
+ abstract protected function _doExecute($executeDbCall = TRUE);
/**
* Cleanup Query resource when clone is called.
View
40 lib/Doctrine/ORM/Internal/Hydration/PDOMock.php
@@ -0,0 +1,40 @@
+<?php
+namespace Doctrine\ORM\Internal\Hydration;
+
+/**
+ *
+ * This is mocking a PDO connection, used for caching
+ * @author Bogdan Albei <bogdan.albei@gmail.com>
+ *
+ */
+class PDOMock
+{
+ private $data;
+ private $index = 0;
+
+ public function __construct($data)
+ {
+ $this->data = $data;
+ }
+
+ public function fetch($mode)
+ {
+ if(isset($this->data[$this->index])) {
+ $ret = $this->data[$this->index];
+ $this->index++;
+ return $ret;
+ }
+ else {
+ return FALSE;
+ }
+ }
+
+ public function fetchAll($mode)
+ {
+ return $this->data;
+ }
+
+ public function closeCursor()
+ {
+ }
+}
View
13 lib/Doctrine/ORM/NativeQuery.php
@@ -55,7 +55,7 @@ public function getSQL()
/**
* {@inheritdoc}
*/
- protected function _doExecute()
+ protected function _doExecute($executeDbCall = TRUE)
{
$stmt = $this->_em->getConnection()->prepare($this->_sql);
$params = $this->_params;
@@ -66,8 +66,13 @@ protected function _doExecute()
$stmt->bindValue($key, $value);
}
}
- $stmt->execute();
-
- return $stmt;
+
+ if($executeDbCall) {
+ $stmt->execute();
+
+ return $stmt;
+ }
+
+ return FALSE;
}
}
View
18 lib/Doctrine/ORM/Query.php
@@ -53,15 +53,6 @@
* @var string
*/
const HINT_REFRESH = 'doctrine.refresh';
-
-
- /**
- * Internal hint: is set to the proxy entity that is currently triggered for loading
- *
- * @var string
- */
- const HINT_REFRESH_ENTITY = 'doctrine.refresh.entity';
-
/**
* The forcePartialLoad query hint forces a particular query to return
* partial objects.
@@ -227,7 +218,7 @@ private function _parse()
/**
* {@inheritdoc}
*/
- protected function _doExecute()
+ protected function _doExecute($executeDbCall = TRUE)
{
$executor = $this->_parse()->getSqlExecutor();
@@ -279,8 +270,11 @@ protected function _doExecute()
if ($this->_resultSetMapping === null) {
$this->_resultSetMapping = $this->_parserResult->getResultSetMapping();
}
-
- return $executor->execute($this->_em->getConnection(), $sqlParams, $types);
+ if($executeDbCall) {
+ return $executor->execute($this->_em->getConnection(), $sqlParams, $types);
+ }
+
+ return FALSE;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.