Commit 079400ff authored by Taylor Otwell's avatar Taylor Otwell

Allow passing in a model instance to relationship insert / update methods.

parent e540fd3b
......@@ -15,11 +15,13 @@ class Belongs_To extends Relationship {
/**
* 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);
}
......
......@@ -79,7 +79,7 @@ class Has_Many_And_Belongs_To extends Relationship {
* @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 @@ class Has_Many_And_Belongs_To extends Relationship {
/**
* Insert a new record for the association.
*
* @param array $attributes
* @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 @@ class Has_Many_And_Belongs_To extends Relationship {
*/
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'];
......
......@@ -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);
......
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