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.

Data Model Questions /

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

Exception when using many_many and belongs_many_many


Go to End


1078 Views

Avatar
Rodskagg

Community Member, 26 Posts

6 February 2013 at 3:54am

Edited: 06/02/2013 3:55am

I have a DataObject called Staff which has a many_many relationship with a DataObject called Stage (which in turn has a belongs_many_many relationship with Staff). When editing the Staff items, Stages are connected using a CheckboxSetField.

Here's the problem. If I check any of the checkboxes before clicking on Create, I get an exception saying "ManyManyList::add() can't be called until a foreign ID is set". If I create the Staff item first, and then checks any of the checkboxes everything works fine. It's the same kind of problem you get if you try to upload an image into an Image field, before the parent DataObject has been created.

Is there any way to fix this? I think it's an incredibly stupid bug/feature, if it's supposed to be like this. My clients won't understand that they have to click on Create before clicking on the checkboxes. Extremely confusing.

In Staff.php:

static $many_many = array(
"Stages" => "Stage"
);

In Stage.php:

public static $belongs_many_many = array(
'Staff' => 'Staff'
);