Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Decorating Member and has-one relationship SilverStripe ver 2.4.7

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

Page: 1
Go to End
Author Topic: 344 Views
  • msawebdev
    Avatar
    Community Member
    10 Posts

    Decorating Member and has-one relationship SilverStripe ver 2.4.7 Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Decorating Member and has-one relationship SilverStripe ver 2.4.7 Link to this post

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

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

    }

  • msawebdev
    Avatar
    Community Member
    10 Posts

    Re: Decorating Member and has-one relationship SilverStripe ver 2.4.7 Link to this post

    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

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