Skip to content

[Proposal] Add embedsMany relationship to Eloquent Model #137

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 1 commit into from
Feb 27, 2014
Merged

[Proposal] Add embedsMany relationship to Eloquent Model #137

merged 1 commit into from
Feb 27, 2014

Conversation

alexandre-butynski
Copy link
Contributor

This pull request come from code that we are using in our Laravel projects. Because this package is not made to rewrite Eloquent, MongoDB specificities are generally not yet implemented. But the main difference, for web development, with a standard SQL driver is the ability to embed documents inside other ones.

We can do that with this package but only with manual data manipulations or quite a lot of custom code. That's why I have wrote an EmbedsMany class in order to deal with this specificity with a high level of abstraction.

In order to keep the spirit of this package, I tried to keep it as close as I can of Eloquent syntax. For example, the three lines below are exactly the same for a hasMany relationship than for a embedsMany relationship :

$address = new Address(array('city' => 'Paris'));
$user->addresses()->save($address);
$addresses = $user->addresses;

I also add several others methods that are basically shorthand to do the same things with raw data, with array of related models and without persistence. You can find that in the test cases.

The main difference from SQL Eloquent relationship is that this relationship is not, in fact, a query but directly an array of data. That's why I had to wrote an other relation parent class : EmbeddedRelation.

I just worked on adding an embedded model and retrieve the list of this models. More work could be done (method remove or delete, test edge cases...) but I want your opinion about that before.

What do you think about the idea, if we keep it simple and close to Eloquent syntax ? What do you think about the implementation (method naming, number of shorthand, architecture...) ?

@jenssegers
Copy link
Contributor

Looks great man!

But I don't really like the LogicException. What does Eloquent throw in a similar case?

jenssegers added a commit that referenced this pull request Feb 27, 2014
[Proposal] Add embedsMany relationship to Eloquent Model
@jenssegers jenssegers merged commit 828a490 into mongodb:master Feb 27, 2014
@jenssegers
Copy link
Contributor

Sorry, Eloquent seems to throw a LogicException as well! Did not know that!

@jenssegers
Copy link
Contributor

$address = new Address(array('city' => 'Paris'));
$user->addresses()->save($address);
$addresses = $user->addresses;

This does not work right? save() does not exists, and it seems that you need to do an additional save() on the parent object. Might have to look into this.

mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this pull request Sep 2, 2024
[Proposal] Add embedsMany relationship to Eloquent Model
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.

2 participants