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.

Blog Module /

Discuss the Blog Module.

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

How to return multiple related tags?


Go to End


2 Posts   1435 Views

Avatar
digibrains

Community Member, 130 Posts

25 May 2011 at 6:32am

Edited: 26/05/2011 7:10am

Unlike the tag could widget which creates links based on a single tag, I'm trying to return entries based on all/each tags of a post.

If I use a function like this, I only get entries that match the entire tag string.

function GetTags(){
	return DataObject::get('BlogEntry',"Tags = '$this->Tags'",'','','');
}

What I want to return is all entries that share each of {$this->Tags}.

So if my entry is tagged (Chicken, BBQ, Summer) I want to return not just entries tagged 'Chicken, BBQ, Summer', but all entries tagged with Chicken and/or BBQ and/or Summer.

I assume my syntax Tags = {$this->Tags} is the issue. I probably need to bust apart $this->Tags into individual tags and return those, but I do not know the SQL syntax to say 'where one tag is x and one tag is y and one tag is z...'

Any guidance is greatly appreciated.

Thanks
Chris

Avatar
digibrains

Community Member, 130 Posts

26 May 2011 at 1:08pm

This is as close as I can get. I'm using $relatedLink = "Tags ='000'"; to start off the SQL since my foreach() is returning "OR Tags LIKE..."

"000" should return nothing. I can't believe that's a great way to do this, but I can live with it.

The only thing I can't figure out is how to remove the current entry page from the results.

class ExtendedEntry_Controller extends BlogEntry_Controller {
    function GetAllRelTags(){
	$theseTags = "$this->Tags";
	$eachTag = explode(',',$theseTags);
	$relatedLink = "Tags ='000'";
	foreach($eachTag as $relatedTag) {
		$relatedLink.= " OR Tags LIKE '%".$relatedTag."%' ";
	}
	return DataObject::get('BlogEntry',$relatedLink,'','','');
    }
}