Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Author as a Dataobject

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

Page: 1
Go to End
Author Topic: 846 Views
  • stew
    Avatar
    Community Member
    30 Posts

    Author as a Dataobject Link to this post

    Hey guys,

    I've hit a bit of a problem with a site I'm developing.

    I have a DataObject called Staff that is managed via StaffPage (holds information on each member of staff; name, role, description, headshot, email address), outputting to the StaffPage is easy. However I'm wanting to be able to use the same information in the site's NewsPage and ArticlePage as the Author information - so at the end of each NewsPage/ArticlePage you get a wee bit of blurb about the writer.

    I've been trying for most of the day to get this to work and I've managed to get a dropdown loaded up in the CMS (thanks to ssbits.com's dropdown population tutorial), however it wont save the information across - and I'm not convinced I've done it right anyway...

    Here is the code in NewsPage:

    <?php
    /**
    * Defines the NewsPage page type
    */
    class NewsPage extends Page {
       static $db = array(
          'Date' => 'Date'
       );
       
       static $has_one = array(
          'Staff' => 'Staff'
       );
       
       static $many_many = array(
       );

       function getMyObjectOptions()
       {
        if($Pages = DataObject::get('Staff'))
        {
        return $Pages->map('ID', 'StaffName', 'Please Select');
        }
        else
        {
        return array('No Staff found');
        }
       }
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new DatePickerField('Date', 'Date'));
          $fields->addFieldToTab("Root.Content.Main", new DropdownField(
           'Staff',
           'Please choose a Staff Member',
           $this->getMyObjectOptions()
          ));
          
          return $fields;
       }
       
    }

    class NewsPage_Controller extends Page_Controller {

    }

    Presumably someone has done this before, I'm just stuck at this point.

    Thanks,

    Stewart

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Author as a Dataobject Link to this post

    I'm not sure about this one since I usually don't use dropdowns but try calling it StaffID instead of Staff.

    $fields->addFieldToTab("Root.Content.Main", new DropdownField(
    'StaffID',
    'Please choose a Staff Member',
    $this->getMyObjectOptions()
    ));

    StaffID is what the actual field in the database is called so that might be why it's not saving. Let me know if it works.

  • stew
    Avatar
    Community Member
    30 Posts

    Re: Author as a Dataobject Link to this post

    Yeah that worked perfectly thanks!

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