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 {
}
/**
* Pass push to the query builder.
* Append one or more values to an array.
*
* @return mixed
*/
......@@ -254,6 +254,18 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
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.
*
......
......@@ -36,9 +36,7 @@ class BelongsToMany extends EloquentBelongsToMany {
{
if (static::$constraints)
{
// Make sure that the primary key of the parent
// is in the relationship array of keys
$this->query->whereIn($this->foreignKey, array($this->parent->getKey()));
$this->query->where($this->foreignKey, $this->parent->getKey());
}
}
......@@ -114,15 +112,6 @@ class BelongsToMany extends EloquentBelongsToMany {
{
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);
// Get the ID's to attach to the two documents
......@@ -130,10 +119,18 @@ class BelongsToMany extends EloquentBelongsToMany {
$foreignIds = array_pluck($records, $this->foreignKey);
// 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
$related->push($this->foreignKey, $foreignIds[0]);
$query->push($this->foreignKey, $foreignIds[0]);
if ($touch) $this->touchIfTouching();
}
/**
......@@ -168,47 +165,32 @@ class BelongsToMany extends EloquentBelongsToMany {
{
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
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
$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
// to run the delete on the pivot table. Then, if the touch parameter
// is true, we will go ahead and touch all related models to sync.
foreach ($ids as $id)
// Prepare the query to select all related objects.
if (count($ids) > 0)
{
$query->pull($this->otherKey, $id);
$query->whereIn($this->related->getKeyName(), $ids);
}
// Remove the relation from the related model
$related->pull($this->foreignKey, $this->parent->getKey());
return count($ids);
}
// Remove the relation to the parent.
$query->pull($this->foreignKey, $this->parent->getKey());
/**
* Create a new query builder for the parent
*
* @return Jenssegers\Mongodb\Builder
*/
protected function newParentQuery()
{
$query = $this->parent->newQuery();
if ($touch) $this->touchIfTouching();
return $query->where($this->parent->getKeyName(), '=', $this->parent->getKey());
return count($ids);
}
/**
......@@ -237,6 +219,16 @@ class BelongsToMany extends EloquentBelongsToMany {
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.
*
......
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