diff --git a/laravel/database/eloquent/model.php b/laravel/database/eloquent/model.php index 0b624051..f1b7d74b 100644 --- a/laravel/database/eloquent/model.php +++ b/laravel/database/eloquent/model.php @@ -174,6 +174,22 @@ public static function create($attributes) return ($success) ? $model : false; } + /** + * Update a model instance in the database. + * + * @param mixed $id + * @param array $attributes + * @return int + */ + public static function update($id, $attributes) + { + $model = new static(array(), true); + + if (static::$timestamps) $attributes['updated_at'] = $model->get_timestamp(); + + return $model->query()->where($model->key(), '=', $id)->update($attributes); + } + /** * Find a model by its primary key. * diff --git a/laravel/database/eloquent/relationships/belongs_to.php b/laravel/database/eloquent/relationships/belongs_to.php index 97c001e8..4ee38543 100644 --- a/laravel/database/eloquent/relationships/belongs_to.php +++ b/laravel/database/eloquent/relationships/belongs_to.php @@ -12,6 +12,17 @@ public function results() return parent::first(); } + /** + * Update the parent model of the relationship. + * + * @param array $attributes + * @return int + */ + public function update($attributes) + { + return $this->model->update($this->foreign_value(), $attributes); + } + /** * Set the proper constraints on the relationship table. * @@ -19,9 +30,7 @@ public function results() */ protected function constrain() { - $foreign = $this->base->get_attribute($this->foreign); - - $this->table->where($this->base->key(), '=', $foreign); + $this->table->where($this->base->key(), '=', $this->foreign_value()); } /** @@ -80,4 +89,14 @@ public function match($relationship, &$children, $parents) } } + /** + * Get the value of the foreign key from the base model. + * + * @return mixed + */ + public function foreign_value() + { + return $this->base->get_attribute($this->foreign); + } + } \ 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 95989e9e..7f3c01b0 100644 --- a/laravel/database/eloquent/relationships/has_many_and_belongs_to.php +++ b/laravel/database/eloquent/relationships/has_many_and_belongs_to.php @@ -114,7 +114,9 @@ public function insert($attributes, $joining = array()) */ public function delete() { - return $this->joining_table()->where($this->foreign_key(), '=', $this->base->get_key())->delete(); + $id = $this->base->get_key(); + + return $this->joining_table()->where($this->foreign_key(), '=', $id)->delete(); } /** diff --git a/laravel/database/eloquent/relationships/relationship.php b/laravel/database/eloquent/relationships/relationship.php index 8d1ebf1b..1fcdc5a4 100644 --- a/laravel/database/eloquent/relationships/relationship.php +++ b/laravel/database/eloquent/relationships/relationship.php @@ -57,7 +57,7 @@ public static function foreign($model, $foreign = null) { if ( ! is_null($foreign)) return $foreign; - // If the model is an object, we will simply get the class of the object and + // If the model is an object we'll simply get the class of the object and // then take the basename, which is simply the object name minus the // namespace, and we'll append "_id" to the name. if (is_object($model))