diff --git a/laravel/database/eloquent/relationships/belongs_to.php b/laravel/database/eloquent/relationships/belongs_to.php index 4ee38543..b73c57bf 100644 --- a/laravel/database/eloquent/relationships/belongs_to.php +++ b/laravel/database/eloquent/relationships/belongs_to.php @@ -15,11 +15,13 @@ public function results() /** * Update the parent model of the relationship. * - * @param array $attributes + * @param Model|array $attributes * @return int */ public function update($attributes) { + $attributes = ($attributes instanceof Model) ? $attributes->get_dirty() : $attributes; + return $this->model->update($this->foreign_value(), $attributes); } 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 23b8765a..169e1f12 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -79,7 +79,7 @@ public function results() * @param array $joining * @return bool */ - public function add($id, $attributes = array()) + public function attach($id, $attributes = array()) { $joining = array_merge($this->join_record($id), $attributes); @@ -89,12 +89,20 @@ public function add($id, $attributes = array()) /** * Insert a new record for the association. * - * @param array $attributes - * @param array $joining + * @param Model|array $attributes + * @param array $joining * @return bool */ public function insert($attributes, $joining = array()) { + // If the attributes are actually an instance of a model, we'll just grab the + // array of attributes off of the model for saving, allowing the developer + // to easily validate the joining models before inserting them. + if ($attributes instanceof Model) + { + $attributes = $attributes->attributes; + } + $model = $this->model->create($attributes); // If the insert was successful, we'll insert a record into the joining table @@ -139,9 +147,6 @@ protected function join_record($id) */ protected function insert_joining($attributes) { - // All joining tables get creation and update timestamps automatically even though - // some developers may not need them. This just provides them if necessary since - // it would be a pain for the developer to maintain them each manually. $attributes['created_at'] = $this->model->get_timestamp(); $attributes['updated_at'] = $attributes['created_at']; diff --git a/laravel/database/eloquent/relationships/has_one_or_many.php b/laravel/database/eloquent/relationships/has_one_or_many.php index 3c9ad4a6..2cdac83e 100644 --- a/laravel/database/eloquent/relationships/has_one_or_many.php +++ b/laravel/database/eloquent/relationships/has_one_or_many.php @@ -7,11 +7,13 @@ class Has_One_Or_Many extends Relationship { /** * Insert a new record for the association. * - * @param array $attributes + * @param Model|array $attributes * @return bool */ public function insert($attributes) { + $attributes = ($attributes instanceof Model) ? $attributes->attributes : $attributes; + $attributes[$this->foreign_key()] = $this->base->get_key(); return $this->model->create($attributes);