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

Author as a Dataobject


Reply

3 Posts   870 Views

Avatar
stew

28 October 2010 at 3:27am (Last edited: 28 October 2010 3:29am), Community Member, 30 Posts

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

Avatar
MarcusDalgren

29 October 2010 at 8:04pm Community Member, 288 Posts

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.

Avatar
stew

2 November 2010 at 3:05am (Last edited: 2 November 2010 3:11am), Community Member, 30 Posts

Yeah that worked perfectly thanks!