bgultekin / laravel4-datatables-package

Server-side handler of DataTables Jquery Plugin for Laravel 4
267 stars 108 forks source link

[BUG] When using DB::raw() for raw queries the query is incorrectly added twice into the SQL #29

Closed ChrisReid closed 10 years ago

ChrisReid commented 11 years ago

Example:

        return static::select(DB::Raw('`teams`.`id`, `teams`.`country`, '  .
            '(select max(`no_draw_streak`) from `draws` where ' .
            '`draws`.`team_id` = `teams`.`id`) as `max_no_draw_streak`, ' .
            '(select avg(`no_draw_streak`) from `draws` where ' .
            '`draws`.`team_id` = `teams`.`id`) as `avg_no_draw_streak`'));

produces this SQL:

        select `teams`.`id`, `teams`.`country`, (select max(`no_draw_streak`) from `draws` where `draws`.`team_id` = `teams`.`id`) as `max_no_draw_streak`, (select avg(`no_draw_streak`) from `draws` where `draws`.`team_id` = `teams`.`id`) as `avg_no_draw_streak`, (select max(`no_draw_streak`) from `draws` where `draws`.`team_id` = `teams`.`id`) as `max_no_draw_streak`, (select avg(`no_draw_streak`) from `draws` where `draws`.`team_id` = `teams`.`id`) as `avg_no_draw_streak`
bgultekin commented 11 years ago

DB::raw is problematic usage for this package. But this may work:

select(array(
    'teams.id','teams.country',
    DB::Raw('(select max(no_draw_streak) from draws where draws.team_id = teams.id) as max_no_draw_streak '),
    DB::Raw('(select avg(no_draw_streak) from draws where draws.team_id = teams.id) as avg_no_draw_streak')
));
MarkVaughn commented 10 years ago

as @bllim noted, this each raw column needs to be passed individually into the select