Browse Source

Merge branch 'master-tabledot-fk' into master

Refs #6616
Mark Story 10 years ago
parent
commit
6578de6bef

+ 3 - 0
src/Core/ConventionsTrait.php

@@ -49,11 +49,14 @@ trait ConventionsTrait
     /**
      * Creates the proper underscored model key for associations
      *
+     * If the input contains a dot, assume that the right side is the real table name.
+     *
      * @param string $name Model class name
      * @return string Singular model key
      */
     protected function _modelKey($name)
     {
+        list(, $name) = pluginSplit($name);
         return Inflector::underscore(Inflector::singularize($name)) . '_id';
     }
 

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

@@ -92,6 +92,22 @@ class BelongsToTest extends TestCase
     }
 
     /**
+     * Test that foreignKey generation ignores database names in target table.
+     *
+     * @return void
+     */
+    public function testForeignKey()
+    {
+        $this->company->table('schema.companies');
+        $this->client->table('schema.clients');
+        $assoc = new BelongsTo('Companies', [
+            'sourceTable' => $this->client,
+            'targetTable' => $this->company,
+        ]);
+        $this->assertEquals('company_id', $assoc->foreignKey());
+    }
+
+    /**
      * Tests that the association reports it can be joined
      *
      * @return void

+ 14 - 0
tests/TestCase/ORM/Association/HasManyTest.php

@@ -84,6 +84,20 @@ class HasManyTest extends TestCase
     }
 
     /**
+     * Test that foreignKey generation ignores database names in target table.
+     *
+     * @return void
+     */
+    public function testForeignKey()
+    {
+        $this->author->table('schema.authors');
+        $assoc = new HasMany('Articles', [
+            'sourceTable' => $this->author
+        ]);
+        $this->assertEquals('author_id', $assoc->foreignKey());
+    }
+
+    /**
      * Tests that the association reports it can be joined
      *
      * @return void

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

@@ -136,6 +136,9 @@ class TableTest extends TestCase
 
         $table->table('other');
         $this->assertEquals('other', $table->table());
+
+        $table->table('database.other');
+        $this->assertEquals('database.other', $table->table());
     }
 
     /**