|
@@ -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());
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ foreach ($ids as $id)
|
|
|
+ {
|
|
|
+ if ( ! in_array($id, $current))
|
|
|
+ {
|
|
|
+ $this->attach($id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $detach = array_diff($current, $ids);
|
|
|
+
|
|
|
+ if (count($detach) > 0)
|
|
|
+ {
|
|
|
+ $this->detach(array_diff($current, $ids));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
* Insert a new record for the association.
|
|
|
*
|
|
@@ -194,7 +239,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
|
$this->with = array_merge($this->with, array($foreign, $other));
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
foreach ($this->with as $column)
|
|
|
{
|
|
@@ -269,9 +314,14 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
|
{
|
|
|
$foreign = $this->foreign_key();
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
foreach ($children as $key => $child)
|
|
|
{
|
|
|
- $parents[$child->pivot->$foreign]->relationships[$relationship][$child->{$child->key()}] = $child;
|
|
|
+ $parent =& $parents[$child->pivot->$foreign];
|
|
|
+
|
|
|
+ $parent->relationships[$relationship][$child->{$child->key()}] = $child;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -292,7 +342,7 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
foreach ($result->attributes as $key => $value)
|
|
|
{
|
|
|
if (starts_with($key, 'pivot_'))
|
|
@@ -322,9 +372,9 @@ class Has_Many_And_Belongs_To extends Relationship {
|
|
|
{
|
|
|
$columns = (is_array($columns)) ? $columns : func_get_args();
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
$this->with = array_unique(array_merge($this->with, $columns));
|
|
|
|
|
|
$this->set_select($this->foreign_key(), $this->other_key());
|