From aa341357ece9eb65ffef75f890656e9a2db50592 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 26 Aug 2012 01:10:31 +0300 Subject: [PATCH 1/2] Fix insert() method for related models. --- .../eloquent/relationships/has_one_or_many.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/laravel/database/eloquent/relationships/has_one_or_many.php b/laravel/database/eloquent/relationships/has_one_or_many.php index a8268de6..7dd453b3 100644 --- a/laravel/database/eloquent/relationships/has_one_or_many.php +++ b/laravel/database/eloquent/relationships/has_one_or_many.php @@ -12,11 +12,18 @@ class Has_One_Or_Many extends Relationship { */ public function insert($attributes) { - $attributes = ($attributes instanceof Model) ? $attributes->attributes : $attributes; + if ($attributes instanceof Model) + { + $attributes->set_attribute($this->foreign_key(), $this->base->get_key()); + + return $attributes->save(); + } + else + { + $attributes[$this->foreign_key()] = $this->base->get_key(); - $attributes[$this->foreign_key()] = $this->base->get_key(); - - return $this->model->create($attributes); + return $this->model->create($attributes); + } } /** From e46f07d4365222b04fd654cada7a1940fc2ec4fd Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Mon, 27 Aug 2012 13:46:32 +0300 Subject: [PATCH 2/2] Fix method signature and return values of insert() method. --- laravel/database/eloquent/relationships/has_one_or_many.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/laravel/database/eloquent/relationships/has_one_or_many.php b/laravel/database/eloquent/relationships/has_one_or_many.php index 7dd453b3..e7bfc0b0 100644 --- a/laravel/database/eloquent/relationships/has_one_or_many.php +++ b/laravel/database/eloquent/relationships/has_one_or_many.php @@ -7,8 +7,10 @@ class Has_One_Or_Many extends Relationship { /** * Insert a new record for the association. * + * If save is successful, the model will be returned, otherwise false. + * * @param Model|array $attributes - * @return bool + * @return Model|false */ public function insert($attributes) { @@ -16,7 +18,7 @@ public function insert($attributes) { $attributes->set_attribute($this->foreign_key(), $this->base->get_key()); - return $attributes->save(); + return $attributes->save() ? $attributes : false; } else {