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

Combining multiple OrWhere to one result in AND #51

Open
dimitrihilverda opened this issue Oct 21, 2019 · 3 comments
Open

Combining multiple OrWhere to one result in AND #51

dimitrihilverda opened this issue Oct 21, 2019 · 3 comments

Comments

@dimitrihilverda
Copy link

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
Copy link

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

@dimitrihilverda
Copy link
Author

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
Copy link

tomkeysers commented Jun 25, 2020

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.

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

2 participants