Hydrahon icon indicating copy to clipboard operation
Hydrahon copied to clipboard

support subqueries as where values

Open midnightmonster opened this issue 3 years ago • 1 comments

Supports queries like this, which can be expressed as joins but are often easier to understand expressed this way--and faster to execute in MySQL:

$rentals = $h->table('rental_properties');
$favorites = $h->table('user_favorite_rental_properties');
$rentals->select()->where('id', 'in',
    $favorites->
        select(['rental_property_id'])->
        where('user_id',$_SESSION['user_id'])
)->where('available', true);

becomes

select * from `rental_properties` where `id` in (select `rental_property_id` from `user_favorite_rental_properties` where `user_id` = ?) and `available` = ?
[12345, true]

I'm on a deadline at the moment but will come back later this week to add some tests if you want them and approve this general approach.

midnightmonster avatar Jun 14 '21 18:06 midnightmonster

Line 546: elseif (is_object($where[3]) && ($where[3] instanceof BaseQuery))

should be: elseif (is_object($where[3]) && ($where[3] instanceof SelectBase))

radar3301 avatar Sep 15 '21 15:09 radar3301