Jump to:

23472 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Creating filters for a search

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 322 Views
  • Bagzli
    Avatar
    Community Member
    31 Posts

    Creating filters for a search Link to this post

    Hey guys,

    Was wondering if somebody can help me theory craft for SS. I have ArticleHolder and ArticlePage. Holder will list all the Pages. What I want to achieve is user enters tags in a single field in the following manner: Tags-> News, History, Gaming, Other

    Then when on the page I click on tag News, I get articles that have the word news in the field above.

    At the moment I am achieving that in the following manner:

    ArticlePage::get()->filter('category:PartialMatch', $tag)->sort('Date DESC');

    Problem with this is that I have tag called Newstorie then it will return under news.

    My solution is to get all the results that partially contain the word and then clean up after by looping through each results and spliting it on the comma. Could would look like something like this:

    public function filterArticles($tag){
    $result = ArticlePage::get()->filter('category:PartialMatch', $tag)->sort('Date DESC');
    $exists = FALSE;
    foreach ($result as $temp){
    $tempArray = $temp->category->split(',');
    foreach ($tempArray as $currentArray){
    if($currentArray == $tag){
    $exists = TRUE;
    }
    }
    if($exists == FALSE){
    $result = $result->subtract($temp);
    }
    else{
    $exists = FALSE;
    }
    }
    return $result;
    }

    I cannot get subtract nor split to work.

    What would be a better way to accomplish this, or at least how do I get split and subtract to work.

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Creating filters for a search Link to this post

    It would seem that you are passing an object, not a list... see...

    http://api.silverstripe.org/3.1/source-class-DataList.html#543-560

    maybe "remove" is more appropriate...

    http://api.silverstripe.org/3.1/source-class-DataList.html#985-995

    If not then consider converting teh list from a DataList to ArrayList as I think the result of a "get" isn't possible to manipulate as easily.

  • Bagzli
    Avatar
    Community Member
    31 Posts

    Re: Creating filters for a search Link to this post

    I have tried remove, what it does it completely removes it from use. It basically unpublishes it and I can no longer access those news. I had to go to console to publish the data again in order for it to be seen.

  • martimiz
    Avatar
    Forum Moderator
    1083 Posts

    Re: Creating filters for a search Link to this post

    An alternative approach could be to 'prepare' the taglist in onBeforeWrite(). One that comes to mind: if you were to trim all spaces, and add a terminating comma, you could filter on 'News,' The adapted taglist would then appear as News,Sometag,Sometag, in the saved record, but I don't see how that would be a problem... Just a thought...

  • Bagzli
    Avatar
    Community Member
    31 Posts

    Re: Creating filters for a search Link to this post

    My idea idea, if I cannot find a better answer, is to have the user enter :MyTag:My SecondTag:MyThird Tag:

    Like this I just have to search for :Tag:, having 2 colons will remove any repetitions. Example: :News:MyNewsTag:MyOtherNewsTag:

    So when I do partial search for :News: only the first one will return.

    322 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.