Commit 46f68ab7 authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #813 from anaxamaxan/patch-3

Allow Model instance or id for first argument in Has_Many_And_Belongs_To::attach()
parents 8f8fa097 19a3e9dc
...@@ -85,12 +85,14 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -85,12 +85,14 @@ class Has_Many_And_Belongs_To extends Relationship {
/** /**
* Insert a new record into the joining table of the association. * Insert a new record into the joining table of the association.
* *
* @param int $id * @param Model|int $id
* @param array $joining * @param array $attributes
* @return bool * @return bool
*/ */
public function attach($id, $attributes = array()) public function attach($id, $attributes = array())
{ {
if ($id instanceof Model) $id = $id->get_key();
$joining = array_merge($this->join_record($id), $attributes); $joining = array_merge($this->join_record($id), $attributes);
return $this->insert_joining($joining); return $this->insert_joining($joining);
...@@ -99,12 +101,13 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -99,12 +101,13 @@ class Has_Many_And_Belongs_To extends Relationship {
/** /**
* Detach a record from the joining table of the association. * Detach a record from the joining table of the association.
* *
* @param int $ids * @param array|Model|int $ids
* @return bool * @return bool
*/ */
public function detach($ids) public function detach($ids)
{ {
if ( ! is_array($ids)) $ids = array($ids); if ($ids instanceof Model) $ids = array($ids->get_key());
elseif ( ! is_array($ids)) $ids = array($ids);
return $this->pivot()->where_in($this->other_key(), $ids)->delete(); return $this->pivot()->where_in($this->other_key(), $ids)->delete();
} }
......
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