From d12bf6e54c248e302b1a6505fd8488f93c1c05fa Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Tue, 30 Jul 2019 21:27:12 +0100 Subject: [PATCH 01/20] Docker Composer Windows Compat --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index c6f20163e..1626c4fb4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: context: . dockerfile: Dockerfile volumes: - - .:/code + - ./:/code working_dir: /code depends_on: - mongodb From 7cb8fea3bba6d70d0944cdaad2407c6932118de7 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Tue, 30 Jul 2019 22:06:43 +0100 Subject: [PATCH 02/20] Added Attrs test --- tests/RelationsTest.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index de3e0f222..57e7544e6 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -280,6 +280,35 @@ public function testBelongsToManySync() $this->assertCount(1, $user->clients); } + public function testBelongsToManySyncAttrs() + { + // create test instances + /** @var User $user */ + $user = User::create(['name' => 'John Doe']); + $client1 = Client::create(['name' => 'Pork Pies Ltd.'])->_id; + $client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id; + + // Sync multiple + $user->clients()->sync([$client1 => ['fresh_client' => true], $client2 => ['fresh_client' => false]]); + + //Check Sync Success + $this->assertEquals($user->client_ids[0]['_id'], $client1); + $this->assertEquals($user->client_ids[1]['_id'], $client2); + + //Check reverse + $this->assertEquals($user->_id, Client::find($client1)->user_ids[0]['_id']); + $this->assertEquals($user->_id, Client::find($client2)->user_ids[0]['_id']); + + $clients = $user->clients; + $this->assertCount(2, $clients); + + foreach ($user->clients()->withPivot('fresh_client')->get() as $item) { + $this->assertIsArray($item->pivot->toArray()); + $this->assertEquals($item->_id == $client1, $item->pivot->fresh_client); + } + $this->assertTrue(true); + } + public function testBelongsToManyAttachArray() { $user = User::create(['name' => 'John Doe']); @@ -369,7 +398,7 @@ public function testMorph() $this->assertEquals($photo->imageable->name, $user->name); $user = User::with('photos')->find($user->_id); - $relations = $user->getRelations(); + $relations = $user->getRlations(); $this->assertArrayHasKey('photos', $relations); $this->assertEquals(1, $relations['photos']->count()); From 4cabe86b0ce99c828855bd4117532a0dd2ff28f7 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Tue, 30 Jul 2019 22:07:08 +0100 Subject: [PATCH 03/20] Added Attrs Implementation --- .../Mongodb/Relations/BelongsToMany.php | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 0e1b5280b..2bc45fad3 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -5,9 +5,9 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany; use Illuminate\Support\Arr; -use Illuminate\Database\Eloquent\Model as EloquentModel; class BelongsToMany extends EloquentBelongsToMany { @@ -34,7 +34,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->{$keyToUse}; + $model->setRelation($this->accessor, $this->newExistingPivot( + $pcontent[0] + )); + } } /** @@ -74,8 +80,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; } @@ -132,12 +140,17 @@ 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 ($value['_id']) { + $current[$key] = $value['_id']; + } + } } $records = $this->formatSyncList($ids); $current = Arr::wrap($current); - $detach = array_diff($current, array_keys($records)); // 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) $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(); @@ -199,11 +212,17 @@ public function attach($id, array $attributes = [], $touch = true) $query->whereIn($this->related->getKeyName(), (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) { + $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(); @@ -227,7 +246,7 @@ public function detach($ids = [], $touch = true) $ids = (array) $ids; // Detach all ids from the parent model. - $this->parent->pull($this->getRelatedKey(), $ids); + $this->parent->pull($this->getRelatedKey(), ['_id'=>$ids]); // Prepare the query to select all related objects. if (count($ids) > 0) { @@ -235,7 +254,7 @@ public function detach($ids = [], $touch = true) } // Remove the relation to the parent. - $query->pull($this->foreignPivotKey, $this->parent->getKey()); + $query->pull($this->foreignPivotKey, ['_id'=>$this->parent->getKey()]); if ($touch) { $this->touchIfTouching(); From 8e57b37fe2e488fd3669942fee7ac6ffc99c79b3 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Wed, 31 Jul 2019 13:49:40 +0100 Subject: [PATCH 04/20] Syncing --- .../Mongodb/Relations/BelongsTo.php | 35 ++++++++++++++++++- .../Mongodb/Relations/BelongsToMany.php | 20 +++++++---- src/Jenssegers/Mongodb/Relations/HasMany.php | 6 ++-- tests/RelationsTest.php | 17 ++++----- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 457e6bc1f..0975d03f0 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo @@ -26,7 +27,9 @@ public function addConstraints() // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. - $this->query->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey}); + $this->query + ->where($this->getOwnerKey(), '=', $this->parent->{$this->foreignKey}) + ->orWhere($this->getOwnerKey().'._id', '=', $this->parent->{$this->foreignKey}); } } @@ -72,4 +75,34 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @inheritDoc + */ + public function match(array $models, Collection $results, $relation) + { + $foreign = $this->foreignKey; + + $owner = $this->ownerKey; + + // First we will get to build a dictionary of the child models by their primary + // key of the relationship, then we can easily match the children back onto + // the parents using that dictionary and the primary key of the children. + $dictionary = []; + + foreach ($results as $result) { + $dictionary[$result->getAttribute($owner)] = $result; + } + + // Once we have the dictionary constructed, we can loop through all the parents + // and match back onto their children using these keys of the dictionary and + // the primary key of the children to map them onto the correct instances. + foreach ($models as $model) { + if (isset($dictionary[(string)$model->{$foreign}])) { + $model->setRelation($relation, $dictionary[(string)$model->{$foreign}]); + } + } + + return $models; + } } diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 2bc45fad3..6288a78e5 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -141,8 +141,9 @@ public function sync($ids, $detaching = true) if ($current instanceof Collection) { $current = $ids->modelKeys(); } elseif (is_array($current)) { + foreach ($current as $key => $value) { - if ($value['_id']) { + if (is_array($value) && $value['_id']) { $current[$key] = $value['_id']; } } @@ -201,7 +202,7 @@ public function attach($id, array $attributes = [], $touch = true) $id = $model->getKey(); // Attach the new parent id to the related model. - $model->push($this->foreignPivotKey, array_merge($attributes, ['_id' => $this->parent->getKey()]), true); + $model->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); } else { if ($id instanceof Collection) { $id = $id->modelKeys(); @@ -209,10 +210,12 @@ public function attach($id, array $attributes = [], $touch = true) $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, array_merge($attributes, ['_id' => $this->parent->getKey()]), true); + $query->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); } //Pivot Collection @@ -246,14 +249,19 @@ public function detach($ids = [], $touch = true) $ids = (array) $ids; // Detach all ids from the parent model. - $this->parent->pull($this->getRelatedKey(), ['_id'=>$ids]); + // 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) { - $query->whereIn($this->related->getKeyName(), $ids); + $query + ->whereIn($this->related->getKeyName(), $ids) + ->orWhereIn($this->related->getKeyName().'._id', $ids); } // Remove the relation to the parent. + $query->pull($this->foreignPivotKey, $this->parent->getKey()); $query->pull($this->foreignPivotKey, ['_id'=>$this->parent->getKey()]); if ($touch) { diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index 93a25425a..03f3cd2d3 100644 --- a/src/Jenssegers/Mongodb/Relations/HasMany.php +++ b/src/Jenssegers/Mongodb/Relations/HasMany.php @@ -3,8 +3,8 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; class HasMany extends EloquentHasMany { @@ -76,7 +76,9 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['* $key = $this->wrap($this->getQualifiedParentKeyName()); - return $query->where($this->getHasCompareKey(), 'exists', true); + return $query + ->where($this->getHasCompareKey(), 'exists', true) + ->orWhere($this->getHasCompareKey().'._id', 'exists', true); } /** diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 57e7544e6..cb3b58d8e 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -224,7 +224,7 @@ public function testBelongsToMany() public function testBelongsToManyAttachesExistingModels() { - $user = User::create(['name' => 'John Doe', 'client_ids' => ['1234523']]); + $user = User::create(['name' => 'John Doe', 'client_ids' => [['_id'=>'1234523']]]); $clients = [ Client::create(['name' => 'Pork Pies Ltd.'])->_id, @@ -239,7 +239,8 @@ public function testBelongsToManyAttachesExistingModels() // Sync multiple records $user->clients()->sync($clients); - $user = User::with('clients')->find($user->_id); +// $user = User::with('clients')->find($user->_id); + $user = User::find($user->_id); // Assert non attached ID's are detached succesfully $this->assertNotContains('1234523', $user->client_ids); @@ -543,20 +544,20 @@ public function testDoubleSaveManyToMany() $user->save(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([$user->_id], $client->user_ids); - $this->assertEquals([$client->_id], $user->client_ids); + $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $client->_id]], $user->client_ids); $user = User::where('name', 'John Doe')->first(); $client = Client::where('name', 'Admins')->first(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([$user->_id], $client->user_ids); - $this->assertEquals([$client->_id], $user->client_ids); + $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $client->_id]], $user->client_ids); $user->clients()->save($client); $user->clients()->save($client); $user->save(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([$user->_id], $client->user_ids); - $this->assertEquals([$client->_id], $user->client_ids); + $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $client->_id]], $user->client_ids); } } From 3bffda6f9b16a3ccd6c766266b8f9896289a38ef Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Wed, 31 Jul 2019 22:55:24 +0100 Subject: [PATCH 05/20] Working on Eiger Loading Relations --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 2 +- tests/RelationsTest.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 0975d03f0..6ac40309c 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -67,7 +67,7 @@ public function getOwnerKey() /** * Get the name of the "where in" method for eager loading. * - * @param \Illuminate\Database\Eloquent\Model $model + * @param EloquentModel $model * @param string $key * @return string */ diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index cb3b58d8e..958bf4107 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -130,6 +130,7 @@ public function testEasyRelation() $item = Item::create(['type' => 'knife']); $user->items()->save($item); + /** @var User $user */ $user = User::find($user->_id); $items = $user->items; $this->assertCount(1, $items); @@ -224,7 +225,8 @@ public function testBelongsToMany() public function testBelongsToManyAttachesExistingModels() { - $user = User::create(['name' => 'John Doe', 'client_ids' => [['_id'=>'1234523']]]); + // Should be able to remove legacy + $user = User::create(['name' => 'John Doe', 'client_ids' => ['1234523', ['_id' => '1234523']]]); $clients = [ Client::create(['name' => 'Pork Pies Ltd.'])->_id, @@ -252,10 +254,12 @@ public function testBelongsToManyAttachesExistingModels() $user->clients()->sync($moreClients); // Refetch - $user = User::with('clients')->find($user->_id); +// $user = User::with('clients')->find($user->_id); + $user = User::find($user->_id); + $user->load('clients'); // Assert there are now still 2 client objects in the relationship - $this->assertCount(2, $user->clients); + $this->assertCount(2, $user->clients()->get()); // Assert that the new relationships name start with synced $this->assertStringStartsWith('synced', $user->clients[0]->name); From 85a32c9229839fe451bf26988e0de30563d8aff1 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 15:51:47 +0100 Subject: [PATCH 06/20] Eager Loading --- .../Mongodb/Relations/BelongsToMany.php | 19 +++++++++++++++++-- tests/RelationsTest.php | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 6288a78e5..4ea7d163c 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -72,6 +72,17 @@ public function addConstraints() } } + /** + * @inheritDoc + */ + public function addEagerConstraints(array $models) + { + $keys = $this->getKeys($models, $this->parentKey); + $this->query + ->whereIn($this->getQualifiedForeignPivotKeyName(), $keys) + ->orWhereRaw([$this->getQualifiedForeignPivotKeyName().'._id' => ['$in' => $keys]]); + } + /** * Set the where clause for the relation query. * @@ -285,7 +296,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; + } } } @@ -377,4 +392,4 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } -} +} \ No newline at end of file diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 958bf4107..c5c73e108 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -255,6 +255,7 @@ public function testBelongsToManyAttachesExistingModels() // Refetch // $user = User::with('clients')->find($user->_id); + /** @var User $user */ $user = User::find($user->_id); $user->load('clients'); From cdd115156a5145853e7c98b80ec5c700801dac47 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 16:17:48 +0100 Subject: [PATCH 07/20] Dictionary Mapping --- .../Mongodb/Eloquent/HybridRelations.php | 2 +- src/Jenssegers/Mongodb/Relations/HasMany.php | 10 ++++++++++ src/Jenssegers/Mongodb/Relations/HasOne.php | 12 ++++++++++- .../Mongodb/Relations/MorphMany.php | 20 +++++++++++++++++++ src/Jenssegers/Mongodb/Relations/MorphTo.php | 15 +++++++++++++- tests/RelationsTest.php | 17 +++++++++++++--- 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 src/Jenssegers/Mongodb/Relations/MorphMany.php diff --git a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php index 34b8b5788..be3e44bc8 100644 --- a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php @@ -2,7 +2,6 @@ namespace Jenssegers\Mongodb\Eloquent; -use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Support\Str; use Jenssegers\Mongodb\Helpers\EloquentBuilder; @@ -11,6 +10,7 @@ use Jenssegers\Mongodb\Relations\HasMany; use Jenssegers\Mongodb\Relations\HasOne; use Jenssegers\Mongodb\Relations\MorphTo; +use Jenssegers\Mongodb\Relations\MorphMany; trait HybridRelations { diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index 03f3cd2d3..80fdf88b2 100644 --- a/src/Jenssegers/Mongodb/Relations/HasMany.php +++ b/src/Jenssegers/Mongodb/Relations/HasMany.php @@ -3,6 +3,7 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\HasMany as EloquentHasMany; @@ -92,4 +93,13 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + protected function buildDictionary(Collection $results) + { + $foreign = $this->getForeignKeyName(); + + return $results->mapToDictionary(function ($result) use ($foreign) { + return [(string) $result->{$foreign} => $result]; + })->all(); + } } diff --git a/src/Jenssegers/Mongodb/Relations/HasOne.php b/src/Jenssegers/Mongodb/Relations/HasOne.php index a8d65dba9..e6b454421 100644 --- a/src/Jenssegers/Mongodb/Relations/HasOne.php +++ b/src/Jenssegers/Mongodb/Relations/HasOne.php @@ -3,8 +3,9 @@ namespace Jenssegers\Mongodb\Relations; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\HasOne as EloquentHasOne; class HasOne extends EloquentHasOne { @@ -90,4 +91,13 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + protected function buildDictionary(Collection $results) + { + $foreign = $this->getForeignKeyName(); + + return $results->mapToDictionary(function ($result) use ($foreign) { + return [(string) $result->{$foreign} => $result]; + })->all(); + } } diff --git a/src/Jenssegers/Mongodb/Relations/MorphMany.php b/src/Jenssegers/Mongodb/Relations/MorphMany.php new file mode 100644 index 000000000..cd657e750 --- /dev/null +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -0,0 +1,20 @@ +getForeignKeyName(); + + return $results->mapToDictionary(function ($result) use ($foreign) { + return [(string) $result->{$foreign} => $result]; + })->all(); + } + +} \ No newline at end of file diff --git a/src/Jenssegers/Mongodb/Relations/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index 32f3d3ab6..6fd70005e 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -2,8 +2,9 @@ namespace Jenssegers\Mongodb\Relations; -use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; +use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; class MorphTo extends EloquentMorphTo { @@ -20,6 +21,18 @@ public function addConstraints() } } + /** + * @inheritDoc + */ + protected function buildDictionary(Collection $models) + { + foreach ($models as $model) { + if ($model->{$this->morphType}) { + $this->dictionary[$model->{$this->morphType}][(string) $model->{$this->foreignKey}][] = $model; + } + } + } + /** * @inheritdoc */ diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index c5c73e108..607d59b0d 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -204,8 +204,18 @@ public function testBelongsToMany() $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); // Assert they are attached - $this->assertContains($client->_id, $user->client_ids); - $this->assertContains($user->_id, $client->user_ids); + $idPluck = function ($item) { + if (is_object($item)) { + return $item->_id; + } + if (is_array($item)) { + return $item['_id']; + } + return null; + }; + + $this->assertContains($client->_id, array_map($idPluck, $user->client_ids)); + $this->assertContains($user->_id, array_map($idPluck, $client->user_ids)); $this->assertCount(2, $user->clients); $this->assertCount(2, $client->users); @@ -369,6 +379,7 @@ public function testBelongsToManyCustom() $this->assertArrayHasKey('groups', $user->getAttributes()); // Assert they are attached + //TODO: Fix Recursion $this->assertContains($group->_id, $user->groups->pluck('_id')->toArray()); $this->assertContains($user->_id, $group->users->pluck('_id')->toArray()); $this->assertEquals($group->_id, $user->groups()->first()->_id); @@ -404,7 +415,7 @@ public function testMorph() $this->assertEquals($photo->imageable->name, $user->name); $user = User::with('photos')->find($user->_id); - $relations = $user->getRlations(); + $relations = $user->getRelations(); $this->assertArrayHasKey('photos', $relations); $this->assertEquals(1, $relations['photos']->count()); From 72f81c923894bee6fe380c0f894709c8b89d7f60 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 16:54:28 +0100 Subject: [PATCH 08/20] Debugging Aid --- tests/RelationsTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 607d59b0d..f0197c4b3 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -380,7 +380,8 @@ public function testBelongsToManyCustom() // Assert they are attached //TODO: Fix Recursion - $this->assertContains($group->_id, $user->groups->pluck('_id')->toArray()); + $userGroups = $user->groups; + $this->assertContains($group->_id,$userGroups->pluck('_id')->toArray()); $this->assertContains($user->_id, $group->users->pluck('_id')->toArray()); $this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id); From 2efaceffa3bec3f0627dcd5bc6635e408b747cae Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 17:00:30 +0100 Subject: [PATCH 09/20] Applied changes from StyleCI --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 4 ++-- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 2 +- src/Jenssegers/Mongodb/Relations/MorphMany.php | 1 - tests/RelationsTest.php | 3 +-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 6ac40309c..cd8748db4 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -98,8 +98,8 @@ public function match(array $models, Collection $results, $relation) // and match back onto their children using these keys of the dictionary and // the primary key of the children to map them onto the correct instances. foreach ($models as $model) { - if (isset($dictionary[(string)$model->{$foreign}])) { - $model->setRelation($relation, $dictionary[(string)$model->{$foreign}]); + if (isset($dictionary[(string) $model->{$foreign}])) { + $model->setRelation($relation, $dictionary[(string) $model->{$foreign}]); } } diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 4ea7d163c..8e2e883c2 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -223,7 +223,7 @@ public function attach($id, array $attributes = [], $touch = true) $query ->whereIn($this->related->getKeyName(), (array)$id) - ->orWhereIn($this->related->getKeyName().'._id', (array)$id); + ->orWhereIn($this->related->getKeyName().'._id', (array) $id); // Attach the new parent id to the related model. $query->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); diff --git a/src/Jenssegers/Mongodb/Relations/MorphMany.php b/src/Jenssegers/Mongodb/Relations/MorphMany.php index cd657e750..3af24018c 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphMany.php +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -16,5 +16,4 @@ protected function buildDictionary(Collection $results) return [(string) $result->{$foreign} => $result]; })->all(); } - } \ No newline at end of file diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index f0197c4b3..a452578e9 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -211,7 +211,6 @@ public function testBelongsToMany() if (is_array($item)) { return $item['_id']; } - return null; }; $this->assertContains($client->_id, array_map($idPluck, $user->client_ids)); @@ -381,7 +380,7 @@ public function testBelongsToManyCustom() // Assert they are attached //TODO: Fix Recursion $userGroups = $user->groups; - $this->assertContains($group->_id,$userGroups->pluck('_id')->toArray()); + $this->assertContains($group->_id, $userGroups->pluck('_id')->toArray()); $this->assertContains($user->_id, $group->users->pluck('_id')->toArray()); $this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id); From 014e860ab3f4df91efc5b8dd6968d33bf29b80f8 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 17:03:48 +0100 Subject: [PATCH 10/20] Applied changes from StyleCI --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 10 +++++----- src/Jenssegers/Mongodb/Relations/MorphMany.php | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 8e2e883c2..bfd4d58c9 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -222,7 +222,7 @@ public function attach($id, array $attributes = [], $touch = true) $query = $this->newRelatedQuery(); $query - ->whereIn($this->related->getKeyName(), (array)$id) + ->whereIn($this->related->getKeyName(), (array) $id) ->orWhereIn($this->related->getKeyName().'._id', (array) $id); // Attach the new parent id to the related model. @@ -231,7 +231,7 @@ public function attach($id, array $attributes = [], $touch = true) //Pivot Collection $pivot_x = []; - foreach ((array)$id as $item) { + foreach ((array) $id as $item) { $pivot_x[] = array_merge($attributes, ['_id' => $item]); } @@ -296,9 +296,9 @@ protected function buildDictionary(Collection $results) foreach ($results as $result) { foreach ($result->$foreign as $item) { - if (is_array($item)){ + if (is_array($item)) { $dictionary[$item['_id']][]=$result; - }else { + } else { $dictionary[$item][] = $result; } } @@ -392,4 +392,4 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } -} \ No newline at end of file +} diff --git a/src/Jenssegers/Mongodb/Relations/MorphMany.php b/src/Jenssegers/Mongodb/Relations/MorphMany.php index 3af24018c..d0c88c126 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphMany.php +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -1,6 +1,5 @@ {$foreign} => $result]; })->all(); } -} \ No newline at end of file +} From e58e09c0daa9c30b34e1eaace093eee31584d504 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Thu, 1 Aug 2019 17:04:20 +0100 Subject: [PATCH 11/20] Applied changes from StyleCI --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index bfd4d58c9..1e77c0cdb 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -152,7 +152,6 @@ public function sync($ids, $detaching = true) 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']; From 613dce4f9774b3977f5f5d7e50f00cf9a350f0f3 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 13:15:01 +0100 Subject: [PATCH 12/20] Fixed issue where relationship named same as attribrute caused loop --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 1e77c0cdb..1a20e167b 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -36,7 +36,7 @@ protected function hydratePivotRelation(array $models) { foreach ($models as $model) { $keyToUse = $this->getTable() == $model->getTable() ? $this->getForeignKey() : $this->getRelatedKey(); - $pcontent = $model->{$keyToUse}; + $pcontent = $model->getAttributes()[$keyToUse]; $model->setRelation($this->accessor, $this->newExistingPivot( $pcontent[0] )); From 532975b5c531b3f757a5e0922db5912afe281403 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 13:30:18 +0100 Subject: [PATCH 13/20] Added Unzip and Coverage --- Dockerfile | 2 +- phpunit.xml.dist | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 360f67e66..aaefdbe0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM composer:${COMPOSER_VERSION} FROM php:${PHP_VERSION}-cli RUN apt-get update && \ - apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev && \ + apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev unzip && \ pecl install mongodb && docker-php-ext-enable mongodb && \ pecl install xdebug && docker-php-ext-enable xdebug && \ docker-php-ext-install -j$(nproc) pdo_mysql zip diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2669537cb..11ea25011 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -42,4 +42,9 @@ tests/ValidationTest.php + + + src + + From 91bfb9c836e048c691dff7c549e5bc3019f5878a Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 14:57:39 +0100 Subject: [PATCH 14/20] Testing Legacy support --- .../Mongodb/Relations/BelongsToMany.php | 2 +- tests/RelationsTest.php | 35 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 1a20e167b..d2dcbf79b 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -38,7 +38,7 @@ protected function hydratePivotRelation(array $models) $keyToUse = $this->getTable() == $model->getTable() ? $this->getForeignKey() : $this->getRelatedKey(); $pcontent = $model->getAttributes()[$keyToUse]; $model->setRelation($this->accessor, $this->newExistingPivot( - $pcontent[0] + is_string($pcontent[0]) ? ['_id' => $pcontent] : $pcontent[0] )); } } diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index a452578e9..4075bc676 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -330,7 +330,6 @@ public function testBelongsToManyAttachArray() $client1 = Client::create(['name' => 'Test 1'])->_id; $client2 = Client::create(['name' => 'Test 2'])->_id; - $user = User::where('name', '=', 'John Doe')->first(); $user->clients()->attach([$client1, $client2]); $this->assertCount(2, $user->clients); } @@ -342,7 +341,6 @@ public function testBelongsToManyAttachEloquentCollection() $client2 = Client::create(['name' => 'Test 2']); $collection = new \Illuminate\Database\Eloquent\Collection([$client1, $client2]); - $user = User::where('name', '=', 'John Doe')->first(); $user->clients()->attach($collection); $this->assertCount(2, $user->clients); } @@ -364,6 +362,38 @@ public function testBelongsToManySyncAlreadyPresent() $this->assertCount(1, $user['client_ids']); } + public function testBelongsToManyLegacy() + { + // Construct Legacy Relationship model + $client1 = Client::create(['name' => 'Test 1']); + $client2 = Client::create(['name' => 'Test 2']); + $user1 = User::create(['name' => 'John Doe', 'client_ids' => [$client1->_id, $client2]]); + $user2 = User::create(['name' => 'John Doe', 'client_ids' => [['_id' => $client1->_id], ['_id' => $client2]]]); + $client1->fill(['user_ids' => [$user1->_id,$user2->_id]])->save(); + $client2->fill(['user_ids' => [$user1->_id,$user2->_id]])->save(); + + // Check for retrieval + $this->assertCount(2, $user1->clients()->get(), "Get via Helper"); + $this->assertCount(2, $user1->clients, "Get via Attr"); + $this->assertCount(2, $user2->clients()->get(), "Get via Helper"); + $this->assertCount(2, $user2->clients, "Get via Attr"); + + // Check retrieval is correct + $testMatrix = [ + [$client1, $user1->clients[0]], + [$client2, $user1->clients[1]], + [$client1, $user2->clients[0]], + [$client2, $user2->clients[1]] + ]; + foreach ($testMatrix as $k => $test) { + $this->assertEquals($test[0]->_id, $test[1]->_id, "Matrix #{$k}"); + } + + // Check inverse + $this->assertCount(2, $client1->users()->get(), "Get Inverse via Helper"); + $this->assertCount(2, $client1->users, "Get Inverse via Attr"); + } + public function testBelongsToManyCustom() { $user = User::create(['name' => 'John Doe']); @@ -378,7 +408,6 @@ public function testBelongsToManyCustom() $this->assertArrayHasKey('groups', $user->getAttributes()); // Assert they are attached - //TODO: Fix Recursion $userGroups = $user->groups; $this->assertContains($group->_id, $userGroups->pluck('_id')->toArray()); $this->assertContains($user->_id, $group->users->pluck('_id')->toArray()); From 65b1bec0b94c8c3961a2bc0a529597433b61c08d Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 15:36:43 +0100 Subject: [PATCH 15/20] Codeacy Appeasement: Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index aaefdbe0e..ef9daf5f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ ARG PHP_VERSION=7.2 ARG COMPOSER_VERSION=1.8 -FROM composer:${COMPOSER_VERSION} +FROM composer:${COMPOSER_VERSION} as composer FROM php:${PHP_VERSION}-cli RUN apt-get update && \ - apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev unzip && \ + apt-get install -y autoconf pkg-config libssl-dev git libzip-dev zlib1g-dev unzip --no-install-recommends && \ pecl install mongodb && docker-php-ext-enable mongodb && \ pecl install xdebug && docker-php-ext-enable xdebug && \ docker-php-ext-install -j$(nproc) pdo_mysql zip From 6896e2687e24903a506e1b0650a2f1ade5417491 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 15:58:45 +0100 Subject: [PATCH 16/20] Codeacy Appeasement: README.md --- README.md | 157 ++++++++++++++++++++++++------------------------------ 1 file changed, 70 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index e444dd9d2..82921a2d6 100644 --- a/README.md +++ b/README.md @@ -1,50 +1,46 @@ -Laravel MongoDB -=============== +# Laravel MongoDB [![Latest Stable Version](http://img.shields.io/github/release/jenssegers/laravel-mongodb.svg)](https://packagist.org/packages/jenssegers/mongodb) [![Total Downloads](http://img.shields.io/packagist/dm/jenssegers/mongodb.svg)](https://packagist.org/packages/jenssegers/mongodb) [![Build Status](http://img.shields.io/travis/jenssegers/laravel-mongodb.svg)](https://travis-ci.org/jenssegers/laravel-mongodb) [![Coverage Status](http://img.shields.io/coveralls/jenssegers/laravel-mongodb.svg)](https://coveralls.io/r/jenssegers/laravel-mongodb?branch=master) [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg)](https://www.paypal.me/jenssegers) -An Eloquent model and Query builder with support for MongoDB, using the original Laravel API. *This library extends the original Laravel classes, so it uses exactly the same methods.* +An Eloquent model and Query builder with support for MongoDB, using the original Laravel API. _This library extends the original Laravel classes, so it uses exactly the same methods._ -Table of contents ------------------ -* [Installation](#installation) -* [Upgrading](#upgrading) -* [Configuration](#configuration) -* [Eloquent](#eloquent) -* [Optional: Alias](#optional-alias) -* [Query Builder](#query-builder) -* [Schema](#schema) -* [Extensions](#extensions) -* [Troubleshooting](#troubleshooting) -* [Examples](#examples) +## Table of contents -Installation ------------- +- [Installation](#installation) +- [Upgrading](#upgrading) +- [Configuration](#configuration) +- [Eloquent](#eloquent) +- [Optional: Alias](#optional-alias) +- [Query Builder](#query-builder) +- [Schema](#schema) +- [Extensions](#extensions) +- [Troubleshooting](#troubleshooting) +- [Examples](#examples) -Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php +## Installation + +Make sure you have the MongoDB PHP driver installed. You can find installation instructions at **WARNING**: The old mongo PHP driver is not supported anymore in versions >= 3.0. Installation using composer: -``` -composer require jenssegers/mongodb -``` + composer require jenssegers/mongodb ### Laravel version Compatibility - Laravel | Package -:---------|:---------- - 4.2.x | 2.0.x - 5.0.x | 2.1.x - 5.1.x | 2.2.x or 3.0.x - 5.2.x | 2.3.x or 3.0.x - 5.3.x | 3.1.x or 3.2.x - 5.4.x | 3.2.x - 5.5.x | 3.3.x - 5.6.x | 3.4.x - 5.7.x | 3.4.x - 5.8.x | 3.5.x +| Laravel | Package | +| :------ | :------------- | +| 4.2.x | 2.0.x | +| 5.0.x | 2.1.x | +| 5.1.x | 2.2.x or 3.0.x | +| 5.2.x | 2.3.x or 3.0.x | +| 5.3.x | 3.1.x or 3.2.x | +| 5.4.x | 3.2.x | +| 5.5.x | 3.3.x | +| 5.6.x | 3.4.x | +| 5.7.x | 3.4.x | +| 5.8.x | 3.5.x | And add the service provider in `config/app.php`: @@ -73,8 +69,7 @@ $capsule->getDatabaseManager()->extend('mongodb', function($config, $name) }); ``` -Upgrading ---------- +## Upgrading #### Upgrading from version 2 to 3 @@ -108,17 +103,13 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe $books = $user->books()->sortBy('title'); ``` -Testing -------- +## Testing To run the test for this package, run: -``` -docker-compose up -``` + docker-compose up -Configuration -------------- +## Configuration Change your default database connection name in `config/database.php`: @@ -168,10 +159,9 @@ Alternatively, you can use MongoDB connection string: ], ``` -Please refer to MongoDB official docs for its URI format: https://docs.mongodb.com/manual/reference/connection-string/ +Please refer to MongoDB official docs for its URI format: -Eloquent --------- +## Eloquent This package includes a MongoDB enabled Eloquent class that you can use to define models for corresponding collections. @@ -205,7 +195,7 @@ class MyModel extends Eloquent { } ``` -Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent +Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on ### Optional: Alias @@ -221,8 +211,7 @@ This will allow you to use the registered alias like: class MyModel extends Moloquent {} ``` -Query Builder -------------- +## Query Builder The database driver plugs right into the original query builder. When using mongodb connections, you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operators/operations. @@ -238,10 +227,9 @@ If you did not change your default database connection, you will need to specify $user = DB::connection('mongodb')->collection('users')->get(); ``` -Read more about the query builder on http://laravel.com/docs/queries +Read more about the query builder on -Schema ------- +## Schema The database driver also has (limited) schema builder support. You can easily manipulate collections and set indexes: @@ -256,14 +244,14 @@ Schema::create('users', function($collection) Supported operations are: - - create and drop - - collection - - hasCollection - - index and dropIndex (compound indexes supported as well) - - unique - - background, sparse, expire, geospatial (MongoDB specific) +- create and drop +- collection +- hasCollection +- index and dropIndex (compound indexes supported as well) +- unique +- background, sparse, expire, geospatial (MongoDB specific) -All other (unsupported) operations are implemented as dummy pass-through methods, because MongoDB does not use a predefined schema. Read more about the schema builder on http://laravel.com/docs/schema +All other (unsupported) operations are implemented as dummy pass-through methods, because MongoDB does not use a predefined schema. Read more about the schema builder on ### Geospatial indexes @@ -287,8 +275,7 @@ Schema::create('users', function($collection) }); ``` -Extensions ----------- +## Extensions ### Auth @@ -331,14 +318,13 @@ Jenssegers\Mongodb\MongodbQueueServiceProvider::class, ### Sentry -If you want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out https://github.com/jenssegers/Laravel-MongoDB-Sentry +If you want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out ### Sessions -The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session +The MongoDB session driver is available in a separate package, check out -Examples --------- +## Examples ### Basic Usage @@ -441,7 +427,7 @@ $users = Users::groupBy('title')->get(['title', 'name']); **Aggregation** -*Aggregations are only available for MongoDB versions greater than 2.2.* +_Aggregations are only available for MongoDB versions greater than 2.2._ ```php $total = Order::count(); @@ -510,7 +496,7 @@ class User extends Eloquent { } ``` -For more information check http://laravel.com/docs/eloquent#soft-deleting +For more information check ### MongoDB specific operators @@ -546,7 +532,7 @@ Selects documents where values match a specified regular expression. User::where('name', 'regex', new \MongoDB\BSON\Regex("/.*doe/i"))->get(); ``` -**NOTE:** you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a MongoDB\BSON\Regex object. +**NOTE:** you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a MongoDB\\BSON\\Regex object. ```php User::where('name', 'regexp', '/.*doe/i'))->get(); @@ -560,7 +546,7 @@ User::where('name', 'not regexp', '/.*doe/i'))->get(); **Type** -Selects documents if a field is of the specified type. For more information check: http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type +Selects documents if a field is of the specified type. For more information check: ```php User::where('age', 'type', 2)->get(); @@ -639,10 +625,9 @@ $locations = Location::where('location', 'geoIntersects', [ ]); ``` - **Where** -Matches documents that satisfy a JavaScript expression. For more information check http://docs.mongodb.org/manual/reference/operator/query/where/#op._S_where +Matches documents that satisfy a JavaScript expression. For more information check ### Inserts, updates and deletes @@ -672,7 +657,7 @@ $user->email = 'john@foo.com'; $user->save(); ``` -*There is also support for upsert operations, check https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operations* +_There is also support for upsert operations, check _ **Deleting a model** @@ -689,11 +674,11 @@ Or deleting a model by its key: User::destroy('517c43667db388101e00000f'); ``` -For more information about model manipulation, check http://laravel.com/docs/eloquent#insert-update-delete +For more information about model manipulation, check ### Dates -Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields, you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators +Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields, you will need to manually specify them as described here: Example: @@ -717,12 +702,12 @@ $users = User::where('birthday', '>', new DateTime('-18 years'))->get(); Supported relations are: - - hasOne - - hasMany - - belongsTo - - belongsToMany - - embedsOne - - embedsMany +- hasOne +- hasMany +- belongsTo +- belongsToMany +- embedsOne +- embedsMany Example: @@ -754,7 +739,7 @@ class Item extends Eloquent { } ``` -The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to `null`: +The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to `null`: ```php use Jenssegers\Mongodb\Eloquent\Model as Eloquent; @@ -769,8 +754,7 @@ class User extends Eloquent { } ``` - -Other relations are not yet supported, but may be added in the future. Read more about these relations on http://laravel.com/docs/eloquent#relationships +Other relations are not yet supported, but may be added in the future. Read more about these relations on ### EmbedsMany Relations @@ -797,7 +781,7 @@ You can access the embedded models through the dynamic property: $books = User::first()->books; ``` -The inverse relation is auto*magically* available, you don't need to define this reverse relation. +The inverse relation is auto_magically_ available, you don't need to define this reverse relation. ```php $user = $book->user; @@ -849,7 +833,7 @@ Like other relations, embedsMany assumes the local key of the relationship based return $this->embedsMany('Book', 'local_key'); ``` -Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections +Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: ### EmbedsOne Relations @@ -1015,7 +999,6 @@ $projections = ['id', 'name']; DB::collection('items')->paginate($limit, $projections); ``` - **Push** Add items to an array. @@ -1063,7 +1046,7 @@ You may easily cache the results of a query using the remember method: $users = User::remember(10)->get(); ``` -*From: http://laravel.com/docs/queries#caching-queries* +_From: _ ### Query Logging @@ -1073,4 +1056,4 @@ By default, Laravel keeps a log in memory of all queries that have been run for DB::connection()->disableQueryLog(); ``` -*From: http://laravel.com/docs/database#query-logging* +_From: _ \ No newline at end of file From fad2ef504491a7af1a9426881134bfbaefe2ef25 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 16:11:02 +0100 Subject: [PATCH 17/20] Codeacy Appeasement: README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 82921a2d6..686adca0f 100644 --- a/README.md +++ b/README.md @@ -657,7 +657,7 @@ $user->email = 'john@foo.com'; $user->save(); ``` -_There is also support for upsert operations, check _ +There is also support for upsert operations, check **Deleting a model** From db8fa9dad4c77df9cacf71066a4c2830e07c767e Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 20:50:52 +0100 Subject: [PATCH 18/20] Cleaning Up Updates --- .../Mongodb/Relations/BelongsToMany.php | 33 ++++++++++++++++--- tests/RelationsTest.php | 29 ++++++++++++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index d2dcbf79b..bc73cb3ff 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -198,7 +198,32 @@ 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(); + } + + $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); + } /** @@ -265,14 +290,12 @@ public function detach($ids = [], $touch = true) // Prepare the query to select all related objects. if (count($ids) > 0) { - $query - ->whereIn($this->related->getKeyName(), $ids) - ->orWhereIn($this->related->getKeyName().'._id', $ids); + $query->whereIn($this->related->getKeyName(), $ids); } // Remove the relation to the parent. $query->pull($this->foreignPivotKey, $this->parent->getKey()); - $query->pull($this->foreignPivotKey, ['_id'=>$this->parent->getKey()]); + $query->pull($this->foreignPivotKey, [$this->parentKey=>$this->parent->getKey()]); if ($touch) { $this->touchIfTouching(); diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 4075bc676..72b466c0e 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -263,10 +263,7 @@ public function testBelongsToManyAttachesExistingModels() $user->clients()->sync($moreClients); // Refetch -// $user = User::with('clients')->find($user->_id); - /** @var User $user */ - $user = User::find($user->_id); - $user->load('clients'); + $user = User::with('clients')->find($user->_id); // Assert there are now still 2 client objects in the relationship $this->assertCount(2, $user->clients()->get()); @@ -304,7 +301,10 @@ public function testBelongsToManySyncAttrs() $client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id; // Sync multiple - $user->clients()->sync([$client1 => ['fresh_client' => true], $client2 => ['fresh_client' => false]]); + $user->clients()->sync([ + $client1 => ['fresh_client' => true], + $client2 => ['fresh_client' => false] + ]); //Check Sync Success $this->assertEquals($user->client_ids[0]['_id'], $client1); @@ -317,11 +317,26 @@ public function testBelongsToManySyncAttrs() $clients = $user->clients; $this->assertCount(2, $clients); - foreach ($user->clients()->withPivot('fresh_client')->get() as $item) { + foreach ($user->clients()->get() as $item) { $this->assertIsArray($item->pivot->toArray()); $this->assertEquals($item->_id == $client1, $item->pivot->fresh_client); } - $this->assertTrue(true); + } + + public function testBelongsToManyUpdatePivot() + { + /** @var User $user */ + $user = User::create(['name' => 'John Doe']); + $client1 = Client::create(['name' => 'Pork Pies Ltd.'])->_id; + $client2 = Client::create(['name' => 'Buffet Bar Inc.'])->_id; + + // Sync multiple + $user->clients()->sync([ + $client1 => ['fresh_client' => true], + $client2 => ['fresh_client' => false] + ]); + $user->clients()->updateExistingPivot($client1,['fresh_client'=>false]); + $this->assertFalse($user->clients()->where('_id',$client1)->first()->pivot->fresh_client); } public function testBelongsToManyAttachArray() From 077e911ea2605b18f506e631c3764fde1642a924 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 21:01:59 +0100 Subject: [PATCH 19/20] StyleCI Fixes --- .../Mongodb/Relations/BelongsToMany.php | 323 +++++++++--------- tests/RelationsTest.php | 18 +- 2 files changed, 170 insertions(+), 171 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index bc73cb3ff..5c594bd72 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -22,44 +22,21 @@ public function getHasCompareKey() } /** - * @inheritdoc - */ - public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) - { - return $query; - } - - /** - * @inheritdoc - */ - protected function hydratePivotRelation(array $models) - { - 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] - )); - } - } - - /** - * Set the select clause for the relation query. + * Get the fully qualified foreign key for the relation. * - * @param array $columns - * @return array + * @return string */ - protected function getSelectColumns(array $columns = ['*']) + public function getForeignKey() { - return $columns; + return $this->foreignPivotKey; } /** * @inheritdoc */ - protected function shouldSelect(array $columns = ['*']) + public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) { - return $columns; + return $query; } /** @@ -72,17 +49,6 @@ public function addConstraints() } } - /** - * @inheritDoc - */ - public function addEagerConstraints(array $models) - { - $keys = $this->getKeys($models, $this->parentKey); - $this->query - ->whereIn($this->getQualifiedForeignPivotKeyName(), $keys) - ->orWhereRaw([$this->getQualifiedForeignPivotKeyName().'._id' => ['$in' => $keys]]); - } - /** * Set the where clause for the relation query. * @@ -99,6 +65,25 @@ protected function setWhere() return $this; } + /** + * @inheritDoc + */ + public function addEagerConstraints(array $models) + { + $keys = $this->getKeys($models, $this->parentKey); + $this->query + ->whereIn($this->getQualifiedForeignPivotKeyName(), $keys) + ->orWhereRaw([$this->getQualifiedForeignPivotKeyName().'._id' => ['$in' => $keys]]); + } + + /** + * @inheritdoc + */ + public function getQualifiedForeignPivotKeyName() + { + return $this->foreignPivotKey; + } + /** * @inheritdoc */ @@ -111,6 +96,67 @@ public function save(Model $model, array $joining = [], $touch = true) return $model; } + /** + * @inheritdoc + */ + public function attach($id, array $attributes = [], $touch = true) + { + if ($id instanceof Model) { + $model = $id; + + $id = $model->getKey(); + + // Attach the new parent id to the related model. + $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) + ->orWhereIn($this->related->getKeyName().'._id', (array) $id); + + // Attach the new parent id to the related model. + $query->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); + } + + //Pivot Collection + $pivot_x = []; + foreach ((array) $id as $item) { + $pivot_x[] = array_merge($attributes, ['_id' => $item]); + } + + // Attach the new ids to the parent model. + $this->parent->push($this->getRelatedKey(), $pivot_x, true); + + if ($touch) { + $this->touchIfTouching(); + } + } + + /** + * Create a new query builder for the related model. + * + * @return \Illuminate\Database\Query\Builder + */ + public function newRelatedQuery() + { + return $this->related->newQuery(); + } + + /** + * Get the related key with backwards compatible support. + * + * @return string + */ + public function getRelatedKey() + { + return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey; + } + /** * @inheritdoc */ @@ -194,77 +240,23 @@ public function sync($ids, $detaching = true) } /** - * @inheritdoc - */ - public function updateExistingPivot($id, array $attributes, $touch = true) - { - if ($id instanceof Model) { - $model = $id; - $id = $model->getKey(); - } else { - if ($id instanceof Collection) { - $id = $id->modelKeys(); - } - - $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); - - } - - /** - * @inheritdoc + * Format the sync list so that it is keyed by ID. (Legacy Support) + * The original function has been renamed to formatRecordsList since Laravel 5.3 + * + * @param array $records + * @return array + * @deprecated */ - public function attach($id, array $attributes = [], $touch = true) + protected function formatSyncList(array $records) { - if ($id instanceof Model) { - $model = $id; - - $id = $model->getKey(); - - // Attach the new parent id to the related model. - $model->push($this->foreignPivotKey, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); - } else { - if ($id instanceof Collection) { - $id = $id->modelKeys(); + $results = []; + foreach ($records as $id => $attributes) { + if (!is_array($attributes)) { + list($id, $attributes) = [$attributes, []]; } - - $query = $this->newRelatedQuery(); - - $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, [array_merge($attributes, ['_id' => $this->parent->getKey()])], true); - } - - //Pivot Collection - $pivot_x = []; - foreach ((array) $id as $item) { - $pivot_x[] = array_merge($attributes, ['_id' => $item]); - } - - // Attach the new ids to the parent model. - $this->parent->push($this->getRelatedKey(), $pivot_x, true); - - if ($touch) { - $this->touchIfTouching(); + $results[$id] = $attributes; } + return $results; } /** @@ -286,7 +278,7 @@ public function detach($ids = [], $touch = true) // Detach all ids from the parent model. // Legacy Support $this->parent->pull($this->getRelatedKey(), $ids); - $this->parent->pull($this->getRelatedKey(), ['_id'=>['$in'=>$ids]]); + $this->parent->pull($this->getRelatedKey(), ['_id' => ['$in' => $ids]]); // Prepare the query to select all related objects. if (count($ids) > 0) { @@ -295,7 +287,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()]); + $query->pull($this->foreignPivotKey, [$this->parentKey => $this->parent->getKey()]); if ($touch) { $this->touchIfTouching(); @@ -307,106 +299,113 @@ public function detach($ids = [], $touch = true) /** * @inheritdoc */ - protected function buildDictionary(Collection $results) + public function updateExistingPivot($id, array $attributes, $touch = true) { - $foreign = $this->foreignPivotKey; + if ($id instanceof Model) { + $model = $id; + $id = $model->getKey(); + } else { + if ($id instanceof Collection) { + $id = $id->modelKeys(); + } - // First we will build a dictionary of child models keyed by the foreign key - // of the relation so that we will easily and quickly match them to their - // parents without having a possibly slow inner loops for every models. - $dictionary = []; + $related = $this->newRelatedQuery()->whereIn($this->related->getKeyName(), (array) $id); + $filter = [$this->parentKey => $this->parent->getKey()]; + $pivot_x = [array_merge($attributes, $filter)]; - foreach ($results as $result) { - foreach ($result->$foreign as $item) { - if (is_array($item)) { - $dictionary[$item['_id']][]=$result; - } else { - $dictionary[$item][] = $result; - } - } + //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); - return $dictionary; } /** * @inheritdoc */ - protected function newPivotQuery() + public function getQualifiedRelatedPivotKeyName() { - return $this->newRelatedQuery(); + return $this->relatedPivotKey; } /** - * Create a new query builder for the related model. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ - public function newRelatedQuery() + protected function hydratePivotRelation(array $models) { - return $this->related->newQuery(); + 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] + )); + } } /** - * Get the fully qualified foreign key for the relation. + * Set the select clause for the relation query. * - * @return string + * @param array $columns + * @return array */ - public function getForeignKey() + protected function getSelectColumns(array $columns = ['*']) { - return $this->foreignPivotKey; + return $columns; } /** * @inheritdoc */ - public function getQualifiedForeignPivotKeyName() + protected function shouldSelect(array $columns = ['*']) { - return $this->foreignPivotKey; + return $columns; } /** * @inheritdoc */ - public function getQualifiedRelatedPivotKeyName() + protected function buildDictionary(Collection $results) { - return $this->relatedPivotKey; - } + $foreign = $this->foreignPivotKey; - /** - * Format the sync list so that it is keyed by ID. (Legacy Support) - * The original function has been renamed to formatRecordsList since Laravel 5.3 - * - * @deprecated - * @param array $records - * @return array - */ - protected function formatSyncList(array $records) - { - $results = []; - foreach ($records as $id => $attributes) { - if (!is_array($attributes)) { - list($id, $attributes) = [$attributes, []]; + // First we will build a dictionary of child models keyed by the foreign key + // of the relation so that we will easily and quickly match them to their + // parents without having a possibly slow inner loops for every models. + $dictionary = []; + + foreach ($results as $result) { + foreach ($result->$foreign as $item) { + if (is_array($item)) { + $dictionary[$item['_id']][] = $result; + } else { + $dictionary[$item][] = $result; + } } - $results[$id] = $attributes; } - return $results; + + return $dictionary; } /** - * Get the related key with backwards compatible support. - * - * @return string + * @inheritdoc */ - public function getRelatedKey() + protected function newPivotQuery() { - return property_exists($this, 'relatedPivotKey') ? $this->relatedPivotKey : $this->relatedKey; + return $this->newRelatedQuery(); } /** * Get the name of the "where in" method for eager loading. * - * @param \Illuminate\Database\Eloquent\Model $model + * @param EloquentModel $model * @param string $key * @return string */ diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 72b466c0e..5d9e47d9a 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -335,8 +335,8 @@ public function testBelongsToManyUpdatePivot() $client1 => ['fresh_client' => true], $client2 => ['fresh_client' => false] ]); - $user->clients()->updateExistingPivot($client1,['fresh_client'=>false]); - $this->assertFalse($user->clients()->where('_id',$client1)->first()->pivot->fresh_client); + $user->clients()->updateExistingPivot($client1, ['fresh_client' => false]); + $this->assertFalse($user->clients()->where('_id', $client1)->first()->pivot->fresh_client); } public function testBelongsToManyAttachArray() @@ -384,8 +384,8 @@ public function testBelongsToManyLegacy() $client2 = Client::create(['name' => 'Test 2']); $user1 = User::create(['name' => 'John Doe', 'client_ids' => [$client1->_id, $client2]]); $user2 = User::create(['name' => 'John Doe', 'client_ids' => [['_id' => $client1->_id], ['_id' => $client2]]]); - $client1->fill(['user_ids' => [$user1->_id,$user2->_id]])->save(); - $client2->fill(['user_ids' => [$user1->_id,$user2->_id]])->save(); + $client1->fill(['user_ids' => [$user1->_id, $user2->_id]])->save(); + $client2->fill(['user_ids' => [$user1->_id, $user2->_id]])->save(); // Check for retrieval $this->assertCount(2, $user1->clients()->get(), "Get via Helper"); @@ -550,14 +550,14 @@ public function testNestedKeys() $client = Client::create([ 'data' => [ 'client_id' => 35298, - 'name' => 'John Doe', + 'name' => 'John Doe', ], ]); $address = $client->addresses()->create([ 'data' => [ 'address_id' => 1432, - 'city' => 'Paris', + 'city' => 'Paris', ], ]); @@ -604,20 +604,20 @@ public function testDoubleSaveManyToMany() $user->save(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $user->_id]], $client->user_ids); $this->assertEquals([['_id' => $client->_id]], $user->client_ids); $user = User::where('name', 'John Doe')->first(); $client = Client::where('name', 'Admins')->first(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $user->_id]], $client->user_ids); $this->assertEquals([['_id' => $client->_id]], $user->client_ids); $user->clients()->save($client); $user->clients()->save($client); $user->save(); $this->assertEquals(1, $user->clients()->count()); - $this->assertEquals([['_id' =>$user->_id]], $client->user_ids); + $this->assertEquals([['_id' => $user->_id]], $client->user_ids); $this->assertEquals([['_id' => $client->_id]], $user->client_ids); } } From 87ac10c52c563391087a5575b954a91d40782322 Mon Sep 17 00:00:00 2001 From: Adam Allport Date: Fri, 2 Aug 2019 21:02:32 +0100 Subject: [PATCH 20/20] StyleCI Fixes --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 5c594bd72..3caaeebb7 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -325,7 +325,6 @@ public function updateExistingPivot($id, array $attributes, $touch = true) $this->parent->pull($this->getRelatedKey(), $id); $this->parent->pull($this->getRelatedKey(), $filter); $this->parent->push($this->getRelatedKey(), $pivot_x, true); - } /**