Solved it :)
<?php
class CarModel extends DataObject {
static $db = array(
'Title' => 'Varchar(255)'
);
static $has_one = array(
'Brand' => 'CarBrand'/*,
'Holder' => 'CarHolder',*/
);
static $has_many = array(
'Holders' => 'CarHolder'
);
function getCMSFields_forPopup() {
$f = new FieldSet();
$myholder = $this->Brand()->ID;
$oData = DataObject::get("CarBrand", "ID = '$myholder'");
if ($oData) {
$CategoriesSource = $oData->toDropDownMap('ID','Title');
$dropdown = new DropdownField('BrandID', 'Brand', $CategoriesSource, $this->BrandID);
$f->push ( $dropdown );
}
// record detail fields
$f->push( new TextField('Title','Title') );
return $f;
}
}
?>
<?php
class CarBrand extends DataObject {
static $db = array(
'Title' => 'Varchar(255)'
);
static $has_many = array(
'CarModels' => 'CarModel',
'Holders' => 'CarHolder'
);
/*static $belongs_many_many = array (
'Holders' => 'CarHolder'
); */
function getCMSFields_forPopup() {
$f = new FieldSet();
$f->push( new TextField( 'Title', 'Title') );
return $f;
}
function getDropdownSummary() {
$String = $this->Title . " (" . $this->CarModels()->TotalItems() . ")";
return $String;
}
}
?>
<?php
class CarHolder extends Page {
static $db = array(
//car properties:
"Car_Vitesse" =>"Enum('Manual,Automatic')",
"Car_Category"=>"Enum('Sports/Luxury,Berling/Brake,Cabriolet,4x4/Off Roaders,Multi-purpose,Fun/2 Wheels')",
"Car_Version"=>"Text",
"Car_Year"=>"Text",
"Car_Mileage"=>"Text",
"Car_Price"=>"Text",
"Car_Description"=>"Text",
"Car_Contact_Name"=>"Text",
"Car_Contact_Email"=>"Text",
"Car_Contact_Phone"=>"Text",
"Car_Agent"=>"Text",
"Car_DateIn"=>"Date",
"Car_DateOut"=>"Date"
);
static $has_many= array(
'Photos' => 'CarImageResource'
);
static $has_one = array (
'Model' => 'CarModel',
'Brand' => 'CarBrand'
);
public function getCMSFields() {
$f = parent::getCMSFields();
$mng_records = new HasOneComplexTableField(
$this,
'Brand',
'CarBrand',
array(
'Title' => 'Title',
),
'getCMSFields_forPopup'
);
$f->addFieldToTab("Root.Content.Brand",$mng_records);
if($this->BrandID)
{
$bid = $this->BrandID;
$mng_records = new HasOneComplexTableField(
$this,
'Model',
'CarModel',
array(
'Title' => 'Title',
'Brand.Title' => 'Brand',
),
'getCMSFields_forPopup',
array("BrandID='$bid'")
);
$f->addFieldToTab("Root.Content.Model",$mng_records);
}
$vitess = new DropdownField('Vitesse','Vitesse',singleton('CarHolder')->dbObject('Car_Vitesse')->enumValues());
$category = new DropdownField('Category','Category',singleton('CarHolder')->dbObject('Car_Category')->enumValues());
$f->addFieldsToTab('Root.Content.Main',array($vitess,$category));
$manager = new ImageDataObjectManager(
$this, // Controller
'Photos', // Source name
'CarImageResource', // Source class
'Attachment', // File name on DataObject
array(
//'http_url' => 'http_url',
), // Headings
'getCMSFields_forPopup' // Detail fields
// Filter clause
// Sort clause
// Join clause
);
$f->addFieldToTab("Root.Content.Photos",$manager);
return $f;
}
}
class CarHolder_Controller extends Page_Controller {
}
?>
To manage the models and brands from a single interface (instead from doing it when you add a new CarHolder Page)
<?php
class CarModelAdmin extends ModelAdmin {
static $managed_models = array(
'CarBrand',
'CarModel'
);
static $url_segment = 'cars';
static $menu_title = 'Cars';
}
?>