Skip to content

Commit

Permalink
Merge branch 'master-tabledot-fk' into master
Browse files Browse the repository at this point in the history
Refs #6616
  • Loading branch information
markstory committed May 29, 2015
2 parents f2414e1 + e820d97 commit 6578de6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Core/ConventionsTrait.php
Expand Up @@ -49,11 +49,14 @@ protected function _entityName($name)
/**
* 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';
}

Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/ORM/Association/BelongsToTest.php
Expand Up @@ -91,6 +91,22 @@ public function tearDown()
TableRegistry::clear();
}

/**
* 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
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/ORM/Association/HasManyTest.php
Expand Up @@ -83,6 +83,20 @@ public function tearDown()
TableRegistry::clear();
}

/**
* 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
*
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -136,6 +136,9 @@ public function testTableMethod()

$table->table('other');
$this->assertEquals('other', $table->table());

$table->table('database.other');
$this->assertEquals('database.other', $table->table());
}

/**
Expand Down

0 comments on commit 6578de6

Please sign in to comment.