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.

Form Questions /

[User Error] Uncaught Exception: ManyManyList::add() can't be called until a foreign ID is set


Reply


4 Posts   805 Views

Avatar
tfliam

Community Member, 20 Posts

1 June 2012 at 4:40pm

Edited: 01/06/2012 4:41pm

Hi,

in my form, I tried to add new record with many-many relations using checkboxsetfield, but it returned the error "[User Error] Uncaught Exception: ManyManyList::add() can't be called until a foreign ID is set".

Anyone know what does it mean and how do I solve it?

By the way, I m using SS3 beta 3.

Many thanks.

Avatar
tfliam

Community Member, 20 Posts

1 June 2012 at 5:49pm

Edited: 01/06/2012 6:03pm

I have found a way to solve it - that is to write once before save and rewrite. But I don't feel like this is the proper way to do it, can anyone advise me on this? Thanks.

....
$Event = new Event();               
if ($EventID == 0) {      
$Event->write(); //write the event once before save would solve the problem
} else {
$Event->ID = $EventID;
}

$form->saveInto($Event);
$Event->write();      
...

Avatar
Martin D.

Community Member, 21 Posts

29 September 2012 at 5:55am

tfliam,

Can you share where you would have the code?

Thanks

Avatar
tfliam

Community Member, 20 Posts

30 September 2012 at 5:50pm

Edited: 30/09/2012 5:51pm

The code is at the Page controller where you add your object, you may check out the structure of the codes in silverstripe tutorial http://doc.silverstripe.org/framework/en/tutorials/3-forms, my codes is as shown below:

class EventAddPage_Controller extends Page_Controller
{
...
public function doFrontEndForm($data, $form)
{

         $Event = new Event();                     
         $Event->write(); //for adding new record, it will have error when it comes to many-many relations if without write once   before save
                              
         $form->saveInto($Event);
         $Event->write();   
}
...
}