-
Notifications
You must be signed in to change notification settings - Fork 45
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
[FEATURE] QueryBuilder::whereIn #9
Comments
Interesting idea. I am trying to decide if it makes sense to add new methods for each condition, eg |
What other where methods do you envision being added in the future? Right now already all basic conditions can be expressed easily, only I did not yet dive into the code so I don't know exactly how the queries are represented internally, but I think there are two main cases.
...->where(function (SubWhere $sub) {
$sub->where("x = ?")
->orWhere(function (SubWhere $sub) {
$sub->where("y = ?")
->where("z = ?");
});
}); This would produce something like this: ... WHERE (x = ? OR (y = ? AND z = ?)) Number one is now well covered except for |
I'd rather have |
@MaartenStaa I am not sure what other conditions we'd support in the future, but I cannot rule adding more out in the future. If PHP had short closures, something like number two might be ok. For the time being, though I feel like it's awfully verbose. @naroga The issue with my solution is that I hate static methods. Almost every time I have used them, it's because I didn't spend enough time contemplating how I could've made them instance methods. Static methods are harder to mock and unit test, so I try to steer clear of them whenever I can. I'll have to think about this for a little bit. |
Agreed about static methods, as well as about the function syntax. For what it's worth though, this is the same format Laravel uses, so it is something people are used to. |
I personally think |
@Garethp the same could be achieved by sub-classing the query builder though, and then you could do |
True, but with |
@MaartenStaa @Garethp I'm leaning more towards leaving How about something like the following? (new QueryBuilder)->select("name")
->from("users")
->where((new ConditionFactory)->in("id", [1, 2, 3])); If the user plans on generating multiple conditions, they could create the factory beforehand: $conditions = new ConditionFactory();
(new QueryBuilder)->select("name")
->from("users")
->where($conditions->in("id", [1, 2, 3]))
->orWhere($conditions->between("age", 18, 21)); Thoughts? |
@opulencephp I just had the exact same Idea of a |
What would |
I'm thinking it'd return an interface with methods to get the generated SQL as well as the parameters bound by the condition. So, |
My knowledge on the inner workings of ORM's isn't up to scratch, but from an outside perspective I would assume that actually generating the SQL's would be the responsibilities of the adapters, no? Or am I off base there? |
To permit the most flexibility, Opulence's |
I've implemented this, and documented it. |
It would be nice if the query builder supported a
whereIn
function, along withorWhereIn
. This is something I miss quite often also in other frameworks.Example usage:
This would output:
I wouldn't mind giving implementing this a shot, the only question is whether this should default to using named or unnamed parameters. Perhaps it could default to unnamed, with an optional parameter to use named parameters? I'd like to hear what you think.
The text was updated successfully, but these errors were encountered: