Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filter with OR statement

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

Page: 1
Go to End
Author Topic: 453 Views
  • sashion
    Avatar
    Community Member
    23 Posts

    Filter with OR statement Link to this post

    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

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Filter with OR statement Link to this post

    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

  • sashion
    Avatar
    Community Member
    23 Posts

    Re: Filter with OR statement Link to this post

    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

    453 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.