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

Remove force loading model relationships #2412

Merged
merged 1 commit into from
Apr 26, 2023
Merged

Conversation

erikn69
Copy link
Contributor

@erikn69 erikn69 commented Apr 26, 2023

Alternative to #2410

In some scenarios it is not necessary to load the relationships, and they are loaded later when they are required, this would allow less sqls to be executed in the mentioned scenarios
Example: batch user creation and assignment

@drbyte
Copy link
Collaborator

drbyte commented Apr 26, 2023

-            $model->load('permissions');
+             $model->unsetRelation('permissions');

One thing I want to research is what the issues were when we specifically added the load() calls into the package, to make sure we're adequately testing for the situations that "fixed".

@erikn69
Copy link
Contributor Author

erikn69 commented Apr 26, 2023

Actually it is not a bug/issue, it is a performance improvement, it is not necessary to load the relation immediately, it is enough to remove it, if there is any check after that the relation will be reloaded, otherwise this sql and memory usage are avoided

Look at the tests, the sql count is less on
calling_givePermissionTo_before_saving_object_doesnt_interfere_with_other_objects
calling_syncPermissions_before_saving_object_doesnt_interfere_with_other_objects
calling_syncRoles_before_saving_object_doesnt_interfere_with_other_objects
calling_assignRole_before_saving_object_doesnt_interfere_with_other_objects
This would decrease sql batch tasks too

what the issues were when we specifically added the load() calls into the package

Ok, after sync, detach, the relations may be preloaded, if so, the new changes are not accessible, so use load to get the updated relation data from db

@drbyte here efab130, laravel-permission/releases/tag/2.25.0

@drbyte
Copy link
Collaborator

drbyte commented Apr 26, 2023

what the issues were when we specifically added the load() calls into the package

Ok, after sync, detach, the relations may be preloaded, if so, the new changes are not accessible, so use load to get the updated relation data from db

Right. So it seems our existing loadMissing() calls take care of the things needed after (your proposed) unsetRelation has been called.

@drbyte
Copy link
Collaborator

drbyte commented Apr 26, 2023

+         $this->testUser->loadMissing('roles.permissions', 'permissions'); // load relations

I'm concerned that requiring this in the Cache tests may indicate ripple-effect problems.

@erikn69
Copy link
Contributor Author

erikn69 commented Apr 26, 2023

+         $this->testUser->loadMissing('roles.permissions', 'permissions'); // load relations

I'm concerned that requiring this in the Cache tests may indicate ripple-effect problems.

Those tests are supposed to check that no unnecessary calls are made to the db after the relations have been loaded.
Do I go back to how it was before?

@drbyte
Copy link
Collaborator

drbyte commented Apr 26, 2023

Do I go back to how it was before?

No. I'm just letting you know my thoughts as I look at the PR.
I'm always trying to identify any hidden BC breaks.

I think this should be fine.

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