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.

Data Model Questions /

Filter with OR statement


Reply


3 Posts   536 Views

Avatar
sashion

Community Member, 23 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
simon_w

Forum Moderator, 474 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, 23 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