I'm trying to create a has_many relationship between a page type (PortfolioPage) and a dataobject (PortfolioImage) such that a PortfolioPage can have many PortfolioImages. So far I have set up:
class PortfolioPage extends Page {
static $db = array(
);
static $has_many = array(
'PortfolioImages' => 'PortfolioImage'
);
}
class PortfolioImage extends DataObject {
static $db = array(
'Title' => 'Varchar',
'Filename' => 'Varchar'
);
static $has_one = array(
'PortfolioPage' => 'PortfolioPage'
);
}
This has created the correct tables in the database, and the PortfolioImage table has the PortfolioPageID field. What I'm stuck on is how to use the CMS to edit the content of this table.
I want to add an editable table to PortfolioPage which allows PortfolioImages to be added to that particular page. I tried using TableField:
function getCMSFields() {
$fields = parent::getCMSFields();
$portfolioImageTable = new TableField('Images', 'PortfolioImage', PortfolioImage::$field_names, PortfolioImage::$field_types);
$fields->addFieldToTab('Root.Content.Images', $portfolioImageTable);
return $fields;
}
($field_names and $field_types are defined in the PortfolioImage class), but although this provides the required editable table it isn't using the PortfolioPageID field, so every page shows the same images.
Can anyone give me some guidance because the documentation for this is very sketchy and mostly geared towards many_many relationships.
Thanks.