I'm new to SilverStripe (and OO PHP) and am hoping someone can pick out what's wrong with or what I need to do to the code I currently have.
Requirements:
A parts list.
A bundles list.
Recording which bundles contain which parts.
I've got so far as successfully creating the CMS fields for the two lists, but am stuck on controlling which parts are in which bundles.
After a /dev/build, I get this in the admin:
[User Warning] I could not find the relation PartsBundles in PartsHolder or any of its ancestors.
GET /admin
Line 54 in /data/htdocs/ac/dataobject_manager/code/ManyManyDataObjectManager.php
I think I may need to change '$this' in the '$manager_parts_bundles' DOM subclass in PartsHolder.php to something else to let SilverStripe know that it needs to look into one of the other two files to establish the relationship, but I'm not sure how to go about this.
Here are my files:
PartsHolder.php
class PartsHolder extends Page {
static $has_many = array(
'Parts' => 'Part',
'Bundles' => 'PartsBundle'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$manager_parts = new DataObjectManager(
$this,
'Parts',
'Part'
);
$fields->addFieldToTab('Root.Content.Parts', $manager_parts);
$manager_bundles = new DataObjectManager(
$this,
'Bundles',
'PartsBundle'
);
$fields->addFieldToTab('Root.Content.Bundles', $manager_bundles);
$manager_parts_bundles = new ManyManyDataObjectManager(
$this,
'PartsBundles',
'PartsBundle'
);
$fields->addFieldToTab('Root.Content.Bundles', $manager_parts_bundles);
return $fields;
}
}
class PartsHolder_Controller extends Page_Controller {
}
Part.php
class Part extends DataObject {
static $db = array(
'PartNumber' => 'Varchar',
'Description' => 'Text',
'PurchasePrice' => 'Varchar',
'SupplierPartNumber' => 'Varchar',
'SupplierDescription' => 'Text',
'SupplierTerms' => 'Text',
'SalesPrice' => 'Varchar'
);
static $has_one = array(
'PartsHolder' => 'PartsHolder',
'Image' => 'Image'
);
static $belongs_many_many = array(
'Bundles' => 'PartsBundle'
);
static $summary_fields = array(
'PartNumber' => 'PartNumber',
'Description' => 'Description',
'PurchasePrice' => 'PurchasePrice',
'SalesPrice' => 'SalesPrice'
);
function getCMSFields() {
return new FieldSet(
new TextField('PartNumber'),
new TextAreaField('Description'),
new NumericField('PurchasePrice'),
new TextField('SupplierPartNumber'),
new TextAreaField('SupplierDescription'),
new TextAreaField('SupplierTerms'),
new NumericField('SalesPrice'),
new ImageField('Image')
);
}
}
PartsBundle.php
class PartsBundle extends DataObject {
static $db = array(
'Description' => 'Text',
'SalesPrice' => 'Varchar'
);
static $has_one = array(
'PartsHolder' => 'PartsHolder'
);
static $many_many = array(
'Parts' => 'Part'
);
static $summary_fields = array(
'Description' => 'Description',
'SalesPrice' => 'SalesPrice'
);
function getCMSFields() {
return new FieldSet(
new TextAreaField('Description'),
new NumericField('SalesPrice')
);
}
}
Many thanks!