Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incosistency between HAVING and WHERE #245

Open
dakujem opened this issue Nov 28, 2019 · 0 comments
Open

Incosistency between HAVING and WHERE #245

dakujem opened this issue Nov 28, 2019 · 0 comments

Comments

@dakujem
Copy link

dakujem commented Nov 28, 2019

Version: 2.x, 3.x

I was wondering why the two methods where and having were so inconsistent.
Why does having only accept a string, while where will happily accept an array of string as well?
All the while multiple calls to having will overwrite the HAVING condition, while multiple calls to where will add more WHERE conditions.

This I find suboptimal, as both HAVING and WHERE are similar in the way the resulting SQL is built.

I have a use case, where the condition is wrapped into a "filter instruction object", that comes from a filter form and upon reaching the DB layer it is translated to SQL. Depending on the particular filtering setting, groupping and stuff can be activated, which is when some of the conditions need to be added into HAVING part of the SQL. Dealing with this inconsistency is laborous and defeats the purpose of database as a "smart" layer.

I would like the following pattern / pseudocode to be usable:

$filter = [
    'size' => 'big',
    'color' => 'red',
    'min-count' => 10,
    'min-value' => 500,
];
$query = $connection->select(...);
$map = [
    'min-count' => 'COUNT(`id`) ?',
    'min-value' => 'SUM(`price`) ?',
];
foreach ($filter as $subject => $value) {
    $mapped = $map[$subject] ?? null;
    if (!$mapped) {
        $query->where("%n ?", $subject, $value);
    } else {
        // this does not work as expected right now (gets overwritten)
        $query->having($mapped, $value);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant