Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Filter with OR statement


Go to End


3 Posts   2263 Views

Avatar
sashion

Community Member, 25 Posts

18 August 2013 at 11:06pm

Hey Folks,

I would like to do a filter expression in SS3 similar like:

'Title like '%".$_GET['search']."%' OR Content like '%".$_GET['search']."%'

I tried this at first:
$myfilter['Title:PartialMatch'] = $_GET['search'];
$myfilter['Content:PartialMatch'] = $_GET['search'];

NewsPage::get()->filter($myfilter);

But it seems that it uses the filter with and AND-expression.

What is the correct way to get a filter that looks if the seach-parameter is either in content OR (!) in the title?

Thanks in adcance.
sash

Avatar
(deleted)

Community Member, 473 Posts

19 August 2013 at 7:50am

Edited: 19/08/2013 7:52am

With 3.1, you can use filterAny(). In 3.0, something like this is required:


	$clauses = array();
	foreach($myfilter as $key => $value) {
		if(substr($key, -13) == ':PartialMatch') {
			$clauses[] = '"' . substr($key, 0, -13) . '" LIKE \'%' . Convert::raw2sql($value) . '%\'';
		} else {
			$clauses[] = '"' . $key . '" = \'' . Convert::raw2sql($value) . '\'';
		}
	}
	NewsPage::get()->where(implode(' OR ', $clauses));

Or just build up the clauses themselves instead of $myfilter

Avatar
sashion

Community Member, 25 Posts

19 August 2013 at 9:19am

hey simon,

thx for your answer. I was hoping that there was an easy way with the PartialMatch Filter in SS3.
But I think then I just copy my first expression in the WHERE function instead of the filter function.

cheers,
sash