Browse Source

Merge pull request #6165 from ramiroaraujo/query-logger-with-same-params-multiple-times

QueryLogger bug with same named placeholder used multiple times
José Lorenzo Rodríguez 11 years ago
parent
commit
d501b34fe7
2 changed files with 20 additions and 1 deletions
  1. 2 1
      src/Database/Log/QueryLogger.php
  2. 18 0
      tests/TestCase/Database/Log/QueryLoggerTest.php

+ 2 - 1
src/Database/Log/QueryLogger.php

@@ -70,10 +70,11 @@ class QueryLogger
         }, $query->params);
 
         $keys = [];
+        $limit = is_int(key($params)) ? 1 : -1;
         foreach ($params as $key => $param) {
             $keys[] = is_string($key) ? "/:$key/" : '/[?]/';
         }
 
-        return preg_replace($keys, $params, $query->query, 1);
+        return preg_replace($keys, $params, $query->query, $limit);
     }
 }

+ 18 - 0
tests/TestCase/Database/Log/QueryLoggerTest.php

@@ -85,6 +85,24 @@ class QueryLoggerTest extends TestCase
     }
 
     /**
+     * Tests that repeated placeholders are correctly replaced
+     *
+     * @return void
+     */
+    public function testStringInterpolation3()
+    {
+        $logger = $this->getMock('\Cake\Database\Log\QueryLogger', ['_log']);
+        $query = new LoggedQuery;
+        $query->query = 'SELECT a FROM b where a = :p1 AND b = :p1 AND c = :p2 AND d = :p2';
+        $query->params = ['p1' => 'string', 'p2' => 3];
+
+        $logger->expects($this->once())->method('_log')->with($query);
+        $logger->log($query);
+        $expected = "SELECT a FROM b where a = 'string' AND b = 'string' AND c = 3 AND d = 3";
+        $this->assertEquals($expected, (string)$query);
+    }
+
+    /**
      * Tests that the logged query object is passed to the built-in logger using
      * the correct scope
      *