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.

We've moved the forum!

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.

General Questions /

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

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

Dataobject in Dataobject?


Go to End


5 Posts   618 Views

Avatar
cSGermany

Community Member, 37 Posts

30 July 2013 at 11:01pm

Edited: 30/07/2013 11:22pm

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
copernican

Community Member, 189 Posts

31 July 2013 at 12:15am

Edited: 31/07/2013 12:16am

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

Community Member, 37 Posts

31 July 2013 at 2:46am

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
copernican

Community Member, 189 Posts

31 July 2013 at 4:13am

Edited: 31/07/2013 4:13am

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

Community Member, 37 Posts

31 July 2013 at 5:16am

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

Thanks :)