Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Dataobject in Dataobject?


Reply

5 Posts   224 Views

Avatar
cSGermany

30 July 2013 at 11:01pm (Last edited: 30 July 2013 11:22pm), Community Member, 37 Posts

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

Avatar
IOTI

31 July 2013 at 12:15am (Last edited: 31 July 2013 12:16am), Community Member, 189 Posts

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.

Avatar
cSGermany

31 July 2013 at 2:46am Community Member, 37 Posts

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

Avatar
IOTI

31 July 2013 at 4:13am (Last edited: 31 July 2013 4:13am), Community Member, 189 Posts

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'
);
}

Avatar
cSGermany

31 July 2013 at 5:16am Community Member, 37 Posts

Oh damn it, such a stupid mistake from me :D

Thanks :)