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

Adding a default association for many_many relationships


Go to End


2 Posts   1205 Views

Avatar
Neomang

Community Member, 9 Posts

7 June 2017 at 7:40am

I have several classes with many_many relations used in my site. However, for one section of the site, if a given object being referenced does not have an active relation with an object it belongs_many_many with, the page will crash (fatal error when trying to retrieve that relation). I'm looking into fixing it, and was wondering if there was a way to create an association that each new instance of this object is given as a default until it can be associated with the correct object so as to avoid this kind of error. I'm looking into various ways to fix this issue, this is just one of them.

As an example, in my System, we have posts that each belong to a topic, which belongs to an aggregator page. Each post is displayed via the aggregator page. So to link the post on another page, I create the link by getting the post's topic(s), getting the first topic in the list (where the error occurs), then getting that topic's aggregator page, then getting the link of that page. If this method has a simpler or more elegant fix, I'm also amenable to changing it, this was just what came together relatively simply.

Avatar
martimiz

Forum Moderator, 1391 Posts

22 June 2017 at 1:29am

Sorry for the late reply.

Hope you have this worked out by now, but if not, then could you send the parts of your code where these relations are defined (Post, Topic, Aggregator)?