FaaPz / PDO

Just another PDO database library
MIT License
316 stars 103 forks source link

Parenthetical grouping in where clause #54

Closed ha17 closed 5 years ago

ha17 commented 8 years ago

Is there a way to group like (this = that AND that = this) AND (foo = bar OR foo = baz)

Query try 1:

    $selectStatement = $dbw->select($fields)
        ->from('projects p')
        ->join('int_users_projects iup', 'p.id', '=', 'iup."fkProjectId"')
        ->join('clients c', 'p."fkClientId"', '=', 'c.id')
        ->where('iup."fkUserId"', '=', $authUser->getId(), 'AND')
        ->where('p."fkSuperUserId"', '=', $authUser->getId(), 'OR')
        ->where('p.status', '=', 'active', 'AND')
        ->where('c.status', '=', 'active', 'AND');

Query try 2:

    $selectStatement = $dbw->select($fields)
        ->from('projects p')
        ->join('int_users_projects iup', 'p.id', '=', 'iup."fkProjectId"')
        ->join('clients c', 'p."fkClientId"', '=', 'c.id')
        ->where('iup."fkUserId"', '=', $authUser->getId())
        ->orwhere('p."fkSuperUserId"', '=', $authUser->getId())
        ->where('p.status', '=', 'active')
        ->where('c.status', '=', 'active');

I get:

SELECT p.id , p.uniq_id , p.name , p.description , p."startDate" , p."endDate" , p.latitude , p.longitude 
FROM projects p 
INNER JOIN int_users_projects iup ON p.id = iup."fkProjectId" 
INNER JOIN clients c ON p."fkClientId" = c.id 
WHERE iup."fkUserId" = ? OR p."fkSuperUserId" = ? AND p.status = ? AND c.status = ? 

But I want:

SELECT p.id , p.uniq_id , p.name , p.description , p."startDate" , p."endDate" , p.latitude , p.longitude 
FROM projects p 
INNER JOIN int_users_projects iup ON p.id = iup."fkProjectId" 
INNER JOIN clients c ON p."fkClientId" = c.id 
WHERE (iup."fkUserId" = ? OR p."fkSuperUserId" = ?) AND p.status = ? AND c.status = ? 
kwhat commented 5 years ago

Hi Ha17,

This issue has been addressed in version 2.x-dev. Please give the new version a try and see if this solves your issue. You want to use a couple of nested Grouping clauses.

Thanks!