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.

General Questions /

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

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

Exclusive filter on a many-many relationship


Go to End


3 Posts   1092 Views

Avatar
Neomang

Community Member, 9 Posts

17 February 2017 at 5:26am

Edited: 17/02/2017 5:38am

The site I'm building uses posts that can belong to many topics in a many-many relationship. I then have pages that aggregate posts based on certain topics. However, on one page in particular, I want to have options to further filter the dataset to only return posts that belong to both the topic specified by the page, and at least one other topic (based on specific needs, arts/crafts, holidays, etc).
Filtering out just the posts assigned to the pagewide topic is simple I just use :

$this-Topic()->Posts()
but I'm running into trouble when I try to further filter the list. If I try :
$this-Topic()->Posts()->filter("Topic", $filter)
I get an Uncaught SS_DatabaseException that tells me there's no "Topic" column that my where clause can use. I double-checked the original class, and that's definitely the name of the relation. If I can resolve this issue, scaling it up to including multiple topics to filter by should be simple enough, but I'm not sure what I'm doing wrong.

Avatar
Devlin

Community Member, 344 Posts

25 February 2017 at 5:38am

I assume your Topic relation is a has_one relation. In this case, you will need to use ->filter("TopicID", $filter) instead -- to match the sql table column.

Avatar
Neomang

Community Member, 9 Posts

25 February 2017 at 6:50am

No, Topics are a many_many relation, as noted in the title. Posts can belong to many topics, and each topic can have many posts under it. It's not a huge deal now, as I've figured a sort of way around it since this functionality was not present.