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.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

SS3 replacement for HasOneComplexTableField?


Go to End


4 Posts   2406 Views

Avatar
Quadra

Community Member, 16 Posts

18 July 2012 at 1:55am

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

Avatar
Mo

Community Member, 541 Posts

25 August 2012 at 4:36am

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

Avatar
BenWu

Community Member, 97 Posts

27 September 2012 at 4:57am

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

Avatar
gaspra

Community Member, 8 Posts

2 October 2012 at 10:23pm

Edited: 02/10/2012 10:28pm

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.