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.

General Questions /

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

Creating filters for a search


Reply


5 Posts   412 Views

Avatar
Bagzli

Community Member, 37 Posts

23 February 2014 at 7:40am

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.

Avatar
swaiba

Forum Moderator, 1805 Posts

28 February 2014 at 9:44pm

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.

Avatar
Bagzli

Community Member, 37 Posts

2 March 2014 at 6:27am

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.

Avatar
martimiz

Forum Moderator, 1132 Posts

3 March 2014 at 12:50am

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

Avatar
Bagzli

Community Member, 37 Posts

3 March 2014 at 12:47pm

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.