From 453d4154f27c9ff05d8849b3769915f7bed4036a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 22 Mar 2012 09:07:21 -0500 Subject: [PATCH] Added "pivot" method to has_many_and_belongs_to. --- laravel/database/eloquent/query.php | 2 +- .../relationships/has_many_and_belongs_to.php | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/laravel/database/eloquent/query.php b/laravel/database/eloquent/query.php index b592bb35..250519a1 100644 --- a/laravel/database/eloquent/query.php +++ b/laravel/database/eloquent/query.php @@ -136,7 +136,7 @@ public function hydrate($model, $results, $include = true) // any pivot columns that are on the model. if ($this instanceof Relationships\Has_Many_And_Belongs_To) { - $this->pivot($models); + $this->hydrate_pivot($models); } return $models; 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 169e1f12..bfc17dd0 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -108,9 +108,11 @@ public function insert($attributes, $joining = array()) // If the insert was successful, we'll insert a record into the joining table // using the new ID that was just inserted into the related table, allowing // the developer to not worry about maintaining the join table. - if ($model instanceof Model and is_numeric($id = $model->get_key())) + if ($model instanceof Model) { - $result = $this->insert_joining(array_merge($this->join_record($id), $joining)); + $joining = array_merge($this->join_record($id), $joining); + + $result = $this->insert_joining($joining); } return $model instanceof Model and $result; @@ -279,7 +281,7 @@ public function match($relationship, &$parents, $children) * @param array $results * @return void */ - protected function pivot(&$results) + protected function hydrate_pivot(&$results) { foreach ($results as &$result) { @@ -288,17 +290,19 @@ protected function pivot(&$results) // the pivot table that may need to be accessed by the developer. $pivot = new Pivot($this->joining); + $attributes = array_filter($result->attributes, function($attribute) + { + return starts_with($attribute, 'pivot_'); + }); + // 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 // from the model since it actually belongs to the pivot. - foreach ($result->attributes as $key => $value) + foreach ($attributes as $key => $value) { - if (starts_with($key, 'pivot_')) - { - $pivot->{substr($key, 6)} = $value; + $pivot->{substr($key, 6)} = $value; - $result->purge($key); - } + $result->purge($key); } // Once we have completed hydrating the pivot model instance, we'll set @@ -330,6 +334,16 @@ public function with($columns) return $this; } + /** + * Get a model instance of the pivot table for the relationship. + * + * @return Pivot + */ + public function pivot() + { + return new Pivot($this->joining); + } + /** * Get the other or associated key for the relationship. *