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.

Data Model Questions

Decorating Member and has-one relationship SilverStripe ver 2.4.7


Reply

3 Posts   447 Views

Avatar
msawebdev

3 June 2013 at 5:15pm Community Member, 15 Posts

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

4 June 2013 at 5:38pm Forum Moderator, 5511 Posts

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

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

}

Avatar
msawebdev

4 June 2013 at 6:30pm Community Member, 15 Posts

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