-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
executeCacheQuery + fetchOne is a footgun #4634
Comments
This does not look like a bug, so I don't think we should fix it in 2.x. I think it still exist on 3.x though, and on 3.x, |
No it's definitely not a bug in itself, more something that's unclear if you don't read the docs carefully, and so it makes it easy to write bugs using this code. IMO it'd be great if it can be made harder to misuse, but I'll leave the how up to you because I'm definitely not familiar enough with the doctrine internals. I think there are a few ways to use this result:
|
@morozov WDYT? Maybe we could target 2.x with the suggested docblock improvements, and then do what I described above? 2.x could also receive a doc improvement, IMO we could make this a stronger warning, based on @Seldaek 's comment, right now it does not catch the eye enough I think:
|
I'm not touching this (#4189 (review)). |
Okay noted! |
Gave the documentation a try at #4640 |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug Report
Summary
When running a cached query, the docs do mention you have to fetch all results (
In order for the data to actually be cached its necessary to ensure that the entire result set is read (the easiest way to ensure this is to use one of the fetchAll*() methods
), but the code does not enforce this at all, making it an easy footgun.How to reproduce
Yesterday I noticed some deprecations warnings in this code:
So I figured "oh ok fetchAll is deprecated, and I see this only uses/selects one row actually so let me go ahead and use fetchOne and clean this up a little":
Then this disaster happened: https://twitter.com/seldaek/status/1389657778533322758
And ultimately I found in the docs that one has to fetch all records, which in this case I kinda did anyway with fetchOne but I guess you have to fetch until it realizes there is nothing left to fetch otherwise it's not enough. And if you don't fetch all then it somehow does not use the cached result it seems, or does not persist it, anyway it kinda defeated the whole purpose of having a cached query here..
So switching back to fetchAllAssociative resolved this issue in composer/packagist@998cb8a
Expected behaviour
I think using fetchOne on a cached statement should perhaps throw, or something along those lines, or on __destruct it could maybe check if there were more rows to be fetched and if not then do the caching as I expected it to do.
I am not entirely sure how this plays out with dbal 3.x, or if I maybe misunderstood everything, but I figured people with more of a clue about the project should be aware of the presence of this footgun :)
The text was updated successfully, but these errors were encountered: