-
Notifications
You must be signed in to change notification settings - Fork 109
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
Hydration of belongsToMany relationship #20
Comments
Yeah, you need to skip hydration if creating then use the In your hydrator: /**
* @param RelationshipInterface $relationship
* @param Post $model
*/
protected function hydrateCompaniesRelationship(RelationshipInterface $relationship, User $user)
{
if (!$user->exists) {
return;
}
$user->companies()->attach($relationship->getIdentifiers()->getIds());
} v0.4 controller: protected function created(Model $model)
{
$companies = $this->getResource()->getRelationships()->getRelated('companies');
$this->hydrator->hydrateRelationship('companies', $relationship, $model);
} In v0.5 I need to update the event hooks in the controller to receive the resource as the second method |
PS: happy for any suggestions if you think there's a better way of doing it, but this is working for me for any relationship where the model needs to exist first |
I'm not enough into this library to suggest alternatives. But it seems a bit dirty to have to do this in the controller, as the hydrator—from my intuitive perspective—should be responsible for all hydration. If the Hydrator knows about calling |
Yeah, good point. I suppose there's a strong argument for this problem not being unique to Eloquent - i.e. there's a lot of database layers and abstractions that would need to do a hydration after creating the primary record. So on that basis, I could add to the Not sure what to call the method, maybe It'd require an |
Can you explain to an outsider, why the hydration of a newly created resource would be different from an existing? |
Ok, thinking about it again, then all the controller needs is a list of relationships that it needs to hydrate after creating the model. (As this problem only applies to creation, not to update.) Not sure what to call it as it applied to any So maybe your controller would have this property on it: protected $hasOneOrMany = ['companies']; And then the |
Actually it wouldn't need that property as it's already got a list of relationship mappings in the That makes sense to me, but realise I might not have explained it well. Basically it's possible for the controller to work this out itself. The controller needs to work it out because it knows when the save has been executed, but the hydrator does not. I'll add to |
When updating a resource, how does it know to call |
@egeriis yeah, it traverses the relationship object received from the client. The hydrator is just meant to contain the logic of transferring data from client's representation of the resource to the domain record. The controller is the thing executing the CRUD operations (which is where I believe it should be because that's kind of the role of the controller.) |
Did you need anything from me, on this one? |
Nothing more required - thanks for your input. I've actually solved this and the demo app has a I've added an interface for a hydrator to indicate that it needs to hydrate related resources as well as the "primary" resource. This is actually really useful as I have a couple of resources in one of my apps that contain attributes from a related Eloquent model, so it can be used for that purpose as well as has-many relationships: The Eloquent controller on the And additionally I've added an Putting it all together, here is the new |
Hey!
I'm having an issue with my Hydrator, and I can't seem to figure our how to solve this.
This method is intended to register the companies that belongs to a user. But upon creating a new user it fails, as
attach
will attempt to register the relationship immediately and the user is still to have an ID issued by the database.How do I go about this?
The text was updated successfully, but these errors were encountered: