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

Hybrid support for MorphToMany relationship #2690

Merged
merged 20 commits into from
Dec 11, 2023

Conversation

hans-thomas
Copy link
Contributor

Hello everybody, this PR adds hybrid support for the MorphToMany relationship.
I tested it in two scenarios. First, an SQL model makes a MorphToMany relation to a MongoDB model. In the second one, A MongoDB Model makes a MorphToMany to an SQL model.

@hans-thomas hans-thomas marked this pull request as ready for review November 29, 2023 08:29
@hans-thomas hans-thomas force-pushed the add-hybrid-support-to-morph-to-many branch from 6a2437c to af2be8c Compare November 29, 2023 12:04
@hans-thomas hans-thomas force-pushed the add-hybrid-support-to-morph-to-many branch from af2be8c to e561201 Compare November 29, 2023 12:08
@GromNaN
Copy link
Member

GromNaN commented Dec 11, 2023

Can you update this PR please. There is some conflicts.

@hans-thomas hans-thomas force-pushed the add-hybrid-support-to-morph-to-many branch from 1ce14da to 8440c57 Compare December 11, 2023 11:59
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.

Thank you for the update.


// Attach the new parent id to the related model.
$model->push($this->foreignPivotKey, $this->parseIds($this->parent), true);
$model->push($this->foreignPivotKey, (array) $this->parent->{$this->parentKey}, true);
Copy link
Member

Choose a reason for hiding this comment

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

(array) cast should not be used. If the value is an ObjectId, the result is not the one you expect.

var_dump((array) new MongoDB\BSON\ObjectId());
/*
array(1) {
  'oid' =>
  string(24) "6577071f3fded0903a01f030"
}
*/

Here we know it's not a list of ids, so a simple array wrapping is fine.

Suggested change
$model->push($this->foreignPivotKey, (array) $this->parent->{$this->parentKey}, true);
$model->push($this->foreignPivotKey, [$this->parent->{$this->parentKey}], true);

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 will double-check it later. thanks for the review❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ObjectId fields are always string. I tested this with a primary key and a custom key and a custom key was cast to ObjectId.
In all cases, the $this->parent->{$this->parentKey} value is string. Your suggestion is more readable, but I don't see any bug here to open a PR.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's something we'll have to fix if we manage to store references as ObjectId. But there are many other hurdles to overcome, and I think this notion of "all keys are strings" is too hard-wired into Eloquent anyway.

@GromNaN GromNaN merged commit 1a4a972 into mongodb:4.1 Dec 11, 2023
15 checks passed
@GromNaN
Copy link
Member

GromNaN commented Dec 11, 2023

Thank you @hans-thomas

@hans-thomas hans-thomas deleted the add-hybrid-support-to-morph-to-many branch December 11, 2023 17:37
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.

None yet

2 participants