-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Feature] Add MorphToMany support #2670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 76 commits
Commits
Show all changes
81 commits
Select commit
Hold shift + click to select a range
8eaad01
add MorphToMany
ithuis acfdb19
requested code cleanup
ithuis 5a55f80
Merge branch '4.1' into morphToMany
ithuis d929456
WIP
hans-thomas dd8e701
WIP
hans-thomas 17a3aca
Add tests for MorphToMany and MorphedByMany;
hans-thomas 4e7ad77
Improve DX;
hans-thomas 07afe18
Merge branch '4.1' into 2640-morph-to-many
hans-thomas f8240e2
fix cs;
hans-thomas b9ae8ab
Merge branch '4.1' into 2640-morph-to-many
hans-thomas fd0a9f9
Querying logic in inverse mode updated;
hans-thomas dbe93d0
Attaching a collection or string id(s) fixed;
hans-thomas dc22fc9
Add tests for attaching a collection;
hans-thomas a0cb480
Add tests for attaching multiple string ids;
hans-thomas 9f888ab
WIP
hans-thomas 3d4abab
Fix CS;
hans-thomas 24119d7
Detaching fixed;
hans-thomas 1833c5a
Add tests for detaching;
hans-thomas ad1c6ae
Add tests for detaching multiple ids;
hans-thomas 26f7ad2
Update MorphToMany.php
hans-thomas 8921127
Add test for syncing a mode;
hans-thomas dbaab5f
Add test for syncing a collection of models;
hans-thomas a2c6407
Add test for syncing multiple ids;
hans-thomas 284b8c3
WIP
hans-thomas 92040cc
Fix syncing in inverse mode
hans-thomas 79e5618
Add test for syncing a model;
hans-thomas 5ea4aef
Add test for syncing a collection of models;
hans-thomas 7008008
Add test for syncing multiple id;
hans-thomas f655f72
Fix CS;
hans-thomas 4932b3b
Fix eager loading relation and refreshing the model before lazy loading;
hans-thomas c1df501
Use load and refresh in tests;
hans-thomas d2b8267
Merge branch '4.1' into 2640-morph-to-many
hans-thomas c8ee4ce
New labelsWithCustomKeys relation defined with custom keys;
hans-thomas b4fca9d
Add tests for new relation with custom keys;
hans-thomas 7a32c98
Merge branch '4.1' into 2640-morph-to-many
hans-thomas db782d9
New clientsWithCustomKeys defined with custom keys;
hans-thomas 85c0159
Add test for clientsWithCustomKeys relation;
hans-thomas 31dbf5e
Add test for load and refreshing an instance with MorphToMany relation;
hans-thomas d54f46c
Add test for load and refreshing an instance with MorphedByMany relat…
hans-thomas 4026890
Fix CS;
hans-thomas 5307e19
Merge branch '2640-morph-to-many' of github.com:hans-thomas/laravel-m…
hans-thomas 9e21445
testMorphedByManyLoadAndRefreshing test updated;
hans-thomas 0872237
Comments updated;
hans-thomas 77bb847
Fix CS;
hans-thomas 3f3db65
WIP
hans-thomas 1903bb4
Fix pushing relation data to the instance;
hans-thomas 3888115
Fix CS;
hans-thomas bf990a5
Update RelationsTest.php
hans-thomas 9e41f46
Fix the error;
hans-thomas 91e0556
Rerun the pipeline;
hans-thomas f8c322a
Create extractIds method;
hans-thomas 2f83d02
Fix CS;
hans-thomas e411071
Update MorphToMany.php
hans-thomas c097392
Detach multiple ids at once;
hans-thomas 711ed0f
Remove meaningless comments;
hans-thomas b785d93
Pluralize using Str facade;
hans-thomas c443ca6
Remove guessBelongsToManyRelation method;
hans-thomas f6b5112
Remove getSelectColumns method;
hans-thomas 0674ad6
Remove getQualifiedForeignPivotKeyName method;
hans-thomas 874a7d3
Remove getForeignKey method;
hans-thomas add4f1c
formatRecordsList replaced with deprecated formatSyncList method;
hans-thomas 4e2766e
Remove useless condition in extractIds method;
hans-thomas 45a0c7a
Fix CS;
hans-thomas 5d229eb
extractIds method returns unique ids;
hans-thomas b56b90e
getHasCompareKey updated;
hans-thomas ca1cc62
Handling MorphToMany has query;
hans-thomas 5d77236
Add tests for handling has query in MorphToMany relationship;
hans-thomas 9618b84
Handling MorphedByMany has query;
hans-thomas 6d6a99e
getHasCompareKey removed;
hans-thomas 9f53b26
Add test for MorphedByMany has query;
hans-thomas c4ca146
Fix handling custom MorphedByMany has query;
hans-thomas 989b263
Add test for MorphByMany has query;
hans-thomas 1817c94
Revert "extractIds method returns unique ids;"
hans-thomas fc53e7d
Make extractIds method public;
hans-thomas d9dedf6
Add test for handling custom MorphToMany has query;
hans-thomas 800bfac
Fix CS;
hans-thomas 51b9e2a
Parent morphToMany call with a minuscule "m";
hans-thomas cc5bbcf
Add an empty line before return in handleMorphToMany method;
hans-thomas 92e6389
Add an empty line before return in handleMorphedByMany method;
hans-thomas 9198df9
Uncomment the checks of testHasManyHas;
hans-thomas 44c2517
Merge branch '4.1' into 2640-morph-to-many
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,12 +13,15 @@ | |
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Support\Collection; | ||
use MongoDB\Laravel\Eloquent\Model; | ||
use MongoDB\Laravel\Relations\MorphToMany; | ||
|
||
use function array_count_values; | ||
use function array_filter; | ||
use function array_keys; | ||
use function array_map; | ||
use function class_basename; | ||
use function collect; | ||
use function get_class; | ||
use function in_array; | ||
use function is_array; | ||
use function is_string; | ||
|
@@ -114,13 +117,46 @@ public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $ | |
$not = ! $not; | ||
} | ||
|
||
$relations = $hasQuery->pluck($this->getHasCompareKey($relation)); | ||
$relations = match (true) { | ||
$relation instanceof MorphToMany => $relation->getInverse() ? | ||
$this->handleMorphedByMany($hasQuery, $relation) : | ||
$this->handleMorphToMany($hasQuery, $relation), | ||
default => $hasQuery->pluck($this->getHasCompareKey($relation)) | ||
}; | ||
Comment on lines
-117
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a |
||
|
||
$relatedIds = $this->getConstrainedRelatedIds($relations, $operator, $count); | ||
|
||
return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not); | ||
} | ||
|
||
/** | ||
* @param Builder $hasQuery | ||
* @param Relation $relation | ||
* | ||
* @return Collection | ||
*/ | ||
private function handleMorphToMany($hasQuery, $relation) | ||
{ | ||
// First we select the parent models that have a relation to our related model, | ||
// Then extracts related model's ids from the pivot column | ||
$hasQuery->where($relation->getTable() . '.' . $relation->getMorphType(), get_class($relation->getParent())); | ||
$relations = $hasQuery->pluck($relation->getTable()); | ||
$relations = $relation->extractIds($relations->flatten(1)->toArray(), $relation->getForeignPivotKeyName()); | ||
return collect($relations); | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* @param Builder $hasQuery | ||
* @param Relation $relation | ||
* | ||
* @return Collection | ||
*/ | ||
private function handleMorphedByMany($hasQuery, $relation) | ||
{ | ||
$hasQuery->whereNotNull($relation->getForeignPivotKeyName()); | ||
return $hasQuery->pluck($relation->getForeignPivotKeyName())->flatten(1); | ||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** @return string */ | ||
protected function getHasCompareKey(Relation $relation) | ||
{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.