Nested 'group's
riseoflex88 opened this issue · 3 comments
riseoflex88 commented
Is it possible to group one group inside another? I need to recreate this Query for an existing search tool.
The existing sql is like so...
WHERE something = 1
AND (
po.po_duration_magnitude <= 90
AND po.po_duration_unit = 'Days'
) OR (
po.po_duration_magnitude <= 12
AND po.po_duration_unit = 'Weeks'
)
My best guess on doing this was to nest a group inside another group like so...
$sql_ob->andWhere(
group(
group(
field('po.po_duration_magnitude')->eq('90')
->or(field('po.po_duration_unit')->eq('Days'))
),
group(
field('po.po_duration_magnitude')->eq('12')
->or(field('po.po_duration_unit')->eq('Weeks'))
)
)
);
But, this doesn't work... :(
Any suggestions on how to achieve this?
shadowhand commented
You almost had the syntax right:
$is90Days = group(
field('po.po_duration_magnitude')->eq('90')->and(
field('po.po_duration_unit')->eq('Days')
)
);
$is12Weeks = group(
field('po.po_duration_magnitude')->eq('12')->and(
field('po.po_duration_unit')->eq('Weeks')
)
);
$select->where($is90Days->or($is12Weeks));
shadowhand commented
Let me know if that doesn't work as expected.
riseoflex88 commented
Amazing!
Almost perfect, just needed the extra 'group' to wrap them...
$select->where(field('something')->eq(1));
$is90Days = group(
field('po.po_duration_magnitude')->eq('90')->and(
field('po.po_duration_unit')->eq('Days')
)
);
$is12Weeks = group(
field('po.po_duration_magnitude')->eq('12')->and(
field('po.po_duration_unit')->eq('Weeks')
)
);
$select->addWhere(group($is90Days->or($is12Weeks)));