Browse Source

Add test covering correct use for #10592

Aliased fields must include the association names or the ORM cannot
correctly map the result set onto entities.
Mark Story 9 years ago
parent
commit
edfb0c6bb1
1 changed files with 26 additions and 0 deletions
  1. 26 0
      tests/TestCase/ORM/QueryRegressionTest.php

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

@@ -92,6 +92,32 @@ class QueryRegressionTest extends TestCase
     }
 
     /**
+     * Tests that eagerloading associations with aliased fields works.
+     *
+     * @return void
+     */
+    public function testEagerLoadingAliasedAssociationFields()
+    {
+        $this->loadFixtures('Articles', 'Authors');
+        $table = TableRegistry::get('Articles');
+        $table->belongsTo('Authors', [
+            'foreignKey' => 'author_id'
+        ]);
+        $result = $table->find()
+            ->contain(['Authors' => [
+                'fields' => [
+                    'id',
+                    'Authors__aliased_name' => 'name'
+                ]
+            ]])
+            ->where(['Articles.id' => 1])
+            ->first();
+        $this->assertInstanceOf(EntityInterface::class, $result);
+        $this->assertInstanceOf(EntityInterface::class, $result->author);
+        $this->assertSame('mariano', $result->author->aliased_name);
+    }
+
+    /**
      * Tests that eagerloading and hydration works for associations that have
      * different aliases in the association and targetTable
      *