Skip to content

Commit

Permalink
Ability to set the table name for the join table
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 26, 2013
1 parent 8ec20f8 commit 9c61c65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -217,14 +217,15 @@ protected function _buildQuery($options) {
*/
protected function _joinTableName($name = null) {
if ($name === null) {
if (empty($this->_joinTableName)) {
if (empty($this->_joinTable)) {
$aliases = array_map('\Cake\Utility\Inflector::tableize', [
$sAlias = $this->source()->alias(),
$tAlias = $this->target()->alias()
]);
sort($aliases);
$name = implode('_', $aliases);
$this->_joinTable = implode('_', $aliases);
}
return $this->_joinTable;
}
return $this->_joinTable = $name;
}
Expand Down
17 changes: 17 additions & 0 deletions lib/Cake/Test/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -128,4 +128,21 @@ public function testPivot() {
$assoc->pivot('ArticleTag');
$this->assertSame($pivot, $assoc->pivot());
}

/**
* Tests it is possible to set the table name for the join table
*
* @return void
*/
public function testPivotWithDefaultTableName() {
$assoc = new BelongsToMany('Test', [
'sourceTable' => $this->article,
'targetTable' => $this->tag,
'joinTable' => 'tags_articles'
]);
$pivot = $assoc->pivot();
$this->assertEquals('ArticleTag', $pivot->alias());
$this->assertEquals('tags_articles', $pivot->table());
}

}

0 comments on commit 9c61c65

Please sign in to comment.