Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Out of memory - many to many relationship

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 768 Views
  • Ronaldo71
    Avatar
    Community Member
    9 Posts

    Out of memory - many to many relationship Link to this post

    Hi all,

    First, what a breeze if you compare SilverStripe to Drupal... Love it! And I love the ModelAdmin a lot. How easy it is to add your own model.
    I ran into a recursive problem (I think) however, and I wanted to share "a" solution. Here's my case:

    The model consists of a School (Where multiple teachers are teaching) and Teachers (Who can teach on multiple schools). Hence, it's a many to many relationship, as defined in these model classes:

    <?php
    class School extends DataObject {

       static $db = array(
          'Name' => 'Varchar(50)',
          'City' => 'Varchar(35)'
       );

       static $many_many = array('Teachers' => 'Teacher');

       public function getTitle() {
          return $this->City . ' - ' . $this->Name;
       }

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $teachers = new ManyManyDataObjectManager(
             $this,
          'Teachers',
          'Teacher',
             array('FirstName' => 'FirstName', 'SurName' => 'SurName'),
          'getCMSFields_forPopup'
          );
          $fields->removeFieldFromTab('Root', 'Teachers'); // replace the tab with MMDOM tab
          $fields->addFieldToTab('Root.Teachers', $teachers);
           return $fields;
       }
       
       public function getCMSFields_forPopup() {
          return new FieldSet(
             new TextField('Name'),
             new TextField('City')
          );
       }
    }

    <?php
    class Teacher extends Member {

       static $db = array(
          'test' => 'Varchar(20)' // Dummy to force creation of "Teacher" table - Just for fun/testing
       );
          
       static $belongs_many_many = array('Schools' => 'School');
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $schools = new ManyManyDataObjectManager(
             $this,
          'Schools',
          'School',
             array('Name' => 'Name'),
          'getCMSFields_forPopup'
          );
          $fields->removeFieldFromTab('Root', 'Schools'); // replace the tab with MMDOM tab
          $fields->addFieldToTab('Root.Schools', $schools);

          return $fields;
       }
          
       public function getCMSFields_forPopup() {
          return new FieldSet(
             new TextField('FirstName'),
             new TextField('SurName')
          );
       }
    }

    Of course, I wanted to select teachers per school and schools per teacher, but if I remove both of the getCMSFields_forPopup methods, I get an out of memory error.

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 90 bytes) in /Users/ronald/Development/eclipse-php-workspace/leeskilometers/httpdocs/sapphire/core/model/DataObject.php on line 3185

    My best guess is that the form is recursively creating the tabs for the other class over and over again. Maybe everybody always defines the getCMSFields_forPopup methods (to have control over the fields), but I didn't and hence the error.

    Best regards,
    Ronaldo

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