-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Updated PR#1796] Add Attributes to the Pivot Embed #1952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
206f0f9
c531b96
26cda8b
0155cca
fdfe093
0c71f0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,13 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, | |
*/ | ||
protected function hydratePivotRelation(array $models) | ||
{ | ||
// Do nothing. | ||
foreach ($models as $model) { | ||
$keyToUse = $this->getTable() == $model->getTable() ? $this->getForeignKey() : $this->getRelatedKey(); | ||
$pcontent = $model->getAttributes()[$keyToUse]; | ||
$model->setRelation($this->accessor, $this->newExistingPivot( | ||
is_string($pcontent[0]) ? ['_id' => $pcontent] : $pcontent[0] | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -71,8 +77,10 @@ public function addConstraints() | |
protected function setWhere() | ||
{ | ||
$foreign = $this->getForeignKey(); | ||
|
||
$this->query->where($foreign, '=', $this->parent->getKey()); | ||
$key = $this->parent->getKey(); | ||
$this->query | ||
->where($foreign, '=', $key) | ||
->orWhereRaw([$foreign.'._id' => $key]); | ||
|
||
return $this; | ||
} | ||
|
@@ -129,6 +137,12 @@ public function sync($ids, $detaching = true) | |
// See issue #256. | ||
if ($current instanceof Collection) { | ||
$current = $ids->modelKeys(); | ||
} elseif (is_array($current)) { | ||
foreach ($current as $key => $value) { | ||
if (is_array($value) && $value['_id']) { | ||
$current[$key] = $value['_id']; | ||
} | ||
} | ||
} | ||
|
||
$records = $this->formatSyncList($ids); | ||
|
@@ -171,7 +185,30 @@ public function sync($ids, $detaching = true) | |
*/ | ||
public function updateExistingPivot($id, array $attributes, $touch = true) | ||
{ | ||
// Do nothing, we have no pivot table. | ||
if ($id instanceof Model) { | ||
$model = $id; | ||
$id = $model->getKey(); | ||
} else { | ||
if ($id instanceof Collection) { | ||
$id = $id->modelKeys(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, there is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that author of parent PR don't see this function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use this function and remove this code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I don't like this implementation at all, it has a tests but implementation isn't following framework conventions, I think it needs complete rewrite from scratch, maybe close this at all? |
||
} | ||
|
||
$related = $this->newRelatedQuery()->whereIn($this->related->getKeyName(), (array) $id); | ||
$filter = [$this->parentKey => $this->parent->getKey()]; | ||
$pivot_x = [array_merge($attributes, $filter)]; | ||
|
||
//TODO: Put this in a transaction | ||
$related->pull($this->getForeignKey(), $this->parent->getKey()); | ||
$related->pull($this->getForeignKey(), $filter); | ||
$related->push($this->getForeignKey(), $pivot_x, true); | ||
} | ||
$filter = [$this->parentKey => $id]; | ||
$pivot_x = [array_merge($attributes, $filter)]; | ||
|
||
//TODO: Put this in a transaction | ||
$this->parent->pull($this->getRelatedKey(), $id); | ||
$this->parent->pull($this->getRelatedKey(), $filter); | ||
$this->parent->push($this->getRelatedKey(), $pivot_x, true); | ||
} | ||
|
||
/** | ||
|
@@ -185,22 +222,30 @@ public function attach($id, array $attributes = [], $touch = true) | |
$id = $model->getKey(); | ||
|
||
// Attach the new parent id to the related model. | ||
$model->push($this->foreignPivotKey, $this->parent->getKey(), true); | ||
$model->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); | ||
} else { | ||
if ($id instanceof Collection) { | ||
$id = $id->modelKeys(); | ||
} | ||
|
||
$query = $this->newRelatedQuery(); | ||
|
||
$query->whereIn($this->related->getKeyName(), (array) $id); | ||
$query | ||
->whereIn($this->related->getKeyName(), (array) $id) | ||
->orWhereIn($this->related->getKeyName().'._id', (array) $id); | ||
|
||
// Attach the new parent id to the related model. | ||
$query->push($this->foreignPivotKey, $this->parent->getKey(), true); | ||
$query->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); | ||
} | ||
|
||
//Pivot Collection | ||
$pivot_x = []; | ||
foreach ((array) $id as $item) { | ||
divine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$pivot_x[] = array_merge($attributes, ['_id' => $item]); | ||
} | ||
|
||
// Attach the new ids to the parent model. | ||
$this->parent->push($this->getRelatedKey(), (array) $id, true); | ||
$this->parent->push($this->getRelatedKey(), $pivot_x, true); | ||
|
||
if ($touch) { | ||
$this->touchIfTouching(); | ||
|
@@ -224,7 +269,9 @@ public function detach($ids = [], $touch = true) | |
$ids = (array) $ids; | ||
|
||
// Detach all ids from the parent model. | ||
// Legacy Support | ||
$this->parent->pull($this->getRelatedKey(), $ids); | ||
$this->parent->pull($this->getRelatedKey(), ['_id' => ['$in' => $ids]]); | ||
|
||
// Prepare the query to select all related objects. | ||
if (count($ids) > 0) { | ||
|
@@ -233,6 +280,7 @@ public function detach($ids = [], $touch = true) | |
|
||
// Remove the relation to the parent. | ||
$query->pull($this->foreignPivotKey, $this->parent->getKey()); | ||
$query->pull($this->foreignPivotKey, [$this->parentKey => $this->parent->getKey()]); | ||
|
||
if ($touch) { | ||
$this->touchIfTouching(); | ||
|
@@ -255,7 +303,11 @@ protected function buildDictionary(Collection $results) | |
|
||
foreach ($results as $result) { | ||
foreach ($result->$foreign as $item) { | ||
$dictionary[$item][] = $result; | ||
if (is_array($item)) { | ||
$dictionary[$item['_id']][] = $result; | ||
} else { | ||
$dictionary[$item][] = $result; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -342,4 +394,15 @@ protected function whereInMethod(EloquentModel $model, $key) | |
{ | ||
return 'whereIn'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
divine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function addEagerConstraints(array $models) | ||
{ | ||
$keys = $this->getKeys($models, $this->parentKey); | ||
$this->query | ||
->whereIn($this->getQualifiedForeignPivotKeyName(), $keys) | ||
->orWhereRaw([$this->getQualifiedForeignPivotKeyName().'._id' => ['$in' => $keys]]); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.