Thanks Sean - Kinda there but not quite :)
I should have probably explained a little more about what I'm doing. I'm setting up a blog where I list products and stores that have that product. To help manage the different stores I have created the following.
- ProductPage (Page) - that is a list of products and its details (hasMany StoreList Objects)
- StorePage (Page) - that is a list of stores and its details (hasMany StoreList Objects)
- StoreList (DataObject) - that lists the url to that product for that store (hasOne ProductPage and hasOne StorePage)
Ideally what I'm kinda looking for is, when you go to a Product in the admin and add a Storelist item, it is automatically assigned to that Product (rather than going though a potential huge list of Storelist items to assign to a Product - eventually there will be hundreds of storelist items).
Also, when you are on each product in the admin, I want to only show the StoreList items related to that Product.
So I guess there are two things that are needed:
1. When adding a new StoreList item, how do you make it automatically related to the Product that you are creating the StoreList item from?
2. When viewing the list of StoreList items on a particular product, how do you filter it so that it only shows ShopList items related to that Product?
Any suggestions? I'm a bit of a novice at this but I'm learning ;)
This is what I have so far:
ProductPage.php
class ProductPage extends Page {
static $db = array(
"StartDate" => "Datetime",
"EndDate" => "Datetime",
);
static $has_one = array(
"ProductImage" => "Page_ResizeImages",
"Category" => "CategoryPage",
"Brand" => "BrandPage"
);
static $has_many = array(
"HomeHighlight" => "HomeHighlight",
"Stores" => "StoreList"
);
static $many_many = array(
"Sections" => "SectionPage",
);
static $allowed_children = "none";
static $default_parent = array('ProductHolder');
static $icon = "themes/loaded/images/icons/product";
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab('Root.Content.Main', "MenuTitle");
// Category Drop Down List
$categories = DataObject::get('CategoryPage');
$categoriesMap = $categories->toDropDownMap('ID', 'Title');
// Brands Drop Down List
$brands = DataObject::get('BrandPage');
$brandsMap = $brands->toDropDownMap('ID', 'Title');
// Sections Table Field List
$sectionsTablefield = new ManyManyComplexTableField(
$this,
'Sections',
'SectionPage',
array('Title' => 'Sections'),
'getCMSFields'
);
$sectionsTablefield->setPermissions(array());
// Store Table List
$productID = $this->ID;
$stores = DataObject::get('StoreList');
$storesTablefield = new ComplexTableField(
$this,
'Stores',
'StoreList',
array(
'StoreName' => "Store Name"
),
'getCMSFields_forPopup'
);
// Create Fields in CMS
$fields->addFieldToTab('Root.Content.Main', new DropdownField('CategoryID','Category',$categoriesMap,$this,'null',true));
$fields->addFieldToTab('Root.Content.Main', new DropdownField('BrandID',"Brand",$brandsMap,$this,'null',true));
$fields->addFieldToTab('Root.Content.Main', $sectionsTablefield);
$fields->addFieldToTab('Root.Content.Main', new PopupDateTimeField('StartDate','Start Date'));
$fields->addFieldToTab('Root.Content.Main', new PopupDateTimeField('EndDate','End Date'));
$fields->addFieldToTab('Root.Content.Stores', $storesTablefield);
$fields->addFieldToTab('Root.Content.Images', new ImageField('ProductImage','Product Image'));
return $fields;
}
}
class ProductPage_Controller extends Page_Controller {
}
StoreList.php
class StoreList extends DataObject {
static $db = array(
'StoreName' => 'Text',
'ProductUrl' => 'Text',
'Price' => 'Currency',
'PriceRange' => "Enum('Set Price,From Price','Set Price')",
);
static $has_one = array(
'Currency' => 'CurrencyType',
'Store' => 'StorePage',
'ProductPage' => 'ProductPage',
);
static $belongs_many_many = array(
);
function getCMSFields_forPopup() {
$fields = new FieldSet();
// Currency Drop Down List
$currencies = DataObject::get("CurrencyType");
if(!$currencies) {
$currenciesMap = null;
} else {
$currenciesMap = $currencies->toDropDownMap('ID', 'Name');
}
$currenciesDropDown = new DropdownField(
'CurrencyID',
'Currency',
$currenciesMap,
$this,
'null',
true
);
// Store Drop Down List
$stores = DataObject::get("StorePage");
if(!$stores) {
$storesMap = null;
} else {
$storesMap = $stores->toDropDownMap('ID', 'Title');
}
$storeDropDown = new DropdownField(
'StoreID',
'Store Tracking',
$storesMap,
$this,
'null',
true
);
$fields->push(new TextField( 'StoreName', 'Store Name' ));
$fields->push($storeDropDown);
$fields->push(new TextField( 'ProductUrl', 'Product Url' ));
$fields->push($currenciesDropDown);
$fields->push(new TextField( 'Price', 'Price' ));
return $fields;
}
}