Browse Source

Fix matchingData not having type information set.

As part of the refactoring work done to make typecasting lazy and work
with functions the casting around `_matchingData` was accidentally
broken. This adds each attached association to the type map when fields
are selected on a given association.

Refs #8147
Mark Story 10 years ago
parent
commit
ae5b1bf59c

+ 5 - 4
src/ORM/Association.php

@@ -699,14 +699,14 @@ abstract class Association
      */
     protected function _appendFields($query, $surrogate, $options)
     {
-        $fields = $surrogate->clause('select') ?: $options['fields'];
-        $target = $this->_targetTable;
-        $autoFields = $surrogate->autoFields();
-
         if ($query->eagerLoader()->autoFields() === false) {
             return;
         }
 
+        $fields = $surrogate->clause('select') ?: $options['fields'];
+        $target = $this->_targetTable;
+        $autoFields = $surrogate->autoFields();
+
         if (empty($fields) && !$autoFields) {
             if ($options['includeFields'] && ($fields === null || $fields !== false)) {
                 $fields = $target->schema()->columns();
@@ -719,6 +719,7 @@ abstract class Association
 
         if (!empty($fields)) {
             $query->select($query->aliasFields($fields, $target->alias()));
+            $query->addDefaultTypes($target);
         }
     }
 

+ 6 - 0
tests/TestCase/ORM/Association/BelongsToTest.php

@@ -170,6 +170,12 @@ class BelongsToTest extends TestCase
             ]
         ];
         $this->assertEquals($expected, $query->clause('join'));
+
+        $this->assertEquals(
+            'integer',
+            $query->typeMap()->type('Companies__id'),
+            'Associations should map types.'
+        );
     }
 
     /**

+ 6 - 0
tests/TestCase/ORM/Association/HasOneTest.php

@@ -125,6 +125,12 @@ class HasOneTest extends TestCase
             'Profiles__user_id' => 'Profiles.user_id'
         ]);
         $association->attachTo($query);
+
+        $this->assertEquals(
+            'string',
+            $query->typeMap()->type('Profiles__first_name'),
+            'Associations should map types.'
+        );
     }
 
     /**

+ 39 - 5
tests/TestCase/ORM/QueryTest.php

@@ -95,8 +95,6 @@ class QueryTest extends TestCase
         $orders->hasOne('stuff');
         $stuff->belongsTo('stuffTypes');
         $companies->belongsTo('categories');
-
-        $this->fooTypeMap = new TypeMap(['foo.id' => 'integer', 'id' => 'integer']);
     }
 
     /**
@@ -680,8 +678,8 @@ class QueryTest extends TestCase
                         'user_id' => 4,
                         'comment' => 'Second Comment for First Article',
                         'published' => 'Y',
-                        'created' => '2007-03-18 10:47:23',
-                        'updated' => '2007-03-18 10:49:31',
+                        'created' => new Time('2007-03-18 10:47:23'),
+                        'updated' => new Time('2007-03-18 10:49:31'),
                     ]
                 ]
             ]
@@ -690,6 +688,28 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Tests that tables results can be filtered by the result of a HasMany
+     *
+     * @return void
+     */
+    public function testFilteringByHasManyHydration()
+    {
+        $table = TableRegistry::get('Articles');
+        $query = new Query($this->connection, $table);
+        $table->hasMany('Comments');
+
+        $result = $query->repository($table)
+            ->matching('Comments', function ($q) {
+                return $q->where(['Comments.user_id' => 4]);
+            })
+            ->first();
+        $this->assertInstanceOf('Cake\ORM\Entity', $result);
+        $this->assertInstanceOf('Cake\ORM\Entity', $result->_matchingData['Comments']);
+        $this->assertInternalType('integer', $result->_matchingData['Comments']->id);
+        $this->assertInstanceOf('Cake\I18n\Time', $result->_matchingData['Comments']->created);
+    }
+
+    /**
      * Tests that BelongsToMany associations are correctly eager loaded.
      * Also that the query object passes the correct parent model keys to the
      * association objects in order to perform eager loading with select strategy
@@ -2389,6 +2409,20 @@ class QueryTest extends TestCase
                 'authors__name' => 'string',
                 'authors.name' => 'string',
                 'name' => 'string',
+                'articles__id' => 'integer',
+                'articles.id' => 'integer',
+                'articles__author_id' => 'integer',
+                'articles.author_id' => 'integer',
+                'author_id' => 'integer',
+                'articles__title' => 'string',
+                'articles.title' => 'string',
+                'title' => 'string',
+                'articles__body' => 'text',
+                'articles.body' => 'text',
+                'body' => 'text',
+                'articles__published' => 'string',
+                'articles.published' => 'string',
+                'published' => 'string',
             ],
             'decorators' => 0,
             'executed' => false,
@@ -3200,6 +3234,6 @@ class QueryTest extends TestCase
                 ]
             ]
         ];
-        $this->assertEquals($expected, $results->first());
+        $this->assertSame($expected, $results->first());
     }
 }