[TASK] Added support for nested query-builder
This feature will add support for nested queries.
Please not this example is taken from a live site, but it should provide an idea of what the changes are about.
Example (psuedo code):
$userDataQuery = $this->newQuery('user_data')
->getQuery()
->select('user_id')
->where('user_id', '=', \QB::raw('user.id'))
->where('value', 'LIKE', '%'. str_replace('%', '%%', $query) .'%')
->limit(1);
$otherQuery->where('username', 'LIKE', '%'.str_replace('%', '%%', $query).'%')
->orWhere('id', '=', $userDataQuery);
Output:
SELECT * FROM `user` WHERE `username` LIKE '%value%' OR `id` = (SELECT `user_id` FROM `user_data` WHERE `user_id` = user.id AND `value` LIKE '%value%' LIMIT 1)
Appreciate the PR Simon, but have you checked it? https://github.com/usmanhalalit/pixie#sub-queries-and-nested-queries
What! It's already there! What the hell - sorry about wasting your time, was sure that i'd read the entire documentation, but must have missed that section. 👍
But can I use those subqueries in my where statements like this:
... WHERE `id` IN (SELECT id FROM x) AND ...
Would it then just be something like:
->where('user_id', '=', \QB::subQuery($subQuery)) ?
It would be much cleaner though if the subQuery happened behind-the-scenes in the QueryBuilderHandler.php, so you don't have to know that the \QB::subQuery method exists.
My PR allows you to extract the sub-query QueryBuilder object back out of the original QueryBuilder-object instead of just turning it into a raw-query.
What do you think?