Commit c1af77aa authored by Jens Segers's avatar Jens Segers

Fix belongsToMany, fixes #147

parent 92b2299c
...@@ -238,7 +238,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -238,7 +238,7 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
} }
/** /**
* Pass push to the query builder. * Append one or more values to an array.
* *
* @return mixed * @return mixed
*/ */
...@@ -254,6 +254,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -254,6 +254,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
return parent::push(); return parent::push();
} }
/**
* Remove one or more values from an array.
*
* @return mixed
*/
public function pull()
{
$query = $this->setKeysForSaveQuery($this->newQuery());
return call_user_func_array(array($query, 'pull'), func_get_args());
}
/** /**
* Create a new Eloquent query builder for the model. * Create a new Eloquent query builder for the model.
* *
......
...@@ -36,9 +36,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -36,9 +36,7 @@ class BelongsToMany extends EloquentBelongsToMany {
{ {
if (static::$constraints) if (static::$constraints)
{ {
// Make sure that the primary key of the parent $this->query->where($this->foreignKey, $this->parent->getKey());
// is in the relationship array of keys
$this->query->whereIn($this->foreignKey, array($this->parent->getKey()));
} }
} }
...@@ -114,15 +112,6 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -114,15 +112,6 @@ class BelongsToMany extends EloquentBelongsToMany {
{ {
if ($id instanceof Model) $id = $id->getKey(); if ($id instanceof Model) $id = $id->getKey();
// Generate a new parent query instance
$parent = $this->newParentQuery();
// Generate a new related query instance
$related = $this->related->newInstance();
// Set contraints on the related query
$related = $related->where($this->related->getKeyName(), $id);
$records = $this->createAttachRecords((array) $id, $attributes); $records = $this->createAttachRecords((array) $id, $attributes);
// Get the ID's to attach to the two documents // Get the ID's to attach to the two documents
...@@ -130,10 +119,18 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -130,10 +119,18 @@ class BelongsToMany extends EloquentBelongsToMany {
$foreignIds = array_pluck($records, $this->foreignKey); $foreignIds = array_pluck($records, $this->foreignKey);
// Attach to the parent model // Attach to the parent model
$parent->push($this->otherKey, $otherIds[0]); $this->parent->push($this->otherKey, $otherIds[0]);
// Generate a new related query instance
$query = $this->getNewRelatedQuery();
// Set contraints on the related query
$query->where($this->related->getKeyName(), $id);
// Attach to the related model // Attach to the related model
$related->push($this->foreignKey, $foreignIds[0]); $query->push($this->foreignKey, $foreignIds[0]);
if ($touch) $this->touchIfTouching();
} }
/** /**
...@@ -168,47 +165,32 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -168,47 +165,32 @@ class BelongsToMany extends EloquentBelongsToMany {
{ {
if ($ids instanceof Model) $ids = (array) $ids->getKey(); if ($ids instanceof Model) $ids = (array) $ids->getKey();
$query = $this->newParentQuery();
// Generate a new related query instance
$related = $this->related->newInstance();
// If associated IDs were passed to the method we will only delete those // If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken. // associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes. // We'll return the numbers of affected rows when we do the deletes.
$ids = (array) $ids; $ids = (array) $ids;
if (count($ids) > 0) // Pull each id from the parent.
foreach ($ids as $id)
{ {
$query->whereIn($this->otherKey, $ids); $this->parent->pull($this->otherKey, $id);
} }
if ($touch) $this->touchIfTouching(); // Get a new related query.
$query = $this->getNewRelatedQuery();
// Once we have all of the conditions set on the statement, we are ready // Prepare the query to select all related objects.
// to run the delete on the pivot table. Then, if the touch parameter if (count($ids) > 0)
// is true, we will go ahead and touch all related models to sync.
foreach ($ids as $id)
{ {
$query->pull($this->otherKey, $id); $query->whereIn($this->related->getKeyName(), $ids);
} }
// Remove the relation from the related model // Remove the relation to the parent.
$related->pull($this->foreignKey, $this->parent->getKey()); $query->pull($this->foreignKey, $this->parent->getKey());
return count($ids);
}
/** if ($touch) $this->touchIfTouching();
* Create a new query builder for the parent
*
* @return Jenssegers\Mongodb\Builder
*/
protected function newParentQuery()
{
$query = $this->parent->newQuery();
return $query->where($this->parent->getKeyName(), '=', $this->parent->getKey()); return count($ids);
} }
/** /**
...@@ -237,6 +219,16 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -237,6 +219,16 @@ class BelongsToMany extends EloquentBelongsToMany {
return $dictionary; return $dictionary;
} }
/**
* Get a new related query.
*
* @return \Illuminate\Database\Query\Builder
*/
public function getNewRelatedQuery()
{
return $this->related->newQuery();
}
/** /**
* Get the fully qualified foreign key for the relation. * Get the fully qualified foreign key for the relation.
* *
......
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