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

1-to-many not working - not saving parent id


Go to End


7 Posts   2955 Views

Avatar
Romeo

Community Member, 67 Posts

4 December 2009 at 8:34am

I'm tearing my hair out trying to figure what I've done wrong here (if indeed I have). I just can't get a one-to-many relationship to 'stick'. I have a class, SymposiaPage which extends Page, which should have a 1-to-many relationship with a Symposium class which extends DataObject. In SymposiaPage I have this:

	  static $has_many = array (
	  	'Symposia' => 'Symposium'
	  );	  

In Symposium I have this:

	static $has_one = array (
		'SymposiaPage' => 'SymposiaPage'
	);		 

In SymposiaPage I also have this:

	  public function getCMSFields()
	  {
		  $fields = parent::getCMSFields();
		  $tablefield = new HasManyComplexTableField(
         $this,
         'Symposia',
         'Symposium',
         array(
	    'Year' => 'Year',
	    'Dates' => 'Dates',
	    'EventName' => 'Name',
	    'Location'=> 'Location'
         ),
         'getCMSFields_forPopup'
      );
      $tablefield->setAddTitle('Symposium');
 
      $fields->addFieldToTab('Root.Content.Symposia', $tablefield );
		  return $fields;
	  }

This is so that I can add multiple Symposium records within a tab in the SymposiaPage record. It duly creates the Symposium records, but it doesn't establish any relationship with the SymposiaPage record, and I can't determine why. When I look in the database, it shows SymposiaPageID of 0 within the Symposium record - i.e., the real ID of the parent SymposiaPage record has not been saved in the child Symposium records, thus meaning no link has been established between parent and children (in effect they're orphans).

This is so fundamental a problem I can't believe I'm doing something stupid but I've spent hours now trying to figure out what. Any help would be most appreciated! SS 2.3.4.

Avatar
Achilles Interactive

Community Member, 2 Posts

9 December 2009 at 12:22pm

Did you find a solution? I am having the same problem. I can make the front end work if I update the ID in the database, but I cannot get the One-Many relationship to automatically establish in the CMS.

Avatar
kcd

Community Member, 54 Posts

27 January 2010 at 10:54am

Edited: 27/01/2010 10:55am

I have the same problem, no parent relationship (ID not set). I have SS 2.3

DownloadPage has many DownloadFiles

    public static $has_many = array(
        'Downloads' => 'DownloadFile'
    );

...

   function getCMSFields() {
        $fields = parent::getCMSFields();
        
        $tablefield = new HasManyComplexTableField(
         $this,
         'Downloads',
         'DownloadFile',
         array(
	        'Name' => 'Name'
         ),
         'getCMSFields_forPopup'
      );
      $tablefield->setAddTitle('file for download');
      $fields->addFieldToTab("Root.Content.Files", $tablefield); 
        
        
      return $fields;
  }

Each DownloadFile has one (belongs to a) DownloadPage

    public static $has_one = array(
       'ParentPage' => 'DownloadPage',
	  'File' => 'File'
	);

I tried but didn't work out how to manually get the Parent->ID passed to the child's getCMSFields_forPopup() to force save the ParentID ... but I don't think I should have to.

Is this a bug with HasManyComplexTableField?

Any help will be appreciated.

Avatar
kcd

Community Member, 54 Posts

27 January 2010 at 11:02am

Oh and this caused me grief with the getter getDownloads() as I guess that functions by checking the child's parentID matches it's own ID

Avatar
Ben_W

Community Member, 80 Posts

19 February 2010 at 7:44pm

Edited: 19/02/2010 7:45pm

I had a quick look at your code, did not spot anything out of ordinary. However I do remember that I had this particular incident, where I have this one to many relationship set up between ClientPage and Testimonial page. After I tick the appropriate box to link clientpage with Testimonial page, the link is established on the staging table, not the live table, and you can't tell this until you refresh the page(changed page will have green color). so my guess would be to make sure you publish your 'Testimonial' page after you link them. In your case, would be Symposium page. Please give that a try.

Avatar
bummzack

Community Member, 904 Posts

19 February 2010 at 8:42pm

Edited: 19/02/2010 8:42pm

I just tested this, and it seems to work as expected (using SS 2.3.5)
What you have to do is:
- Check the checkbox of the items you want to link to the page
- Save and publish (to see the changes on the published site)

The DataObject Manager module has a replacement for the HasManyComplexTableField too. It doesn't require you to klick the checkboxes and only lists the items that have a relation to the current page... much nicer, you should check it out.

Avatar
kcd

Community Member, 54 Posts

22 February 2010 at 1:52pm

Ah thank you! All is working now. I don't know how I missed that