Commit cd71c1e5 authored by Taylor Otwell's avatar Taylor Otwell

added sync method to has many and belongs to entity relationship.

parent c6125edc
...@@ -86,6 +86,51 @@ class Has_Many_And_Belongs_To extends Relationship { ...@@ -86,6 +86,51 @@ class Has_Many_And_Belongs_To extends Relationship {
return $this->insert_joining($joining); return $this->insert_joining($joining);
} }
/**
* Detach a record from the joining table of the association.
*
* @param int $ids
* @return bool
*/
public function detach($ids)
{
if ( ! is_array($ids)) $ids = array($ids);
return $this->pivot()->where_in($this->other_key(), $ids)->delete();
}
/**
* Sync the joining table with the array of given IDs.
*
* @param array $ids
* @return bool
*/
public function sync($ids)
{
$current = $this->pivot()->lists($this->other_key());
// First we need to attach any of the associated models that are not currently
// in the joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we insert.
foreach ($ids as $id)
{
if ( ! in_array($id, $current))
{
$this->attach($id);
}
}
// Next we will take the difference of the current and given IDs and detach
// all of the entities that exists in the current array but are not in
// the array of IDs given to the method, finishing the sync.
$detach = array_diff($current, $ids);
if (count($detach) > 0)
{
$this->detach(array_diff($current, $ids));
}
}
/** /**
* Insert a new record for the association. * Insert a new record for the association.
* *
......
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