From b5f5927fa7724861abba5d371b28ff5d59186fa8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 2 May 2012 09:37:40 -0500 Subject: [PATCH] Fix bug with many-to-many relationships on non-default database connection. --- laravel/database/eloquent/pivot.php | 14 +++++++++++++- .../relationships/has_many_and_belongs_to.php | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/laravel/database/eloquent/pivot.php b/laravel/database/eloquent/pivot.php index b98fc226..d2878bb5 100644 --- a/laravel/database/eloquent/pivot.php +++ b/laravel/database/eloquent/pivot.php @@ -20,11 +20,13 @@ class Pivot extends Model { * Create a new pivot table instance. * * @param string $table + * @param string $connection * @return void */ - public function __construct($table) + public function __construct($table, $connection = null) { $this->pivot_table = $table; + $this->connection = $connection; parent::__construct(array(), true); } @@ -39,4 +41,14 @@ public function table() return $this->pivot_table; } + /** + * Get the connection used by the pivot table. + * + * @return string + */ + public function connection() + { + return $this->connection; + } + } \ No newline at end of file diff --git a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php index 1992d70e..587e504c 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -349,7 +349,7 @@ protected function hydrate_pivot(&$results) // Every model result for a many-to-many relationship needs a Pivot instance // to represent the pivot table's columns. Sometimes extra columns are on // the pivot table that may need to be accessed by the developer. - $pivot = new Pivot($this->joining); + $pivot = new Pivot($this->joining, $this->model->connection()); // If the attribute key starts with "pivot_", we know this is a column on // the pivot table, so we will move it to the Pivot model and purge it @@ -400,7 +400,9 @@ public function with($columns) */ public function pivot() { - return new Has_Many($this->base, new Pivot($this->joining), $this->foreign_key()); + $pivot = new Pivot($this->joining, $this->model->connection()); + + return new Has_Many($this->base, $pivot, $this->foreign_key()); } /**