shadowhand/latitude

Nested 'group's

riseoflex88 opened this issue · 3 comments

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?

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));

Let me know if that doesn't work as expected.

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)));