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?