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

Many_Many Relations Errors and Problems


Go to End


2 Posts   1849 Views

Avatar
CodeFade

Community Member, 12 Posts

26 August 2015 at 6:50pm

Edited: 26/08/2015 6:51pm

Hi All SilverStripe Professionals,

I am trying to create two ModelAdmins DataObjects which will connect to each other with (Belongs_Many_Many) and (Many_Many) relationship.

ClinicalCenters has many ClinicalDepartments
ClinicalDepartments has many ClinicalCenters

The problem is on the CMS, where I tried to fetch ClinicalCenters as selective checkboxes inside ClinicalDepartments. Not sure it is saved correctly in the Database, or a relation is actually taking place. See below screenshots:

Secondly when trying to list the clinical departments in the CMS and to show a summary field where each departments belongs to what clinical center, it is not showing at all. See below screenshot:

Third, trying to fetch all clinical department on the homepage, to view a list of departments and to what clinical center they belongs to, an error message shows:
Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'ManyManyList'
See below screenshot:

Here is my codes

ClinicalCenters.php

class ClinicalCenters extends DataObject {

	private static $db = array(
		'CenterName' => 'Varchar',
		'Description' => 'HTMLText',
		'Active' => 'Boolean'
	);
	
	private static $has_one = array(
		'FeaturedImage' => 'Image'
	);

	private static $belongs_many_many = array(
		'Clinical Departments' => 'ClinicalDepartments',
	);
	
	private static $summary_fields = array(
		'GridThumbnail' => 'Featured Image',
		'CenterName' => 'Center Name',
		'Active.Nice' => 'Center is active'
	);

ClinicalDepartments.php

class ClinicalDepartments extends DataObject {

	private static $db = array(
		'DepartmentName' => 'Varchar',
		'Description' => 'HTMLText',
		'Active' => 'Boolean'
	);
	
	private static $has_one = array(
		'FeaturedImage' => 'Image'
	);

	private static $many_many = array(
		'ClinicalCenters' => 'ClinicalCenters',
	);

	private static $summary_fields = array(
		'GridThumbnail' => 'Featured Image',
		'DepartmentName' => 'Department Name',
		'ClinicalCenters.CenterName' => 'Belongs to Clinical Centers',
		'Active.Nice' => 'Department is active'
	);

	public function getGridThumbnail() {
	
        if($this->FeaturedImage()->exists()) {
            return $this->FeaturedImage()->SetWidth(150);
        }

        return "(no image)";
    }


	public function getCMSFields() {
		
		$fields = FieldList::create(TabSet::create('Root'));
		
		$fields->addFieldsToTab('Root.Main', array(
			
			TextField::create('DepartmentName', 'Clinical Department Name'),
			HtmlEditorField::create('Description', 'Clinical Department Content Page'),
			CheckboxField::create('Active', 'Is the Department Active?'),
			CheckboxSetField::create(
				'ClinicalCenters',
				'Belongs to Clinical Centers'
			)->setSource(ClinicalCenters::get()->map('ID', 'CenterName')),

			$upload = UploadField::create('FeaturedImage', 'Featured Image')
		
		));
		
		$upload->getValidator()->setAllowedExtensions(array(
			'png', 'jpg', 'jpeg', 'gif'
		));
		
		$upload->setFolderName('clinical-images');
		
		return $fields;
	}
}

HomePage.php

class HomePage extends Page {
    
}

class HomePage_Controller extends Page_Controller {

	public function GetClinicalDepartments($count = 3) {

		return ClinicalDepartments::get()
			->filter(array(
				'Active' => true
			))
			->limit($count);	
	}
}

Please advice guys how is relations done (many_many) and (belongs_to_many), why not displaying in the summary_fileds (is it not connected properly?) and why an error shows on the homepage?

Avatar
justin_t_brown

Community Member, 22 Posts

9 September 2015 at 5:46am

Edited: 09/09/2015 5:48am

I've been looking into the same type of issue and my understanding so far is that you can't display many_many relationships in the $summary_fields. If you find out otherwise though I'd love to know!!