Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 00d1aa2192b86e51872fbc833b2436354f6fe162 1 parent 4b85d7a
Bogdan Albei authored July 22, 2011
27  lib/Doctrine/ORM/AbstractQuery.php
@@ -20,7 +20,8 @@
20 20
 namespace Doctrine\ORM;
21 21
 
22 22
 use Doctrine\DBAL\Types\Type,
23  
-    Doctrine\ORM\Query\QueryException;
  23
+    Doctrine\ORM\Query\QueryException,
  24
+    Doctrine\ORM\Internal\Hydration\PDOMock;
24 25
 
25 26
 /**
26 27
  * Base contract for ORM queries. Base class for Query and NativeQuery.
@@ -570,16 +571,30 @@ public function execute($params = array(), $hydrationMode = null)
570 571
                 // Cache miss.
571 572
                 $stmt = $this->_doExecute();
572 573
 
  574
+                //mock the PDO statement to save the db results into the cache
  575
+                $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  576
+                $stmt->closeCursor();
  577
+                $stmtMock = new PDOMock($data);
  578
+                
573 579
                 $result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
574  
-                    $stmt, $this->_resultSetMapping, $this->_hints
  580
+                    $stmtMock, $this->_resultSetMapping, $this->_hints
575 581
                 );
576 582
 
577  
-                $cacheDriver->save($hash, array($key => $result), $this->_resultCacheTTL);
  583
+                $cacheDriver->save($hash, array($key => $stmtMock->fetchAll(null)), $this->_resultCacheTTL);
578 584
 
579 585
                 return $result;
580 586
             } else {
581 587
                 // Cache hit.
582  
-                return $cached[$key];
  588
+ 
  589
+                    $this->_doExecute(FALSE/*do not execute the db call*/);
  590
+                    $stmtMock = new PDOMock($cached[$key]);
  591
+                    
  592
+                    $result = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
  593
+                        $stmtMock, $this->_resultSetMapping, $this->_hints
  594
+                    );
  595
+                    
  596
+                    return $result;
  597
+                
583 598
             }
584 599
         }
585 600
 
@@ -645,10 +660,10 @@ protected function getResultCacheId()
645 660
 
646 661
     /**
647 662
      * Executes the query and returns a the resulting Statement object.
648  
-     *
  663
+     * @param $executeDbCall Execute actual db call or not
649 664
      * @return Doctrine\DBAL\Driver\Statement The executed database statement that holds the results.
650 665
      */
651  
-    abstract protected function _doExecute();
  666
+    abstract protected function _doExecute($executeDbCall = TRUE);
652 667
 
653 668
     /**
654 669
      * Cleanup Query resource when clone is called.
40  lib/Doctrine/ORM/Internal/Hydration/PDOMock.php
... ...
@@ -0,0 +1,40 @@
  1
+<?php
  2
+namespace Doctrine\ORM\Internal\Hydration;
  3
+
  4
+/**
  5
+ * 
  6
+ * This is mocking a PDO connection, used for caching
  7
+ * @author Bogdan Albei <bogdan.albei@gmail.com>
  8
+ *
  9
+ */
  10
+class PDOMock
  11
+{
  12
+    private $data;
  13
+    private $index = 0;
  14
+    
  15
+    public function __construct($data)
  16
+    {
  17
+        $this->data = $data;
  18
+    }
  19
+    
  20
+    public function fetch($mode)
  21
+    {
  22
+        if(isset($this->data[$this->index])) {
  23
+            $ret = $this->data[$this->index];
  24
+            $this->index++;
  25
+            return $ret;
  26
+        }
  27
+        else {
  28
+            return FALSE;
  29
+        }
  30
+    }
  31
+    
  32
+    public function fetchAll($mode)
  33
+    {
  34
+        return $this->data;
  35
+    }
  36
+    
  37
+    public function closeCursor() 
  38
+    {
  39
+    }
  40
+}
13  lib/Doctrine/ORM/NativeQuery.php
@@ -55,7 +55,7 @@ public function getSQL()
55 55
     /**
56 56
      * {@inheritdoc}
57 57
      */
58  
-    protected function _doExecute()
  58
+    protected function _doExecute($executeDbCall = TRUE)
59 59
     {
60 60
         $stmt = $this->_em->getConnection()->prepare($this->_sql);
61 61
         $params = $this->_params;
@@ -66,8 +66,13 @@ protected function _doExecute()
66 66
                 $stmt->bindValue($key, $value);
67 67
             }
68 68
         }
69  
-        $stmt->execute();
70  
-
71  
-        return $stmt;
  69
+        
  70
+        if($executeDbCall) {
  71
+            $stmt->execute();
  72
+    
  73
+            return $stmt;
  74
+        }
  75
+        
  76
+        return FALSE;
72 77
     }
73 78
 }
18  lib/Doctrine/ORM/Query.php
@@ -53,15 +53,6 @@
53 53
      * @var string
54 54
      */
55 55
     const HINT_REFRESH = 'doctrine.refresh';
56  
-    
57  
-    
58  
-    /**
59  
-     * Internal hint: is set to the proxy entity that is currently triggered for loading
60  
-     * 
61  
-     * @var string
62  
-     */
63  
-    const HINT_REFRESH_ENTITY = 'doctrine.refresh.entity';
64  
-    
65 56
     /**
66 57
      * The forcePartialLoad query hint forces a particular query to return
67 58
      * partial objects.
@@ -227,7 +218,7 @@ private function _parse()
227 218
     /**
228 219
      * {@inheritdoc}
229 220
      */
230  
-    protected function _doExecute()
  221
+    protected function _doExecute($executeDbCall = TRUE)
231 222
     {
232 223
         $executor = $this->_parse()->getSqlExecutor();
233 224
 
@@ -279,8 +270,11 @@ protected function _doExecute()
279 270
         if ($this->_resultSetMapping === null) {
280 271
             $this->_resultSetMapping = $this->_parserResult->getResultSetMapping();
281 272
         }
282  
-
283  
-        return $executor->execute($this->_em->getConnection(), $sqlParams, $types);
  273
+        if($executeDbCall) {
  274
+            return $executor->execute($this->_em->getConnection(), $sqlParams, $types);
  275
+        }
  276
+        
  277
+        return FALSE;
284 278
     }
285 279
 
286 280
     /**

0 notes on commit 00d1aa2

Please sign in to comment.
Something went wrong with that request. Please try again.