Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Dataobject in Dataobject?

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

Page: 1
Go to End
Author Topic: 206 Views
  • cSGermany
    Avatar
    Community Member
    37 Posts

    Dataobject in Dataobject? Link to this post

    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

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Dataobject in Dataobject? Link to this post

    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.

  • cSGermany
    Avatar
    Community Member
    37 Posts

    Re: Dataobject in Dataobject? Link to this post

    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

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Dataobject in Dataobject? Link to this post

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

  • cSGermany
    Avatar
    Community Member
    37 Posts

    Re: Dataobject in Dataobject? Link to this post

    Oh damn it, such a stupid mistake from me

    Thanks

    206 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.