mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
[RFC] Set multiple parameters at once in the QueryBuilder #6579
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Mediagone on GitHub (Dec 3, 2020).
Hi everybody,
I would like to resurrect the discussion about the ability to add multiple parameters to the querybuilder at once.
Similar subjects was previously mentioned in the following discussions, without leading to a concrete solution:
First attempt
I'm looking for a way to handle parameter lists (eg. IN clauses). Since
setParameters()allows array as parameter value, I first tried to do:However, we cannot specify a custom doctrine type for items in an array, it only works with INT or STRING values.
In my case, IDs are objects stored as binary in the DB, but they implements
__toStringso they are automatically converted to strings internally and the comparison fails.SQL output (ids are converted to base 62 strings):
Expected SQL (binary values):
Second attempt
We can specify a custom type if we bind parameters separately, so we could do:
However it kinda defeats the main advantage of query builder. We can write a little helper class to ease parameters binding, but this would still not be satisfactory enough, as we cannot use the fluent interface.
Third attempt
The querybuilder also exposes the
setParameters()method, which accepts multiple parameters:Bad news:
setParametersdiscards all previously defined parameters! It's a serious problem when using criteria that can be combined in any order.Considered solutions
Add a new
addParameters()method that accepts the same arguments assetParameters, but acts likesetParameterin a loop.Modify
setParameter()(and subsequent classes) to handle an array of custom doctrine types (not only int/string arrays).Refactor parameter handling and create a
ParameterCollectionwith "handy methods" to manage the collection (cf. guilhermeblanco comments in previous discussions). However, it sounds like a huge task and BC break.What do you think would the best solution? I can implement it as soon as I have the green light from maintainers.
Thanks for reading :)
@greg0ire commented on GitHub (Dec 3, 2020):
Hi! I think the very best solution would be something that allows you to write the following:
I believe this already works since https://github.com/doctrine/orm/pull/590, but that wasn't documented, sadly.
The code that allows this to work is here:
01187c9260/lib/Doctrine/ORM/UnitOfWork.php (L3033-L3035)When we discussed this on Slack, you said it had the same behavior as the first version you posted, with
.id. I'd recommend you try debugging the piece of code above and find where the unexpected conversion happens, maybe this is just one small bugfix away from working.@Mediagone commented on GitHub (Dec 5, 2020):
Interesting, I never used entities directly in DQL for IN clauses, I'll keep that in mind for the future.
However, it doesn't solve the whole problem because I don't always manipulate entities, but only IDs as plain Value Objects. So I still need a way to define the Doctrine type manually.
@driskell commented on GitHub (Mar 27, 2023):
Would be great to see some support on this. As DX goes this threw me a good few hours and had to learn some internals far more than I intended. It seems to make custom types a fairly "poor-man" choice as they're not fully supported in all places.