Commit fcff36a0 authored by Taylor Otwell's avatar Taylor Otwell

Added related model updating from belongs_to relationship.

parent 68b4e553
...@@ -174,6 +174,22 @@ abstract class Model { ...@@ -174,6 +174,22 @@ abstract class Model {
return ($success) ? $model : false; 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. * Find a model by its primary key.
* *
......
...@@ -12,6 +12,17 @@ class Belongs_To extends Relationship { ...@@ -12,6 +12,17 @@ class Belongs_To extends Relationship {
return parent::first(); 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. * Set the proper constraints on the relationship table.
* *
...@@ -19,9 +30,7 @@ class Belongs_To extends Relationship { ...@@ -19,9 +30,7 @@ class Belongs_To extends Relationship {
*/ */
protected function constrain() protected function constrain()
{ {
$foreign = $this->base->get_attribute($this->foreign); $this->table->where($this->base->key(), '=', $this->foreign_value());
$this->table->where($this->base->key(), '=', $foreign);
} }
/** /**
...@@ -80,4 +89,14 @@ class Belongs_To extends Relationship { ...@@ -80,4 +89,14 @@ class Belongs_To extends Relationship {
} }
} }
/**
* 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
...@@ -114,7 +114,9 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -114,7 +114,9 @@ class Has_Many_And_Belongs_To extends Relationship {
*/ */
public function delete() 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();
} }
/** /**
......
...@@ -57,7 +57,7 @@ abstract class Relationship extends Query { ...@@ -57,7 +57,7 @@ abstract class Relationship extends Query {
{ {
if ( ! is_null($foreign)) return $foreign; 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 // then take the basename, which is simply the object name minus the
// namespace, and we'll append "_id" to the name. // namespace, and we'll append "_id" to the name.
if (is_object($model)) if (is_object($model))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment