Replies: 2 comments 2 replies
-
I think it should be a trait, so anyone can implement this on their own accord ... Then it's not a breaking change ... |
Beta Was this translation helpful? Give feedback.
1 reply
-
I remember reading about something similar about problem 1 on which a problem would be memory leakage ( because of the eager loading). Linking the parent and child in a circle by default seems not ok to me. Imagine a list GET / resource that will be retrieved with relation |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
In my latest work project, I extended Eloquent's
Model
class to create a system that prevents some duplicate database hits by using PHP's\WeakReference
. It has been purposely limited, but it succeeded in increasing the overall performance of the project.I believe I can create a more generic version of this that I could submit to Laravel core as a PR to enhance Eloquent's performance greatly (depending on each case and usage of Eloquent, of course). But before engaging in this work, I'd like to have the input of the community and know what do you think of this suggestion.
Before
To put it simply, here is what Eloquent does right now:
So there are two points that my suggestion will address. The first point is the main focus, the second is just a beneficial side effect of my proposed solution:
select * from authors where..
) that could have been prevented because the data is already there, in the$author
variable that was used to retrieve the$article
object.$author
does not refer to the same object as$article->author
. Again, this could be prevented by storing a weak reference to the parents' object ($author
in this case).After
With my suggestion (and hopefully a PR), here is what it would look like:
To sum-up, now:
$article->author
is, in fact, referring to$author
through the use of a weak ref. Therefore, there was no need to retrieve the data from the database.$article->author
is referring to$author
, therefore thename
is the latest value.Implementation
This system is currently well-tested and implemented in live in my current project. I created a custom
EloquentWeakReference
wrapper class, serializable, and that includes a failsafe that will do a database hit if the object has been destroyed. The use of a weak ref allows to safely store references to parent's object within children without risking a circular reference issue.I also had to use PHP reflection to discover all the Eloquent relations of an Eloquent object. To prevent using Reflection in the live system, I created a new
cache
command that caches the required information forEloquentWeakReference
to work.Disclaimer
By implementing this new way of storing eloquent's model relation, Laravel developers will have to be aware of what it means in terms of object reference. Indeed, one might expect that the line 7 should print the old "Foo" data because this is what Eloquent's has always been doing rightly since ever.
Therefore, if implemented, it might have a moderate to high impact when upgrading Laravel.
Discussion
Now, the discussion is open. In my particular case, this change was a no-brainer, with massive performance improvements and an immense reduction of the database workload. But there might be considerations that I am missing that might justify that it should not be in Laravel core, in which case I might create a composer package for it.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions