? ? ? Success

User tests: Successful: Unsuccessful:

avatar chrisdavenport
chrisdavenport
20 Jan 2015

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.

avatar chrisdavenport chrisdavenport - open - 20 Jan 2015
avatar jissues-bot jissues-bot - change - 20 Jan 2015
Labels Added: ?
avatar jackkum
jackkum - comment - 20 Jan 2015

awesome, i waiting this :smile:
#5601

avatar brianteeman brianteeman - change - 21 Jan 2015
Category Libraries
avatar wilsonge wilsonge - change - 1 Feb 2015
Labels Added: ?
avatar compojoom
compojoom - comment - 14 Mar 2015

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...


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/5837.
avatar compojoom compojoom - test_item - 14 Mar 2015 - Tested successfully
avatar chrisdavenport
chrisdavenport - comment - 14 Mar 2015

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?

avatar photodude
photodude - comment - 15 Dec 2015

:100: Would be nice to see this maybe added to 3.5

avatar chrisdavenport
chrisdavenport - comment - 15 Dec 2015

Then please test it and report your findings. Without testers it cannot be committed. ;-)

avatar photodude
photodude - comment - 16 Dec 2015

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.

avatar mbabker
mbabker - comment - 16 Dec 2015

Nope, it's right. It looks like the source branch for this pull request was deleted.

avatar chrisdavenport
chrisdavenport - comment - 16 Dec 2015

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.

avatar chrisdavenport
chrisdavenport - comment - 16 Dec 2015

Closing in favour of #8718

avatar chrisdavenport chrisdavenport - change - 16 Dec 2015
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2015-12-16 21:04:57
Closed_By chrisdavenport
avatar chrisdavenport chrisdavenport - close - 16 Dec 2015
avatar joomla-cms-bot joomla-cms-bot - change - 16 Dec 2015
Labels Added: ?

Add a Comment

Login with GitHub to post a comment