pixie icon indicating copy to clipboard operation
pixie copied to clipboard

[TASK] Added support for nested query-builder

Open skipperbent opened this issue 9 years ago • 3 comments

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)

skipperbent avatar Apr 12 '16 09:04 skipperbent

Appreciate the PR Simon, but have you checked it? https://github.com/usmanhalalit/pixie#sub-queries-and-nested-queries

usmanhalalit avatar Apr 12 '16 10:04 usmanhalalit

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)) ?

skipperbent avatar Apr 12 '16 11:04 skipperbent

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?

skipperbent avatar Apr 12 '16 11:04 skipperbent