Browse Source

Don't bind placeholders when they aren't needed.

Binding unused parameters causes errors in non-MySQL drivers, so lets
not do that. Fix up some aliasing issues that cause issues in Postgres.
Mark Story 10 years ago
parent
commit
60bd60f52d
2 changed files with 13 additions and 9 deletions
  1. 6 2
      src/Database/QueryCompiler.php
  2. 7 7
      tests/TestCase/ORM/QueryRegressionTest.php

+ 6 - 2
src/Database/QueryCompiler.php

@@ -98,10 +98,14 @@ class QueryCompiler
             $this->{'_' . $type . 'Parts'}
         );
 
-        // Propagate bound parameters from sub-queries
+        // Propagate bound parameters from sub-queries if the
+        // placeholders can be found in the SQL statement.
         if ($query->valueBinder() !== $generator) {
             foreach ($query->valueBinder()->bindings() as $binding) {
-                $generator->bind(':' . $binding['placeholder'], $binding['value'], $binding['type']);
+                $placeholder = ':' . $binding['placeholder'];
+                if (strpos($sql, $placeholder) !== false) {
+                    $generator->bind($placeholder, $binding['value'], $binding['type']);
+                }
             }
         }
         return $sql;

+ 7 - 7
tests/TestCase/ORM/QueryRegressionTest.php

@@ -591,9 +591,9 @@ class QueryRegressionTest extends TestCase
         $table = TableRegistry::get('Articles');
         $query = $table
             ->find()
-            ->select(['Articles.title', 'Articles.id'])
-            ->where("Articles.title LIKE :val")
-            ->group('Articles.id')
+            ->select(['title', 'id'])
+            ->where("title LIKE :val")
+            ->group('id')
             ->bind(':val', '%Second%');
         $count = $query->count();
         $this->assertEquals(1, $count);
@@ -608,14 +608,14 @@ class QueryRegressionTest extends TestCase
     {
         $table = TableRegistry::get('Articles');
         $sub = $table->find()
-            ->select(['Articles.id'])
-            ->where("Articles.title LIKE :val")
+            ->select(['id'])
+            ->where("title LIKE :val")
             ->bind(':val', 'Second %');
 
         $query = $table
             ->find()
-            ->select(['Articles.title'])
-            ->where(["Articles.id NOT IN" => $sub]);
+            ->select(['title'])
+            ->where(["id NOT IN" => $sub]);
         $result = $query->toArray();
         $this->assertCount(2, $result);
         $this->assertEquals('First Article', $result[0]->title);