Browse Source

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

Taylor Otwell 12 years ago
parent
commit
cd71c1e517
1 changed files with 45 additions and 0 deletions
  1. 45 0
      laravel/database/eloquent/relationships/has_many_and_belongs_to.php

+ 45 - 0
laravel/database/eloquent/relationships/has_many_and_belongs_to.php

@@ -86,6 +86,51 @@ class Has_Many_And_Belongs_To extends Relationship {
 		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.
 	 *