Allow passing in a model instance to relationship insert / update methods.
This commit is contained in:
parent
e540fd3b6d
commit
079400ff3d
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 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'];
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue