Skip to content

Commit 4cabe86

Browse files
committed
Added Attrs Implementation
1 parent 7cb8fea commit 4cabe86

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Database\Eloquent\Collection;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Model as EloquentModel;
89
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
910
use Illuminate\Support\Arr;
10-
use Illuminate\Database\Eloquent\Model as EloquentModel;
1111

1212
class BelongsToMany extends EloquentBelongsToMany
1313
{
@@ -34,7 +34,13 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
3434
*/
3535
protected function hydratePivotRelation(array $models)
3636
{
37-
// Do nothing.
37+
foreach ($models as $model) {
38+
$keyToUse = $this->getTable() == $model->getTable() ? $this->getForeignKey() : $this->getRelatedKey();
39+
$pcontent = $model->{$keyToUse};
40+
$model->setRelation($this->accessor, $this->newExistingPivot(
41+
$pcontent[0]
42+
));
43+
}
3844
}
3945

4046
/**
@@ -74,8 +80,10 @@ public function addConstraints()
7480
protected function setWhere()
7581
{
7682
$foreign = $this->getForeignKey();
77-
78-
$this->query->where($foreign, '=', $this->parent->getKey());
83+
$key = $this->parent->getKey();
84+
$this->query
85+
->where($foreign, '=', $key)
86+
->orWhereRaw([$foreign.'._id' => $key]);
7987

8088
return $this;
8189
}
@@ -132,12 +140,17 @@ public function sync($ids, $detaching = true)
132140
// See issue #256.
133141
if ($current instanceof Collection) {
134142
$current = $ids->modelKeys();
143+
} elseif (is_array($current)) {
144+
foreach ($current as $key => $value) {
145+
if ($value['_id']) {
146+
$current[$key] = $value['_id'];
147+
}
148+
}
135149
}
136150

137151
$records = $this->formatSyncList($ids);
138152

139153
$current = Arr::wrap($current);
140-
141154
$detach = array_diff($current, array_keys($records));
142155

143156
// We need to make sure we pass a clean array, so that it is not interpreted
@@ -188,7 +201,7 @@ public function attach($id, array $attributes = [], $touch = true)
188201
$id = $model->getKey();
189202

190203
// Attach the new parent id to the related model.
191-
$model->push($this->foreignPivotKey, $this->parent->getKey(), true);
204+
$model->push($this->foreignPivotKey, array_merge($attributes, ['_id' => $this->parent->getKey()]), true);
192205
} else {
193206
if ($id instanceof Collection) {
194207
$id = $id->modelKeys();
@@ -199,11 +212,17 @@ public function attach($id, array $attributes = [], $touch = true)
199212
$query->whereIn($this->related->getKeyName(), (array) $id);
200213

201214
// Attach the new parent id to the related model.
202-
$query->push($this->foreignPivotKey, $this->parent->getKey(), true);
215+
$query->push($this->foreignPivotKey, array_merge($attributes, ['_id' => $this->parent->getKey()]), true);
216+
}
217+
218+
//Pivot Collection
219+
$pivot_x = [];
220+
foreach ((array)$id as $item) {
221+
$pivot_x[] = array_merge($attributes, ['_id' => $item]);
203222
}
204223

205224
// Attach the new ids to the parent model.
206-
$this->parent->push($this->getRelatedKey(), (array) $id, true);
225+
$this->parent->push($this->getRelatedKey(), $pivot_x, true);
207226

208227
if ($touch) {
209228
$this->touchIfTouching();
@@ -227,15 +246,15 @@ public function detach($ids = [], $touch = true)
227246
$ids = (array) $ids;
228247

229248
// Detach all ids from the parent model.
230-
$this->parent->pull($this->getRelatedKey(), $ids);
249+
$this->parent->pull($this->getRelatedKey(), ['_id'=>$ids]);
231250

232251
// Prepare the query to select all related objects.
233252
if (count($ids) > 0) {
234253
$query->whereIn($this->related->getKeyName(), $ids);
235254
}
236255

237256
// Remove the relation to the parent.
238-
$query->pull($this->foreignPivotKey, $this->parent->getKey());
257+
$query->pull($this->foreignPivotKey, ['_id'=>$this->parent->getKey()]);
239258

240259
if ($touch) {
241260
$this->touchIfTouching();

0 commit comments

Comments
 (0)