From 206f0f9df1bf1a2c29fcfb22f1b7eb32659ab92f Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 02:09:23 +0300 Subject: [PATCH 1/6] Add Attributes to the Pivot Embed Co-Authored-By: Adam Allport --- .../Mongodb/Relations/BelongsTo.php | 30 ++++- .../Mongodb/Relations/BelongsToMany.php | 87 +++++++++++-- src/Jenssegers/Mongodb/Relations/HasMany.php | 14 ++- src/Jenssegers/Mongodb/Relations/HasOne.php | 13 ++ .../Mongodb/Relations/MorphMany.php | 14 +++ src/Jenssegers/Mongodb/Relations/MorphTo.php | 13 ++ tests/RelationsTest.php | 117 +++++++++++++++--- 7 files changed, 260 insertions(+), 28 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index b47e856fa..13d7719c0 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 @@ -25,7 +26,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}); } } @@ -69,4 +72,29 @@ 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 c57857638..77779a476 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -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(); + } + + $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,7 +222,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(); @@ -193,14 +230,22 @@ 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, $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(); @@ -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(); @@ -253,10 +301,14 @@ protected function buildDictionary(Collection $results) // parents without having a possibly slow inner loops for every models. $dictionary = []; - foreach ($results as $result) { - foreach ($result->$foreign as $item) { - $dictionary[$item][] = $result; - } + foreach ($results as $result) { + foreach ($result->$foreign as $item) { + if (is_array($item)) { + $dictionary[$item['_id']][] = $result; + } else { + $dictionary[$item][] = $result; + } + } } return $dictionary; @@ -342,4 +394,15 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @inheritDoc + */ + public function addEagerConstraints(array $models) + { + $keys = $this->getKeys($models, $this->parentKey); + $this->query + ->whereIn($this->getQualifiedForeignPivotKeyName(), $keys) + ->orWhereRaw([$this->getQualifiedForeignPivotKeyName().'._id' => ['$in' => $keys]]); + } } diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index b10426635..73be93d54 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; @@ -71,7 +72,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); } /** @@ -84,4 +87,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 91741c297..e6bb7e0b4 100644 --- a/src/Jenssegers/Mongodb/Relations/HasOne.php +++ b/src/Jenssegers/Mongodb/Relations/HasOne.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\HasOne as EloquentHasOne; @@ -84,4 +85,16 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @inheritdoc + */ + 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 index 0bda3fa65..312d79d06 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphMany.php +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -2,6 +2,7 @@ namespace Jenssegers\Mongodb\Relations; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentMorphMany; @@ -19,4 +20,17 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @inheritdoc + */ + 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/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index 704c4722c..a8bc678ea 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -2,6 +2,7 @@ namespace Jenssegers\Mongodb\Relations; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model as EloquentModel; use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentMorphTo; @@ -53,4 +54,16 @@ protected function whereInMethod(EloquentModel $model, $key) { return 'whereIn'; } + + /** + * @inheritDoc + */ + protected function buildDictionary(Collection $models) + { + foreach ($models as $model) { + if ($model->{$this->morphType}) { + $this->dictionary[$model->{$this->morphType}][(string) $model->{$this->foreignKey}][] = $model; + } + } + } } diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index a50329682..3a903b480 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -133,6 +133,7 @@ public function testEasyRelation(): void $item = Item::create(['type' => 'knife']); $user->items()->save($item); + /** @var User $user */ $user = User::find($user->_id); $items = $user->items; $this->assertCount(1, $items); @@ -206,8 +207,17 @@ public function testBelongsToMany(): void $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']; + } + }; + + $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); @@ -227,7 +237,8 @@ public function testBelongsToMany(): void public function testBelongsToManyAttachesExistingModels(): void { - $user = User::create(['name' => 'John Doe', 'client_ids' => ['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, @@ -242,7 +253,7 @@ public function testBelongsToManyAttachesExistingModels(): void // Sync multiple records $user->clients()->sync($clients); - $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); @@ -257,7 +268,7 @@ public function testBelongsToManyAttachesExistingModels(): void $user = User::with('clients')->find($user->_id); // 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); @@ -283,13 +294,59 @@ public function testBelongsToManySync(): void $this->assertCount(1, $user->clients); } + public function testBelongsToManySyncAttrs(): void + { + // 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()->get() as $item) { + $this->assertIsArray($item->pivot->toArray()); + $this->assertEquals($item->_id == $client1, $item->pivot->fresh_client); + } + } + + public function testBelongsToManyUpdatePivot(): void + { + /** @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(): void { $user = User::create(['name' => 'John Doe']); $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); } @@ -301,7 +358,6 @@ public function testBelongsToManyAttachEloquentCollection(): void $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); } @@ -323,6 +379,38 @@ public function testBelongsToManySyncAlreadyPresent(): void $this->assertCount(1, $user['client_ids']); } + public function testBelongsToManyLegacy(): void + { + // 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(): void { $user = User::create(['name' => 'John Doe']); @@ -337,7 +425,8 @@ public function testBelongsToManyCustom(): void $this->assertArrayHasKey('groups', $user->getAttributes()); // Assert they are attached - $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); @@ -517,20 +606,20 @@ public function testDoubleSaveManyToMany(): void $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 c531b964c4fdc408a93d850d98ab608946d38f56 Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 02:36:43 +0300 Subject: [PATCH 2/6] Fix styling --- .../Mongodb/Relations/BelongsToMany.php | 16 ++++++++-------- src/Jenssegers/Mongodb/Relations/MorphMany.php | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 77779a476..36f1725ff 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -301,14 +301,14 @@ protected function buildDictionary(Collection $results) // 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; - } - } + foreach ($results as $result) { + foreach ($result->$foreign as $item) { + if (is_array($item)) { + $dictionary[$item['_id']][] = $result; + } else { + $dictionary[$item][] = $result; + } + } } return $dictionary; diff --git a/src/Jenssegers/Mongodb/Relations/MorphMany.php b/src/Jenssegers/Mongodb/Relations/MorphMany.php index 312d79d06..a8837899e 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphMany.php +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -31,6 +31,5 @@ protected function buildDictionary(Collection $results) return $results->mapToDictionary(function ($result) use ($foreign) { return [(string) $result->{$foreign} => $result]; })->all(); - } } From 26cda8bed785a84cb1c03945e560a3f14c166dca Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 04:29:40 +0300 Subject: [PATCH 3/6] Rename @inheritDoc to @inheritdoc --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 2 +- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 2 +- src/Jenssegers/Mongodb/Relations/MorphTo.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 13d7719c0..6fc0d7ccc 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -74,7 +74,7 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritDoc + * @inheritdoc */ public function match(array $models, Collection $results, $relation) { diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 36f1725ff..42cb9ab2d 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -396,7 +396,7 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritDoc + * @inheritdoc */ public function addEagerConstraints(array $models) { diff --git a/src/Jenssegers/Mongodb/Relations/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index a8bc678ea..09d533a44 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -56,7 +56,7 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritDoc + * @inheritdoc */ protected function buildDictionary(Collection $models) { From 0155cca6725f335d4f4771da7bfd79f66f381b95 Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 23:22:44 +0300 Subject: [PATCH 4/6] Add correct docblocks --- src/Jenssegers/Mongodb/Relations/BelongsTo.php | 7 ++++++- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 5 ++++- src/Jenssegers/Mongodb/Relations/HasOne.php | 5 ++++- src/Jenssegers/Mongodb/Relations/MorphMany.php | 5 ++++- src/Jenssegers/Mongodb/Relations/MorphTo.php | 4 +++- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsTo.php b/src/Jenssegers/Mongodb/Relations/BelongsTo.php index 6fc0d7ccc..9bfa69f8d 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsTo.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsTo.php @@ -74,7 +74,12 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritdoc + * Match the eagerly loaded results to their parents. + * + * @param array $models + * @param \Illuminate\Database\Eloquent\Collection $results + * @param string $relation + * @return array */ public function match(array $models, Collection $results, $relation) { diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 42cb9ab2d..8c750b3e9 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -396,7 +396,10 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritdoc + * Set the constraints for an eager load of the relation. + * + * @param array $models + * @return void */ public function addEagerConstraints(array $models) { diff --git a/src/Jenssegers/Mongodb/Relations/HasOne.php b/src/Jenssegers/Mongodb/Relations/HasOne.php index e6bb7e0b4..687b065a5 100644 --- a/src/Jenssegers/Mongodb/Relations/HasOne.php +++ b/src/Jenssegers/Mongodb/Relations/HasOne.php @@ -87,7 +87,10 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritdoc + * Build model dictionary keyed by the relation's foreign key. + * + * @param \Illuminate\Database\Eloquent\Collection $results + * @return array */ protected function buildDictionary(Collection $results) { diff --git a/src/Jenssegers/Mongodb/Relations/MorphMany.php b/src/Jenssegers/Mongodb/Relations/MorphMany.php index a8837899e..21ecb703e 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphMany.php +++ b/src/Jenssegers/Mongodb/Relations/MorphMany.php @@ -22,7 +22,10 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritdoc + * Build model dictionary keyed by the relation's foreign key. + * + * @param \Illuminate\Database\Eloquent\Collection $results + * @return array */ protected function buildDictionary(Collection $results) { diff --git a/src/Jenssegers/Mongodb/Relations/MorphTo.php b/src/Jenssegers/Mongodb/Relations/MorphTo.php index 09d533a44..ec5b4e6d9 100644 --- a/src/Jenssegers/Mongodb/Relations/MorphTo.php +++ b/src/Jenssegers/Mongodb/Relations/MorphTo.php @@ -56,7 +56,9 @@ protected function whereInMethod(EloquentModel $model, $key) } /** - * @inheritdoc + * Build a dictionary with the models. + * @param \Illuminate\Database\Eloquent\Collection $models + * @return void */ protected function buildDictionary(Collection $models) { From fdfe09309751d8e072642c8f8ea8837122eaf779 Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 23:43:47 +0300 Subject: [PATCH 5/6] Add missing docblock for attach --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 8c750b3e9..0623c09d2 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -212,7 +212,12 @@ public function updateExistingPivot($id, array $attributes, $touch = true) } /** - * @inheritdoc + * Attach a model to the parent. + * + * @param mixed $id + * @param array $attributes + * @param bool $touch + * @return void */ public function attach($id, array $attributes = [], $touch = true) { From 0c71f0e85287a17d426575b7a38303fc60b6a51e Mon Sep 17 00:00:00 2001 From: Ditty Date: Sun, 9 Feb 2020 23:48:09 +0300 Subject: [PATCH 6/6] Add missing docblock --- src/Jenssegers/Mongodb/Relations/HasMany.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Jenssegers/Mongodb/Relations/HasMany.php b/src/Jenssegers/Mongodb/Relations/HasMany.php index 73be93d54..52e513c46 100644 --- a/src/Jenssegers/Mongodb/Relations/HasMany.php +++ b/src/Jenssegers/Mongodb/Relations/HasMany.php @@ -88,6 +88,12 @@ protected function whereInMethod(EloquentModel $model, $key) return 'whereIn'; } + /** + * Build model dictionary keyed by the relation's foreign key. + * + * @param \Illuminate\Database\Eloquent\Collection $results + * @return array + */ protected function buildDictionary(Collection $results) { $foreign = $this->getForeignKeyName();