Skip to content

Commit

Permalink
Merge pull request #2934 from bar/3.0-through
Browse files Browse the repository at this point in the history
3.0 - Do not eagerly set the junction relation.
  • Loading branch information
lorenzo committed Mar 2, 2014
2 parents 2588333 + 3eecc76 commit 4d96262
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -115,6 +115,13 @@ class BelongsToMany extends Association {
*/
protected $_targetForeignKey;

/**
* The table instance for the junction relation.
*
* @var string|\Cake\ORM\Table $table Name or instance for the join table
*/
protected $_through;

/**
* Sets the name of the field representing the foreign key to the target table.
* If no parameters are passed current field is returned
Expand Down Expand Up @@ -148,14 +155,18 @@ public function junction($table = null) {

if ($table === null) {
if (empty($this->_junctionTable)) {
$tableName = $this->_junctionTableName();
$tableAlias = Inflector::camelize($tableName);

$config = [];
if (!TableRegistry::exists($tableAlias)) {
$config = ['table' => $tableName];
if (!empty($this->_through)) {
$table = $this->_through;
} else {
$tableName = $this->_junctionTableName();
$tableAlias = Inflector::camelize($tableName);

$config = [];
if (!TableRegistry::exists($tableAlias)) {
$config = ['table' => $tableName];
}
$table = TableRegistry::get($tableAlias, $config);
}
$table = TableRegistry::get($tableAlias, $config);
} else {
return $this->_junctionTable;
}
Expand Down Expand Up @@ -944,8 +955,8 @@ protected function _junctionTableName($name = null) {

/**
* Parse extra options passed in the constructor.
* @param array $opts original list of options passed in constructor
*
* @param array $opts original list of options passed in constructor
* @return void
*/
protected function _options(array $opts) {
Expand All @@ -957,7 +968,7 @@ protected function _options(array $opts) {
$this->_junctionTableName($opts['joinTable']);
}
if (!empty($opts['through'])) {
$this->junction($opts['through']);
$this->_through = $opts['through'];
}
if (!empty($opts['saveStrategy'])) {
$this->saveStrategy($opts['saveStrategy']);
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Expand Up @@ -1085,6 +1085,7 @@ public function testUnlinkSuccess() {
'joinTable' => 'tags_articles'
];
$assoc = $this->article->belongsToMany('Test', $config);
$assoc->junction();
$this->article->association('ArticlesTags')
->conditions(['foo' => 1]);

Expand Down Expand Up @@ -1248,6 +1249,7 @@ public function testReplaceLinkSuccess() {
['_collectJointEntities', '_saveTarget'],
['tags', $config]
);
$assoc->junction();

$this->article
->association('ArticlesTags')
Expand Down

0 comments on commit 4d96262

Please sign in to comment.