Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Problems with maintaining One to Many relations

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 830 Views
  • PeeJay
    Avatar
    Community Member
    8 Posts

    Problems with maintaining One to Many relations Link to this post

    Hi,

    As part of exercising tutorial-5, I have created Mentor and Student classes with one to many relation between Mentor to Student.

    Every thing is working fine except that MyMentorID column in Student table always shows 0. It beleive it should store the ID of Mentor object which it beongs to. Is that correct? Any idea where the problem lies?

    The code for Mentor and Student classes is shown below:

    Mentor.php:
    -------------
    <?php
    class Mentor extends Page {
       public static $db = array(
          'FirstName' => 'Text',
          'LastName' => 'Text',
          'Nationality' => 'Text'
       );

       static $has_many = array(
          'Students' => 'Student'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab( 'Root.Content.Main', new TextField( 'FirstName', 'First Name' ) );
          $fields->addFieldToTab( 'Root.Content.Main', new TextField( 'Lastname' ) );
          $fields->addFieldToTab( 'Root.Content.Main', new TextField( 'Nationality' ) );
          $tablefield = new HasManyComplexTableField(
             $this,
             'Students',
             'Student',
             array(
                'FirstName' => 'FirstName',
                'Lastname' => 'Family Name',
                'Nationality' => 'Nationality'
             ),
             'getCMSFields_forPopup'
          );
          $tablefield->setAddTitle( 'A Student' );   
          $fields->addFieldToTab( 'Root.Content.Students', $tablefield );
          return $fields;
       }   
    }
    class Mentor_Controller extends Page_Controller {
    }
    ?>

    Student.php:
    -------------
    <?php
    class Student extends DataObject {
       public static $db = array(
          'FirstName' => 'Text',
          'LastName' => 'Text',
          'Nationality' => 'Text'
       );
       static $has_one = array (
          'MyMentor' => 'Mentor'
       );
       function getCMSFields_forPopup() {
          $fields = new FieldSet();
          $fields->push( new TextField( 'FirstName', 'First Name' ));
          $fields->push( new TextField( 'LastName', 'Last Name' ));
          $fields->push( new TextField( 'Nationality', 'Nationality' ));
          return $fields;
       }
    }
    ?>

    Regards
    PeeJay

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Problems with maintaining One to Many relations Link to this post

    Once you've added the student, you might also have to tick the checkbox next to their name in the tablefield from the Mentor page.

    830 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.