Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » 1-to-many not working - not saving parent id

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

Page: 1
Go to End
Author Topic: 2031 Views
  • Romeo
    Avatar
    Community Member
    67 Posts

    1-to-many not working - not saving parent id Link to this post

    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.
       

  • Achilles Interactive
    Avatar
    Community Member
    2 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

    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.

  • kcd
    Avatar
    Community Member
    54 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

    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.

  • kcd
    Avatar
    Community Member
    54 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

    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

  • Ben_W
    Avatar
    Community Member
    79 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

    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.

  • kcd
    Avatar
    Community Member
    54 Posts

    Re: 1-to-many not working - not saving parent id Link to this post

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

    2031 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.