Jump to:

7939 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject in SiteConfig

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 788 Views
  • ABK
    Avatar
    Community Member
    2 Posts

    DataObject in SiteConfig Link to this post

    I am folllowing a walk through on using the data object manager in siteconfig (http://archive.ssbits.com/2-4-working-with-siteconfig/). I have managed to get it working to the point where I can add objects to the backend but I can't seem to get them to display on the template.

    I am trying to loop through all items inside the object using a control loop but i don't seem to be having any joy... I get an error message [Notice] Undefined index:

    SiteConfig Extender

    <?php
    class SiteConfigExtra extends DataObjectDecorator{

    function extraStatics() {
    return array(
    'db' => array(
    "phone" => "Varchar",
    "email" => "Varchar",
    "twitterEmbed" => "HTMLText",
    "suppliers" => "HTMLText",
    "googleanalytics" => "HTMLText"
    ),
    'has_one' => array(
    "logo" => 'Image',
    "slogan" => 'Image',
    "defaultbanner" => 'Image'
    ),
    'has_many' => array(
    "nemomembers" => "nemomember"
    )
    );
    }

    public function updateCMSFields(FieldSet &$fields) {
    $fields->addFieldToTab("Root.Logo", new ImageField('logo', ' Logo (174x79)', Null, Null, Null, 'Uploads/site-assets/'));
    $fields->addFieldToTab("Root.Logo", new ImageField('slogan', ' Slogan (483x52)', Null, Null, Null, 'Uploads/site-assets/'));
    $fields->addFieldToTab("Root.Main", new TextField('phone',"Phone Number"));
    $fields->addFieldToTab("Root.Main", new TextField('email',"Email Address"));
    $fields->addFieldToTab("Root.Main", new TextAreaField('twitterEmbed',"Twitter Embed"));
    $fields->addFieldToTab("Root.Main", new TextAreaField('googleanalytics',"Google Analytics"));
    $fields->addFieldToTab("Root.Main", new ImageField('defaultbanner', 'Default Banner (973x200)', Null, Null, Null, 'Uploads/site-assets/'));
    $fields->addFieldToTab("Root.Suppliers", new HTMLEditorField('suppliers',"Suppliers"));

    $manager = new DataObjectManager(
    $this->owner,
    'NemoMembers',
    'NemoMember'
    );
    $manager->setSourceID($this->owner->ID);
    $fields->addFieldToTab("Root.NemoMembers", $manager);
    }

    }
    class SiteConfig_DataObjectManager extends DataObjectManager {
    function setSourceID($val) {
    if (is_numeric($val)) {
    $this->sourceID = $val;
    }
    }
    function sourceID() {
    if (isset($this->sourceID) && $this->sourceID !== null && is_numeric($this->sourceID)) {
    return $this->sourceID;
    }
    return parent::sourceID();
    }
    }
    ?>

    Member

    <?php
    class NemoMember extends DataObject
    {
    //db fields
    static $db = array (
    'Name'    => 'Varchar(255)',
       'Address1'    => 'Varchar(255)',
    'Address2'    => 'Varchar(255)',
    'Town'    => 'Varchar(255)',
    'County'    => 'Varchar(255)',
    'Postcode'    => 'Varchar(255)',
    'Lat'    => 'Varchar(255)',
    'Long'    => 'Varchar(255)',
    'Email'    => 'Varchar(255)',
    'Telephone'    => 'Varchar(255)'
    );

    //Relations
    static $has_one = array (
    'Logo'      => 'Image'
    );

    //Fields to show in the DOM table
    static $summary_fields = array(
    'Logo',
    'Name',
    'Address1',
    'Postcode',
    'Lat',
    'Long'
    );

       
       /**
        * Fields for DOM popup
        *
        * @param    void
        * @return    FieldSet DOM Object
        */
    public function getCMSFields()
    {
    return new FieldSet(
    new TextField('Name'),
    new TextField('Address1'),
    new TextField('Address2'),
    new TextField('Town'),
    new TextField('County'),
    new TextField('Postcode'),
    new TextField('Lat'),
    new TextField('Long'),
    new TextField('Email'),
    new TextField('Telephone'),
    new ImageField('Logo', 'Logo', Null, Null, Null, 'Uploads/member-logos/')
    );
    }
    }

    Template

    <% control SiteConfig.NemoMembers %>
    <p>$Name</p>
    <% end_control %>

    Please help, I have looked through other forum posts and haven't managed to get it to work. Please tell me it's something stupid that I have missed. It is driving me mad not being able to get it to work!

    Many thanks,
    Alex

  • KayB
    Avatar
    Community Member
    3 Posts

    Re: DataObject in SiteConfig Link to this post

    I just bumped on the same problem.. So subscribing on this thread
    Any help would be awesome!

  • DougProctor
    Avatar
    Community Member
    1 Post

    Re: DataObject in SiteConfig Link to this post

    You can create functions inside the SiteConfigExtra class that return dataobjects:

    function NemoMembers() {
    return DataObject::get('NemoMember');
    }

    Then you can output them in any template file like this:

    <% control SiteConfig %>
    <% control NemoMembers %>
    <p>$Name</p>
    <% end_control %>
    <% end_control %>

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