nahid / jsonq

A PHP query builder for JSON
Creative Commons Zero v1.0 Universal
870 stars 105 forks source link

Combining multiple OrWhere to one result in AND #51

Open dimitrihilverda opened 4 years ago

dimitrihilverda commented 4 years ago

I am working on a filter page, and I was wondering if we could combine all filters in one category like this:

If I search for 'lorum ipsum' in title and in intro and in content, if its found in any of those 3 then its a hit for that search, but I also like to see if node: specialty is 1 or 2 or 3 (than can easily be done by using whereIN.) and then I like to search in my custom macro and if that is also true then return that item.

so a hit should have ( 'lorum ipsum' in title OR 'lorum ipsum' in intro OR 'lorum ipsum' in content) AND (1 OR 2 OR 3 IN specialty) AND macro IS true.

Is this possible?

tomkeysers commented 4 years ago

I'm doing and wondering the same thing. @dimitrihilverda did you ever figure this out?

dimitrihilverda commented 4 years ago

Yes I did, sort of, I do a query, then get results, with the results I do another query, and so on until all are done. https://gist.github.com/dimitrihilverda/dde5fa04b05bdb1a94634dba9b25e64a

tomkeysers commented 4 years ago

I actually found another way to do this using the reset() function. Likeso:

$jsonq = new Jsonq();
$jsonq->json(json_encode($result));

$res = $jsonq
    ->where('subcategory_website', 'contains', $this->sublevel)
    ->get();

if( isset($_GET['colour']) ) {
    $colour_filters = $_GET['colour'];
    $colour_filters = explode(',', $colour_filters);

    $jsonq->reset();
    foreach ($colour_filters as $key => $value) {
        $jsonq->orWhere('colour_website', 'contains', $value);  
    }
    $res = $jsonq->get();
}

if( isset($_GET['material']) ) {
    $material_filters = $_GET['material'];
    $material_filters = explode(',', $material_filters);

    $jsonq->reset();
    foreach ($material_filters as $key => $value) {
        $jsonq->orWhere('material_website', 'contains', $value);
    }
    $res = $jsonq->get();
}

if( isset($_GET['age']) ) {
    $age_filters = $_GET['age'];
    $age_filters = explode(',', $age_filters);

    $jsonq->reset();
    foreach ($age_filters as $key => $value) {
        $jsonq->orWhere('Age (website)', '=', $value);
    }
    $res = $jsonq->get();
}

Notice that I start off with one required where query, and after that use $jqonq->reset() to function as a sort of AND to then be able to insert as many OR's as needed. And if needed followed again with a reset() AND, and again some OR's.

In the end my $res will hold the result of all queries.