Skip to content

Make where and orWhere just accept one array #80

Closed

Description

@Timu57

We deprecate the following usage of where and orWhere:

$store->where($fieldName, $condition, $value);

$store->orWhere($fieldName, $condition, $value);

Instead it should be used like this:

$store->where([$fieldName, $condition, $value]);

$store->orWhere([$fieldName, $condition, $value]);

Multiple clauses

For multiple where the user had to repeatedly use where:

$store->where($fieldName, $condition, $value)->where($fieldName2, $condition2, $value2)

Now it can be used like this:

$store->where([$fieldName, $condition, $value])->where([$fieldName2, $condition2, $value2]);
// or
$store->where([[$fieldName, $condition, $value], [$fieldName2, $condition2, $value2]]);

Activity

  1. added
    enhancement
    on Jan 13, 2021
  2. self-assigned this
    on Jan 13, 2021
  3. added this to the Version 2.0 milestone on Jan 13, 2021
  4. adrxn commented on Jan 20, 2021

    @adrxn

    To be able to create more complex clauses with unlimited random use of 'and' and 'or' maybe the following idea.

    E.g. to convert and expression like:
    (name like 'a%' or name like 'b%') and ((age >= 16 and age < 20) or (age >= 24 and age < 28))

    I'd like to propose the follwing JSON clause:
    {
    "and" : [
    {
    "or" : [
    [ "name", "like", "a%" ],
    [ "name", "like", "b%" ]
    ]
    },
    {
    "or" : [
    {
    "and" : [
    [ "age", ">=", 16 ],
    [ "age", "<", 20 ]
    ]
    },
    {
    "and" : [
    [ "age", ">=", 24 ],
    [ "age", "<", 28 ]
    ]
    }
    ]
    }
    ]
    }

    It will need recursive parsing.
    I implemented something like that in C# once to create linq expressions dynamically.

  5. Timu57 commented on Jan 20, 2021

    @Timu57
    MemberAuthor

    @adrxn Hi. That looks like a really nice idea.
    Thank you for pointing out that the current implementation is not capable of handling such nested expressions. Do you have an idea how we could further improve on that idea and make it easier for the user? We could have a representation of the expression that looks like this in SleekDB internally, but forcing the user to handle nested arrays like that is not such I good idea I think.

  6. adrxn commented on Jan 20, 2021

    @adrxn

    Indeed maybe you have to get used to the JSON-like annotation of queries.

    But the user has the freedom to make their clauses as complex as they want.
    I mean, it is still possible to use something simple like : ["age", ">=", 16]
    or use multiple where-clauses ...

    For multiple compares (like your examples, and can be more than 2) you need to define if they should be or-ed or and-ed like:
    { "and" : [["age", ">=", 16], ["age", "<", 20]] }

    The second 'and' is similar:
    { "and" : [["age", ">=", 24], ["age", "<", 28]] }

    These two can be or-ed like :
    { "or" : [ { "and" : [["age", ">=", 16], ["age", "<", 20]] }, { "and" : [["age", ">=", 24], ["age", "<", 28]] }] }

    And so on ...

    The only other way I can think of to make complex expressions more readable for the user is to make your own expression parser to allow things like:
    $store->where("(name like 'a%' or name like 'b%') and ((age >= 16 and age < 20) or (age >= 24 and age < 28))")

    ... but that will be harder for you to do ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementrelease candidate

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions