User tests: Successful: Unsuccessful:
The current implementation of the database query where method is limited by an unfortunate restriction in that the first use of the glue parameter fixes the glue for all subsequent uses. For example,
$query->where(array('foo = 1', 'bar = 2'));
// WHERE foo = 1 AND bar = 2
$query->where(array('foo = 1', 'bar = 2'), 'OR');
// WHERE foo = 1 OR bar = 2
$query
->where(array('foo = 1', 'bar = 2'), 'AND')
->where(array('baz = 3', 'goo = 4'), 'OR');
// WHERE foo = 1 AND bar = 2 AND baz = 3 AND goo = 4
// Note that the OR glue is completely ignored in the second where statement.
This behaviour is inconvenient but can't be changed in Joomla 3.x because it would break backwards compatibility. Consequently, this PR offers the following additional syntax to support more complex WHERE clauses:
orWhere($conditions, $glue = 'AND')
$query
->where(array('foo = 1', 'bar = 2'))
->orWhere(array('baz = 3', 'goo = 4'));
// WHERE (foo = 1 AND bar = 2) OR (baz = 3 AND goo = 4)
$query
->where(array('foo = 1', 'bar = 2'))
->orWhere(array('baz = 3', 'goo = 4'), 'XOR');
// WHERE (foo = 1 AND bar = 2) OR (baz = 3 XOR goo = 4)
andWhere($conditions, $glue = 'OR')
$query
->where(array('foo = 1', 'bar = 2'))
->andWhere(array('baz = 3', 'goo = 4'));
// WHERE (foo = 1 AND bar = 2) AND (baz = 3 OR goo = 4)
$query
->where(array('foo = 1', 'bar = 2'))
->andWhere(array('baz = 3', 'goo = 4'), 'XOR');
// WHERE (foo = 1 AND bar = 2) AND (baz = 3 XOR goo = 4)
extendWhere($outerGlue, $conditions, $innerGlue = 'AND')
$query
->where(array('foo = 1', 'bar = 2'))
->extendWhere('XOR', array('baz = 3', 'goo = 4'));
// WHERE (foo = 1 AND bar = 2) XOR (baz = 3 AND goo = 4)
$query
->where(array('foo = 1', 'bar = 2'))
->extendWhere('XOR', array('baz = 3', 'goo = 4'), 'OR');
// WHERE (foo = 1 AND bar = 2) XOR (baz = 3 OR goo = 4)
The extendWhere method allows additional logical operators to be used beyond the commonly used AND and OR operators. For example, you can use XOR, AND NOT and OR NOT.
Backwards compatibility
There are no backwards compatibility issues. Existing code is unaffected because only new syntax is being added.
Testing
The code is accompanied by unit tests for the new methods and an extension to the where method test which verifies the original behaviour.
Labels |
Added:
?
|
Category | ⇒ | Libraries |
Labels |
Added:
?
|
I've tested this and it works.
However with great power comes great responsibility :p
With the new syntax one has to be really careful in what order the andWhere and orWhere parameters are added. If you change their position something completely different than expected would come out.
The benefit of the query class was that you could add everything in a mixed order, now with this, we have to to be really careful how we build the where part of the query. I'm not sure if this is a drawback or not...
Given an arbitrary query object with some unknown WHERE clause, adding an andWhere or an orWhere to it would, I think, give exactly the result you would expect. If you were to add a where([something], 'and') or a where([something], 'or') you would very likely get something unexpected since the second argument would be ignored, so I would say that the behaviour of the existing methods is rather more unexpected.
Can you give me an example of something unexpected coming out when using andWhere or orWhere?
Would be nice to see this maybe added to 3.5
Then please test it and report your findings. Without testers it cannot be committed. ;-)
I attempted to test the patch but I get an error.
"Error: The patch could not be applied because the repository is missing"
I'm guessing that this error is due to "This branch has conflicts that must be resolved" or some other issue with the patch tester.
Nope, it's right. It looks like the source branch for this pull request was deleted.
Git got its knickers in a twist a while back and the only solution was to delete my repo and start again with a clean fork. I'll create a new PR.
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2015-12-16 21:04:57 |
Closed_By | ⇒ | chrisdavenport |
Labels |
Added:
?
|
awesome, i waiting this
#5601