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.

Data Model Questions /

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

Decorating Member and has-one relationship SilverStripe ver 2.4.7


Go to End


3 Posts   1528 Views

Avatar
msawebdev

Community Member, 15 Posts

3 June 2013 at 5:15pm

I am having an issue with decorating the Member object and declaring a has_one relationship to another DataObject.

Firstly I extend the DataObjectDecorator adding a has_one relationship (AreaManagerMember ).
Secondly I add the matching relationship on the related DataObject (JSSRegion)
I then declare the Object::add_extension('Member', 'AreaManagerMember'); in config
run dev/build
checking the Member table there is no field for the relationship

I discovered the statement below at http://doc.silverstripe.org/framework/en/2.4/reference/dataobjectdecorator:
NOTE If you want to add has_one or db items to a particular class, then that class must have that static variable explicitly defined, even if it's just a blank array. For example, the decorator method above wouldn't work if you added to a class that didn't have static $has_one explicitly declared on the object. This is because of PHP's crappy support for statics.

I am not completely sure what this means as the Member object does have a static has_one

Any and all help would be greatly appreciated.

Classes In Question:

<?php

class AreaManagerMember extends DataObjectDecorator{

function extraStatics(){
return array(
$has_one = array(
'JSSRegion' => 'JSSRegion'
)

);
}
}

<?php

class JSSRegion extends DataObject
{
static $db = array(
'Name' => 'Varchar(255)',
'RegionID' => 'Int'
);

//Fields to show in the DOM table
static $summary_fields = array(
'Name' => 'Name',
'RegionID' => 'RegionID'
);

static $has_one = array(
'Manager' => 'AreaManagerMember'
);

static $has_many = array(
'Stores' => 'Store',
);

Avatar
Willr

Forum Moderator, 5523 Posts

4 June 2013 at 5:38pm

You don't need the $ in DataObjectDecorators. E.g your code should be

function extraStatics(){
return array(
'has_one' => array(
'JSSRegion' => 'JSSRegion'
)
);

}

Avatar
msawebdev

Community Member, 15 Posts

4 June 2013 at 6:30pm

Thanks Will,

couldn't see the forest for the trees.

Should have realised it was an associative array not an array on varialbes

>> hangs head in shame