OK, I think I figured it out. Actually, thanks to dio5 indeed, because I revisited the relationship tutorial and it brought me back to the basics again, which is sometimes needed when you over-think things, as I tend to do ;)
Thanks dio5! Anyway, here's some code extracts (some irrelevant details taken out for brevity).
I have a DataObject called Product:
class Product extends DataObject {
public static $db = array(
'ProductNumber' => 'Text',
'ProductName' => 'Text',
'ProductDescription' => 'Text'
);
function getCMSFields_forPopup() {
$fields = new FieldSet();
//code for fields etc.
return $fields;
}
Then I have a ProductPage:
class ProductPage extends Page {
public static $db = array(
"SomeSwitch" => "Boolean"
);
public static $has_one = array(
'Image01' => 'Image',
'MyProduct' => 'Product'
);
public static $many_many = array(
'ProductColors' => 'ProductColor'
);
public static $belongs_many_many = array(
'CustomPages' => 'CustomPage'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$productsTablefield = new HasOneComplexTableField(
$this,
'MyProduct',
'Product',
array(
'ProductNumber' => 'Number',
'ProductName' => 'Name',
'ProductDescription' => 'Description'
),
'getCMSFields_forPopup'
);
$productsTablefield->setParentClass('ProductPage');
$productsTablefield->setOneToOne();
$productsTablefield->setPageSize(50);
$productsTablefield->setAddTitle( 'a Product' );
$colorsTablefield = new ManyManyComplexTableField(
$this,
'ProductColors',
'ProductColor',
array(
'ColorName' => 'Color name',
'ColorCode' => 'Color code'
),
'getCMSFields_forPopup'
);
$colorsTablefield->setParentClass('ProductPage');
$colorsTablefield->setPageSize(50);
$colorsTablefield->setAddTitle( 'a Color' );
$fields->addFieldToTab( 'Root.Content.Images', new CheckboxField("SomeSwitch", "Test switch."));
$fields->addFieldToTab('Root.Content.Images', new ImageField('Image01', 'Image 1.'));
$fields->addFieldToTab( 'Root.Content.Products', $productsTablefield );
$fields->addFieldToTab( 'Root.Content.Colors', $colorsTablefield );
return $fields;
}
}
class ProductPage_Controller extends Page_Controller {
}
This works of course, I can make product pages, select an associated product in the table, have it rendered in the template etc., the usual. Then I want to make a custom page with a custom selection of products that link to their respective product pages:
class CustomPage extends Page {
public static $db = array(
);
public static $has_one = array(
);
public static $many_many = array(
'ProductPages' => 'ProductPage'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$customsetTablefield = new ManyManyComplexTableField(
$this,
'ProductPages',
'ProductPage',
array(
'MenuTitle' => 'Name'
),
'getCMSFields_forPopup'
);
$customsetTablefield->setParentClass('CustomPage');
$customsetTablefield->setPageSize(50);
$customsetTablefield->setPermissions(array());
$fields->addFieldToTab( 'Root.Content.ProductsToShow', $customsetTablefield );
return $fields;
}
}
class CustomPage_Controller extends Page_Controller {
}
Now I can make various custom pages, select a bunch of products per custom page, render it with a ProductPages control block including links to the original product pages, etc. That was all there is to it. First I was thinking of using all kinds of functions with DataObject::get statements etc. but there's no need. The power of an object oriented framework indeed!
If you see anything wrong, please let me know. The only thing I'm stuck at is how to show Product properties like ProductName, ProductNumber etc. in the productsTablefield...
Thanks!