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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Out of memory - many to many relationship


Go to End
Reply


983 Views

Avatar
Ronaldo71

Community Member, 10 Posts

5 June 2011 at 6:51am

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