php - Laravel where exists query issue / not working -
alright have table of items duplicate items can exist same 'parent_uuid' can 'published' or 'discarded'. i'm trying 'discarded' group of items, published 0 there not duplicate row same 'parent_uuid' has published marked 1.
when write query in sql, behaves correctly:
select * `items` `items` `published` = 0 , `created_at` < '2015-08-30 17:23:29' , not exists (select 1 `items` `check` `check`.`parent_uuid` = `items`.`parent_uuid` , `published` = 1) group `parent_uuid`
my test cases return expect them be. when write in laravel query builder this:
$discarded = self::from('items items') ->where('published', 0) ->where('created_at', '<', carbon::now()->submonth()->todatetimestring()) ->wherenotexists(function($query) { $query->select(db::raw(1)) ->from('items check') ->where('check.parent_uuid', 'items.parent_uuid') ->where('published', 1); }) ->groupby('parent_uuid');
i more results i'm supposed to. results items should fail not exists subquery. when output last query, looks fine. , if copy query sql client , swap in variables, works expect to.
array (size=3) 'query' => string 'select * `items` `items` `published` = ? , `created_at` < ? , not exists (select 1 `items` `published` `published`.`parent_uuid` = ? , `published` = ?) group `parent_uuid`' (length=211) 'bindings' => array (size=4) 0 => int 0 1 => string '2015-08-30 17:23:29' (length=19) 2 => string 'items.parent_uuid' (length=22) 3 => int 1 'time' => float 442.8
i'm unsure i'm doing wrong , i've tried can think of.
does know might going on here? i'm on laravel 4.2.
the problem seems laravel's query builder , database driver related. temporary issue build of library have.
a solution found these situations create raw database string , pipe in advanced select query. this:
$where_not_exists = '1 `items` `check` `check`.`parent_uuid` = `items`.`parent_uuid` , `published` = 1'; $discarded = items::from('items items') ->where('published', 0) ->where('created_at', '<', carbon::now()->submonth()->todatetimestring()) ->wherenotexists(function($query) use ($where_not_exists) { $query->select(db::raw($where_not_exists)); }) ->groupby('parent_uuid'); if ($count === true) { return $discarded->count(); } return $discarded->get();
it's not terribly unreadable. sure never inject user sent input directly these!
Comments
Post a Comment