And hello once again,
I'll post the complete code here for future reference. It's a working example with lots of room for improvement, but it's got the basic functionality covered. I'm getting no more errors when trying to create a new Record when there are no Categories, and it seems fairly stable, with filters, pagination and pagesize all working as expected.
DiscoHolder.php
<?php
class DiscoHolder extends Page {
static $has_many = array (
'Records' => 'DiscoRecord',
'Categories' => 'DiscoCategory',
);
public function getCMSFields() {
$f = parent::getCMSFields();
/*********************************
/* SET UP RECORDS MANAGER
/*********************************/
$mng_records = new DataObjectManager(
$this, // Controller
'Records', // Source name
'DiscoRecord', // Source class
array(
'Title' => 'Title',
'Category.Title' => 'Category',
),
'getCMSFields_forPopup'
);
// Filter By Category Setup
// only display the filter if there are categories present
if ($oData = $this->Categories()) {
$CategoriesSource = $oData->toDropDownMap('ID','DropdownSummary');
$mng_records->setFilter(
'CategoryID', // The field we're filtering
'Filter by Category', // The label for the filter field
$CategoriesSource // The dropdown map of values => display text. The values will be matched against the field.
);
}
$f->addFieldToTab("Root.Content.Records",$mng_records);
/*********************************
/* SET UP CATEGORY MANAGER
/*********************************/
$mng_cats = new DataObjectManager(
$this, // Controller
'Categories', // Source name
'DiscoCategory', // Source class
array(
'Title' => 'Title',
),
'getCMSFields_forPopup'
);
$f->addFieldToTab("Root.Content.Categories",$mng_cats);
// return the cms fields
return $f;
}
}
class DiscoHolder_Controller extends Page_Controller {
}
DiscoRecord.php
<?php
class DiscoRecord extends DataObject {
static $db = array(
"Title" => "Varchar(255)"
);
static $has_one = array (
'Holder' => 'DiscoHolder',
'Category' => 'DiscoCategory'
);
function getCMSFields_forPopup() {
$f = new FieldSet();
// build dropdown menu for category selection
$myholder = $this->Holder()->ID;
$oData = DataObject::get("DiscoCategory", "HolderID = '$myholder'");
// but only show the dropdown if there are any categories associated with the holder page
if ($oData) {
$CategoriesSource = $oData->toDropDownMap('ID','Title');
$dropdown = new DropdownField('CategoryID', 'Category', $CategoriesSource, $this->CategoryID);
$f->push ( $dropdown );
}
// record detail fields
$f->push( new TextField('Title','Title') );
return $f;
}
}
DiscoCategory.php
<?php
class DiscoCategory extends DataObject {
static $db = array(
"Title" => "Varchar(255)"
);
static $has_one = array (
'Holder' => 'DiscoHolder'
);
static $has_many = array (
'Records' => 'DiscoRecord'
);
function getCMSFields_forPopup() {
$f = new FieldSet();
$f->push( new TextField( 'Title', 'Title') );
return $f;
}
function getDropdownSummary() {
$String = $this->Title . " (" . $this->Records()->TotalItems() . ")";
return $String;
}
}
Schönes Wochenende!
Andi