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

Many-to-many eager hydration (feature request) #1965

Open
pine3ree opened this issue May 4, 2023 · 4 comments
Open

Many-to-many eager hydration (feature request) #1965

pine3ree opened this issue May 4, 2023 · 4 comments

Comments

@pine3ree
Copy link

pine3ree commented May 4, 2023

Suppose we have a User to Role m:m relationship via a cross-reference table user_to_role.
It would be useful to be able to write:

$users = UserQuery::create()->with('Role')->find();

and only have 1 extra query to fetch related roles records (and hydrate them), instead of issuing 1 query per User entity.
We would need a UserToRoleQuery::filterByUserIDs([...]).
It would also be useful to be able to reference a many-to-many relation via plural name like ->with('roles').

kind regards

@mringler
Copy link
Contributor

mringler commented May 4, 2023

Have a look at the documentation on minimizing queries here and here, the latter also gives an example on how to use populateRelation() on result sets.

@pine3ree
Copy link
Author

pine3ree commented May 4, 2023

Hello @mringler ,

I tried using join+with and joinWith, and I also tried with('User.UserToRole') + with('UserToRole.Role'), but it does not seem to work for many-to-many. When I loop over the users in the example above, I still see 1-query per user to fetch the related roles. I totally missed the populateRelation method. I'll have a look at it. Still, a ->with('roles') call (with pluralization for related AR) would be easier to understand.

kind regards

@pine3ree
Copy link
Author

pine3ree commented May 4, 2023

Hello @mringler,

I checked populateRelation but it only supports RelationMap::ONE_TO_MANY and RelationMap::MANY_TO_ONE (

throw new UnsupportedRelationException(__METHOD__ . ' does not support this relation type');
)

@pine3ree
Copy link
Author

pine3ree commented May 4, 2023

Hello @mringler,

commenting my opening issue: actually a UserToRoleQuery::filterByUserId([...]) method is created and it supports an array of user-id translated into a IN sql condition. But it is not used to hydrate a many-to-many relation.

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

No branches or pull requests

2 participants