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.

Data Model Questions

Exception when using many_many and belongs_many_many


Reply

401 Views

Avatar
Rodskagg

6 February 2013 at 3:54am (Last edited: 6 February 2013 3:55am), Community Member, 8 Posts

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'
);