Browse Source

Merge pull request #6785 from cakephp/issue-6775

Fix missing types for junction tables
José Lorenzo Rodríguez 10 years ago
parent
commit
46c3106694

+ 18 - 1
src/Database/TypeMap.php

@@ -64,6 +64,8 @@ class TypeMap
      * $query->defaults(['created' => 'datetime', 'is_visible' => 'boolean']);
      * ```
      *
+     * This method will replace all the existing type maps with the ones provided.
+     *
      * @param array $defaults associative array where keys are field names and values
      * are the correspondent type.
      * @return $this|array
@@ -78,7 +80,20 @@ class TypeMap
     }
 
     /**
-     * Configures a map of fields and their associated types for single-use.
+     * Add additional default types into the type map.
+     *
+     * If a key already exists it will not be overwritten.
+     *
+     * @param array $types The additional types to add.
+     * @return void
+     */
+    public function addDefaults(array $types)
+    {
+        $this->_defaults = $this->_defaults + $types;
+    }
+
+    /**
+     * Sets a map of fields and their associated types for single-use.
      *
      * If called with no arguments it will return the currently configured types.
      *
@@ -88,6 +103,8 @@ class TypeMap
      * $query->types(['created' => 'time']);
      * ```
      *
+     * This method will replace all the existing type maps with the ones provided.
+     *
      * @param array $types associative array where keys are field names and values
      * are the correspondent type.
      * @return $this|array

+ 1 - 0
src/ORM/Association/BelongsToMany.php

@@ -941,6 +941,7 @@ class BelongsToMany extends Association
 
         $assoc = $this->target()->association($name);
         $query
+            ->addDefaultTypes($assoc->target())
             ->join($matching + $joins, [], true)
             ->autoFields($query->clause('select') === [])
             ->select($query->aliasFields((array)$assoc->foreignKey(), $name));

+ 1 - 1
src/ORM/Query.php

@@ -143,7 +143,7 @@ class Query extends DatabaseQuery implements JsonSerializable
         foreach ($schema->columns() as $f) {
             $fields[$f] = $fields[$alias . '.' . $f] = $schema->columnType($f);
         }
-        $this->defaultTypes($fields);
+        $this->typeMap()->addDefaults($fields);
 
         return $this;
     }

+ 25 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -904,4 +904,29 @@ class QueryRegressionTest extends TestCase
             'Output values for functions are not cast yet.'
         );
     }
+
+    /**
+     * Test that contain queries map types correctly.
+     *
+     * @return void
+     */
+    public function testBooleanConditionsInContain()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->belongsToMany('Tags', [
+            'foreignKey' => 'article_id',
+            'associationForeignKey' => 'tag_id',
+            'through' => 'SpecialTags'
+        ]);
+        $query = $table->find()
+            ->contain(['Tags' => function ($q) {
+                return $q->where(['SpecialTags.highlighted_time >' => new Time('2014-06-01 00:00:00')]);
+            }])
+            ->where(['Articles.id' => 2]);
+
+        $result = $query->first();
+        $this->assertEquals(2, $result->id);
+        $this->assertNotEmpty($result->tags, 'Missing tags');
+        $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
+    }
 }