From 432cc44a303083e57c8ba9634a20293449a3ef19 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 15 Jan 2013 00:43:43 +0100 Subject: [PATCH] Added many-many (plus extrafields) example --- code/Company.php | 12 ++++++++++++ code/Employee.php | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/code/Company.php b/code/Company.php index f160501..d5dcd20 100644 --- a/code/Company.php +++ b/code/Company.php @@ -22,6 +22,18 @@ class Company extends DataObject { 'Employees' => 'Employee' ); + public static $many_many = array( + 'PastEmployees' => 'Employee' + ); + + public static $many_many_extraFields = array( + 'PastEmployees' => array( + 'YearStart' => 'Int', + 'YearEnd' => 'Int', + 'Role' => 'Text', + ) + ); + static $belongs_many_many = array( 'RelationFieldsTestPages' => 'RelationFieldsTestPage', 'GridFieldTestPage' => 'GridFieldTestPage' diff --git a/code/Employee.php b/code/Employee.php index d7232ef..83bad43 100644 --- a/code/Employee.php +++ b/code/Employee.php @@ -15,6 +15,25 @@ class Employee extends DataObject { 'Company' => 'Company', 'ProfileImage' => 'Image' ); + + public static $belongs_many_many = array( + 'PastCompanies' => 'Company' + ); + + function getCMSFields() { + $fields = parent::getCMSFields(); + + if(method_exists('ManyManyList', 'getExtraFields')) { + $fields->addFieldToTab('Root.Main', + new NumericField('ManyMany[YearStart]', 'Year started (3.1, many-many only)') + ); + $fields->addFieldToTab('Root.Main', + new TextField('ManyMany[Role]', 'Role (3.1, many-many only)') + ); + } + + return $fields; + } public function requireDefaultRecords() { parent::requireDefaultRecords();