Hi there,
is it possible to have a dataobject inside a dataobject?
My first dataobject is for products and i want to include another for tier prices inside the first.
Can someone tell me how to do this?!
Thx in advance.
cSGermany
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Hi there,
is it possible to have a dataobject inside a dataobject?
My first dataobject is for products and i want to include another for tier prices inside the first.
Can someone tell me how to do this?!
Thx in advance.
cSGermany
Hi cSGermany,
It is possible to nest Dataobjects inside of dataobjects. You can do this just like you would add a dataobject to your Page. For example
Product.php class
class Products extends DataObject {
public static $has_many=array(
'TierPrices'=>'TierPrice'
);
public function getCMSFields(){
return new FieldList(
new GridField('TierPrices','Tier Price', $this->TierPrices(), GridFieldConfig_RecordEditor::Create())
);
}
}
TierList.php class
class TierList extends DataObject {
public static $db=array(
'Price'=>'Int'
);
public static $has_one=array(
'Parent'=>'Product'
);
public function getCMSFields(){
return new FieldList(
new TextField('Price')
);
}
}
Hope that helps.
Hi IOTI,
thank you it works. But the grid shows only "ID".
I usually call my Grid fields like this
$fields->addFieldToTab('Root.Auftraege', GridFieldBase::getGridField('Auftraege', _t('Dict.ORDERS', 'Auftraege'), $this->Orders()));
GridFieldBase.php
<?php
class GridFieldBase {
static public function getGridField($__name, $__label, $__storage, $__numItemsPerPage = 32, $__sortID = true) {
$gridFieldConfig = GridFieldConfig::create();
$gridFieldConfig->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator($__numItemsPerPage),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
);
if($__sortID) {
$gridFieldConfig->addComponents(
new GridFieldSortableRows('SortID')
);
}
return new GridField($__name, $__label, $__storage, $gridFieldConfig);
}
}
How can I use this for the actual case?
Thx in advance
cSGermany
If you want to control which fields appear in the grid you should use the static $summary_fields array on your dataobject. For example
class Product extends DataObject {
public static $db=array(
'Title'=>'Text',
'Category'=>'Text',
'Size'=>'Text'
);
public static $summary_fields=array(
'Title'=>'Title',
'Category'=>'Product Category'
);
}
Oh damn it, such a stupid mistake from me :D
Thanks :)