Skip to content

Commit

Permalink
Call fewer methods when building belongsToMany associations.
Browse files Browse the repository at this point in the history
Don't call methods more than once when we know the answer already.
  • Loading branch information
markstory committed Nov 25, 2013
1 parent 6a4dad2 commit 2931b7d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Cake/ORM/Association/BelongsToMany.php
Expand Up @@ -106,22 +106,23 @@ public function pivot($table = null) {
if (is_string($table)) {
$table = TableRegistry::get($table);
}
$pivotAlias = $table->alias();

if (!$table->association($sAlias)) {
$table->belongsTo($sAlias)->target($this->source());
$table->belongsTo($sAlias)->target($source);
}

if (!$table->association($tAlias)) {
$table->belongsTo($tAlias)->target($this->target());
$table->belongsTo($tAlias)->target($target);
}

if (!$target->association($table->alias())) {
if (!$target->association($pivotAlias)) {
$target->belongsToMany($sAlias);
$target->hasMany($table->alias())->target($table);
$target->hasMany($pivotAlias)->target($table);
}

if (!$source->association($table->alias())) {
$source->hasMany($table->alias())->target($table);
$source->hasMany($pivotAlias)->target($table);
}

return $this->_pivotTable = $table;
Expand Down

0 comments on commit 2931b7d

Please sign in to comment.