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

Automatic insertion of related entities #8

Open
carlosafonso opened this issue Oct 27, 2015 · 9 comments
Open

Automatic insertion of related entities #8

carlosafonso opened this issue Oct 27, 2015 · 9 comments
Labels
Milestone

Comments

@carlosafonso
Copy link
Owner

If there are two models, Foo and Bar, related so that Bar belongs to Foo, if the following input is received:

POST /foos
{
    "attribute_1": "abc",
    "attribute_2": "def",
    "bar": {
        "another_attribute": "ghi"
    }
}

Then the controller should automatically create the related entity Bar and associate it to the newly created Foo.

@carlosafonso carlosafonso added this to the 0.1.0 milestone Oct 27, 2015
@carlosafonso
Copy link
Owner Author

We could check whether any of the input attributes matches an Eloquent relation in the associated model. If so, we should be able to create and attach the related entity.

We could even check what type of relation it is (belongsTo, hasMany...) and proceed as appropriate.

How about validation though? @namelivia

@namelivia
Copy link
Collaborator

Yeah, that's a good way to proceed. Also the child entities should be validated, probably getting the validation rules from their models.

@carlosafonso
Copy link
Owner Author

Alright. I'm worried about the hit on performance due to the dynamic relationship lookup. Maybe we could use a more static approach, but this is more annoying for the developer:

/* just came up with this, don't take it as something serious */
public function getRelatedEntities()
{
    return ['foo' => Foo::class];
}

@namelivia
Copy link
Collaborator

I can't ensure that would be neccesary due to performance issues, maybe we should not worry that much.

@namelivia
Copy link
Collaborator

My approach is to get the class methods by using a ReflecionClass, however this can be used to determine if the method exists or not. Is this approach the one you were thinkin on @carlosafonso ?

@carlosafonso
Copy link
Owner Author

Yes. Either that or something like method_exists()

@namelivia
Copy link
Collaborator

I've been also thinking that way, the thing is guessing the type of relationship.

@carlosafonso
Copy link
Owner Author

That's a good one. Maybe we could rely on the PHPDoc annotations (@return).

Or maybe this is just way too overkill and we're better off with a custom method that returns the accepted related entities. Maybe this is the easiest approach for now.

@carlosafonso
Copy link
Owner Author

And developers don't have to do anything if they don't want to use this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants