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

fix: UserModel::assignIdentities() always produces a DB query #806

Merged
merged 3 commits into from
Sep 5, 2023

Conversation

michalsn
Copy link
Member

In the UserModel - when we use withIdentities() method the query is produced properly.

The problem occurs in the assignIdentities() method, when we're trying to assign identities to the users.
We're working with the User entity, and there is a line:

$newIdentities   = $mappedUsers[$userId]->identities;

It will call getIdentities() method which will produce a query to the DB, since there are no identities assigned yet.

This PR solves this problem.

@michalsn michalsn added the bug Something isn't working label Aug 29, 2023
Copy link
Collaborator

@datamweb datamweb left a comment

Choose a reason for hiding this comment

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

@michalsn thank you!

@datamweb datamweb closed this Sep 4, 2023
@datamweb datamweb reopened this Sep 4, 2023
@datamweb datamweb closed this Sep 5, 2023
@datamweb datamweb reopened this Sep 5, 2023
@datamweb datamweb requested a review from kenjis September 5, 2023 01:46
tests/Unit/UserTest.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member

kenjis commented Sep 5, 2023

Am I correct in assuming that this PR prevents queries to retrieve unnecessary Identities from being executed?
It's just useless and there has never been a problem with the results returned.

@kenjis
Copy link
Member

kenjis commented Sep 5, 2023

The code looks good.

Co-authored-by: kenjis <kenji.uui@gmail.com>
@michalsn
Copy link
Member Author

michalsn commented Sep 5, 2023

Am I correct in assuming that this PR prevents queries to retrieve unnecessary Identities from being executed?
It's just useless and there has never been a problem with the results returned.

There was never a problem with the incorrect result per se. The problem here was that we were producing n+1 queries for code like this:

$users = model(UserModel::class)->withIdentities()->findAll();
foreach ($users as $user) {
    $user->email;
}

That would be just fine, but since we use withIdentities() it should not work that way.

@datamweb datamweb merged commit d464eff into codeigniter4:develop Sep 5, 2023
28 checks passed
@datamweb
Copy link
Collaborator

datamweb commented Sep 5, 2023

Thanks All!

@michalsn michalsn deleted the fix/identities-fetching branch September 11, 2023 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants