Browse Source

Merge pull request #5916 from cakephp/3.0-failing-entity-source-same

Fix entity->source() for hydrated results
Andy Dawson 11 years ago
parent
commit
cbb2d34547
3 changed files with 34 additions and 1 deletions
  1. 1 1
      src/ORM/ResultSet.php
  2. 1 0
      src/ORM/Table.php
  3. 32 0
      tests/TestCase/ORM/TableTest.php

+ 1 - 1
src/ORM/ResultSet.php

@@ -472,7 +472,7 @@ class ResultSet implements ResultSetInterface
             $results[$defaultAlias]['_matchingData'] = $results['_matchingData'];
         }
 
-        $options['source'] = $defaultAlias;
+        $options['source'] = $this->_defaultTable->registryAlias();
         $results = $results[$defaultAlias];
         if ($this->_hydrate && !($results instanceof Entity)) {
             $results = new $this->_entityClass($results, $options);

+ 1 - 0
src/ORM/Table.php

@@ -2158,6 +2158,7 @@ class Table implements RepositoryInterface, EventListenerInterface
     {
         $conn = $this->connection();
         return [
+            'registryAlias' => $this->registryAlias(),
             'table' => $this->table(),
             'alias' => $this->alias(),
             'entityClass' => $this->entityClass(),

+ 32 - 0
tests/TestCase/ORM/TableTest.php

@@ -2484,6 +2484,23 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests that the source of an existing Entity is the same as a new one
+     *
+     * @return void
+     */
+    public function testEntitySourceExistingAndNew()
+    {
+        Plugin::load('TestPlugin');
+        $table = TableRegistry::get('TestPlugin.Authors');
+
+        $existingAuthor = $table->find()->first();
+        $newAuthor = $table->newEntity();
+
+        $this->assertEquals('TestPlugin.Authors', $existingAuthor->source());
+        $this->assertEquals('TestPlugin.Authors', $newAuthor->source());
+    }
+
+    /**
      * Tests that calling an entity with an empty array will run validation
      * whereas calling it with no parameters will not run any validation.
      *
@@ -3491,6 +3508,7 @@ class TableTest extends TestCase
         $articles->addBehavior('Timestamp');
         $result = $articles->__debugInfo();
         $expected = [
+            'registryAlias' => 'articles',
             'table' => 'articles',
             'alias' => 'articles',
             'entityClass' => 'TestApp\Model\Entity\Article',
@@ -3500,6 +3518,20 @@ class TableTest extends TestCase
             'connectionName' => 'test'
         ];
         $this->assertEquals($expected, $result);
+
+        $articles = TableRegistry::get('Foo.Articles');
+        $result = $articles->__debugInfo();
+        $expected = [
+            'registryAlias' => 'Foo.Articles',
+            'table' => 'articles',
+            'alias' => 'Articles',
+            'entityClass' => '\Cake\ORM\Entity',
+            'associations' => [],
+            'behaviors' => [],
+            'defaultConnection' => 'default',
+            'connectionName' => 'test'
+        ];
+        $this->assertEquals($expected, $result);
     }
 
     /**