Commit 8304a46f authored by Jens Segers's avatar Jens Segers

Small tweaks

parent 593b4a52
...@@ -14,12 +14,13 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -14,12 +14,13 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
protected function hydratePivotRelation(array $models) protected function hydratePivotRelation(array $models)
{ {
// Do nothing // Do nothing.
} }
/** /**
* Set the select clause for the relation query. * Set the select clause for the relation query.
* *
* @param array $columns
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/ */
protected function getSelectColumns(array $columns = array('*')) protected function getSelectColumns(array $columns = array('*'))
...@@ -34,10 +35,21 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -34,10 +35,21 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
public function addConstraints() public function addConstraints()
{ {
if (static::$constraints) if (static::$constraints) $this->setWhere();
{
$this->query->where($this->getForeignKey(), '=', $this->parent->getKey());
} }
/**
* Set the where clause for the relation query.
*
* @return $this
*/
protected function setWhere()
{
$foreign = $this->getForeignKey();
$this->query->where($foreign, '=', $this->parent->getKey());
return $this;
} }
/** /**
...@@ -82,7 +94,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -82,7 +94,7 @@ class BelongsToMany extends EloquentBelongsToMany {
/** /**
* Sync the intermediate tables with a list of IDs or collection of models. * Sync the intermediate tables with a list of IDs or collection of models.
* *
* @param mixed $ids * @param array $ids
* @param bool $detaching * @param bool $detaching
* @return array * @return array
*/ */
...@@ -104,7 +116,11 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -104,7 +116,11 @@ class BelongsToMany extends EloquentBelongsToMany {
$records = $this->formatSyncList($ids); $records = $this->formatSyncList($ids);
$detach = array_values(array_diff($current, array_keys($records))); $detach = array_diff($current, array_keys($records));
// We need to make sure we pass a clean array, so that it is not interpreted
// as an associative array.
$detach = array_values($detach);
// Next, we will take the differences of the currents and given IDs and detach // Next, we will take the differences of the currents and given IDs and detach
// all of the entities that exist in the "current" array but are not in the // all of the entities that exist in the "current" array but are not in the
...@@ -113,7 +129,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -113,7 +129,7 @@ class BelongsToMany extends EloquentBelongsToMany {
{ {
$this->detach($detach); $this->detach($detach);
$changes['detached'] = (array) array_map('intval', $detach); $changes['detached'] = (array) array_map(function($v) { return (int) $v; }, $detach);
} }
// Now we are finally ready to attach the new records. Note that we'll disable // Now we are finally ready to attach the new records. Note that we'll disable
...@@ -156,33 +172,26 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -156,33 +172,26 @@ class BelongsToMany extends EloquentBelongsToMany {
{ {
if ($id instanceof Model) if ($id instanceof Model)
{ {
$model = $id; $id = $model->getKey(); $model = $id;
}
$ids = (array) $id;
// Attach the new ids to the parent model. $id = $model->getKey();
$this->parent->push($this->otherKey, $ids, true);
// If we have a model instance, we can push the ids to that model, // Attach the new parent id to the related model.
// so that the internal attributes are updated as well. Otherwise,
// we will just perform a regular database query.
if (isset($model))
{
// Attach the new ids to the related model.
$model->push($this->foreignKey, $this->parent->getKey(), true); $model->push($this->foreignKey, $this->parent->getKey(), true);
} }
else else
{ {
$query = $this->newRelatedQuery(); $query = $this->newRelatedQuery();
// Select related models. $query->whereIn($this->related->getKeyName(), (array) $id);
$query->whereIn($this->related->getKeyName(), $ids);
// Attach the new parent id to the related model. // Attach the new parent id to the related model.
$query->push($this->foreignKey, $this->parent->getKey(), true); $query->push($this->foreignKey, $this->parent->getKey(), true);
} }
// Attach the new ids to the parent model.
$this->parent->push($this->otherKey, (array) $id, true);
if ($touch) $this->touchIfTouching(); if ($touch) $this->touchIfTouching();
} }
...@@ -238,9 +247,9 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -238,9 +247,9 @@ class BelongsToMany extends EloquentBelongsToMany {
foreach ($results as $result) foreach ($results as $result)
{ {
foreach ($result->$foreign as $single) foreach ($result->$foreign as $item)
{ {
$dictionary[$single][] = $result; $dictionary[$item][] = $result;
} }
} }
......
...@@ -21,15 +21,4 @@ class MorphTo extends EloquentMorphTo { ...@@ -21,15 +21,4 @@ class MorphTo extends EloquentMorphTo {
} }
} }
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
$this->buildDictionary($this->models = Collection::make($models));
}
} }
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