Skip to content
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

[Feature] Add MorphToMany support #2670

Merged
merged 81 commits into from
Nov 22, 2023
Merged

Conversation

hans-thomas
Copy link
Contributor

@hans-thomas hans-thomas commented Nov 7, 2023

Hi,
I continued #2640 PR, thanks to @ithuis. I fixed the relationship and its inverse way,
but it still needs more tests for

  • attaching a collection of models
  • detaching action
  • finally syncing action
  • There is a problem with loading relation using load method

It has to be done by tomorrow.

Comment on lines +36 to +40
/** @inheritdoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
return $query;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the parent method is fine and we don't need to override this. I will write test for this too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is related to HybridRelations and never called in my tests.

Comment on lines -117 to +125
$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))
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a match expression to handle the MorphToMany relations has query.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment. Thanks for working on this @hans-thomas, and I really appreciate the amount of comments you added to the code! ❤️

src/Eloquent/HybridRelations.php Outdated Show resolved Hide resolved
@hans-thomas
Copy link
Contributor Author

@alcaeus My pleasure❤️

Co-authored-by: Andreas Braun <alcaeus@users.noreply.github.com>
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. A last question about the commented code and minor CS fixes.

src/Helpers/QueriesRelationships.php Show resolved Hide resolved
src/Helpers/QueriesRelationships.php Show resolved Hide resolved
tests/RelationsTest.php Outdated Show resolved Hide resolved

if ($this->getInverse()) {
// Attach the new ids to the parent model.
$this->parent->push($this->table, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍🏻 It seems that you have tested this case carefully.

hans-thomas and others added 3 commits November 22, 2023 14:01
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
@GromNaN GromNaN merged commit 82ddb83 into mongodb:4.1 Nov 22, 2023
13 checks passed
@GromNaN
Copy link
Member

GromNaN commented Nov 22, 2023

Thank you so much @hans-thomas

@hans-thomas hans-thomas deleted the 2640-morph-to-many branch November 22, 2023 11:38
@hans-thomas
Copy link
Contributor Author

@GromNaN I'm glad I could help❤️

Treggats pushed a commit to Treggats/laravel-mongodb that referenced this pull request Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants