From 2931b7d2535719e5e823f0d1214f2bcc53f534a7 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 24 Nov 2013 22:24:08 -0500 Subject: [PATCH] Call fewer methods when building belongsToMany associations. Don't call methods more than once when we know the answer already. --- Cake/ORM/Association/BelongsToMany.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Cake/ORM/Association/BelongsToMany.php b/Cake/ORM/Association/BelongsToMany.php index 89790562170..f5f23a02bfb 100644 --- a/Cake/ORM/Association/BelongsToMany.php +++ b/Cake/ORM/Association/BelongsToMany.php @@ -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;