Jump to:

1131 Posts in 1965 Topics by 402 members

Upgrading SilverStripe

SilverStripe Forums » Upgrading SilverStripe » SS3 replacement for HasOneComplexTableField?

Ask questions about upgrading SilverStripe to the latest version.

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

Page: 1
Go to End
Author Topic: 866 Views
  • e-scape
    Avatar
    Community Member
    16 Posts

    SS3 replacement for HasOneComplexTableField? Link to this post

    Hi people,

    We have an intern going through the tutorials on SS3 at the moment and an interesting issue has cropped up. Part of the tutorial revolves around setting up a has_one relationship from a page to a DataObject. The tutorial continues to use HasOneComplexTableField which throws a warning and does not work without setting PHP to ignore notices etc.

    My question is, what is the new SS3 way of managing has_one data object relationships?

    GridField currently looks like it only manages 1-many and many-many relationships, or at least that is what all the various docs and tutorials revolve around. There does not seem to be any evidence of a way to manage has_one relationships directly.

    As a work around we are looking at a drop down for setting the relationship and a GridFieldConfig_RecordEditor setup for managing the DataObjects. Is this the only way right now or are we missing something obvious or undocumented?

    Many thanks
    Jason

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: SS3 replacement for HasOneComplexTableField? Link to this post

    I would also quite like to know this.

    The snippets I have read so far indicate this should be possible through GridField, but not really sure how...

    Mo

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: SS3 replacement for HasOneComplexTableField? Link to this post

    That could be very useful. In fact, i am looking for a solution as well. anyone ?

  • gaspra
    Avatar
    Community Member
    8 Posts

    Re: SS3 replacement for HasOneComplexTableField? Link to this post

    I also don't know about any generic solution but I followed the "dropdown" approach mentioned above. This works quite well if there are not too many objects to choose from for the has_one relation.

    An Example:
    I have a (potentially big) product list with products of only a few categories. Every Category has an associated Icon with a short descriptive Text. I therefore need a has_one relation (Product->Icon) and a has_many relation (Icon->Product)

    Product is a Page, Icon is a DataObject.

    The Icon DataObject is standard, as in all tutorials.

    In the Product Page I use a GridField RecordEditor for adding and deleting the Icon DataObjects:

    $icoconfig = GridFieldConfig_RecordEditor::create();
    $idl = DataList::create('ProductIcon'); // get _all_ ProductIcon DataObjects!
    $iconfield = new GridField(
    'All Icons Grid', // Field name
    'Product Icons', // Field title
    $idl,
    $icoconfig
    );
    $fields->addFieldToTab('Root.All Icons', $iconfield);

    For relating one specific Icon to the Product I then use a DropdownField:

    $drop = new DropdownField('MyIconID', 'Select Icon', ProductIcon::get()->map('ID','Name'));
    $drop->setEmptyString('No Icon'); // Used for "no icon" input
    $fields->addFieldToTab('Root.Main', $drop);


    BTW: The ->map() function shows the Icon Name in the Dropdown Field whereas it uses the "ID" as selected value!

    The MyIconID Table row gets added automatically if you have the according $has_one relation set up in the Product Page

    static $has_one = array(
    'MyIcon' => 'ProductIcon'
    );

    I hope this is helpful ant it's clear what I meant. If it is not, please ask again and I will try to improve what I have written. Overall it is just an explanation/example of the dropdown idea from the first post and it is only feasible if there are not too many DataObjects to choose from. A generic approach via GridField would therefore be very nice but in the meantime this might help. I have set it up that way and it works for me.

    866 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.