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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[SOLVED] Trouble getting the DropDownField & array to work in getCMSFields_Popup


Go to End
Reply


10 Posts   1857 Views

Avatar
RichMcNabb

Community Member, 34 Posts

31 August 2010 at 12:28pm

Edited: 02/09/2010 12:06pm

Hi,

I am trying to create a staff profile page and use a DropDownField for the $Location via a pop-up window in the CMS. This is what I have done so far but I am not sure where to add an array for the office locations (i.e. wellington, auckland, christchurch, london) so the dropdown menu are populated with values.

Any help would be awesome :)

<?php

class Staff extends DataObject {

static $db = array(
'FirstName' => 'Text',
      'LastName' => 'Text',
      'Email' => 'Text',
      'Location' => 'Varchar(100)',
      'JobTitle' => 'Text',
      'JobDescription' => 'Text',
);
   
static $has_one = array(
'Page' => 'StaffPage',
      'StaffProfileImage' => 'CustomImage',
      'Location' => 'Location',
      
);
static $fields = array(
'FirstName' => 'FirstName',
      'LastName' => 'LastName',
      'Email' => 'Email',
      'JobTitle' => 'JobTitle',
      'JobDescription' => 'JobDescription',
);

public function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push(new TextField('FirstName', 'First name'));
$fields->push(new TextField('LastName', 'Last name'));
      $fields->push(new DropDownField('Location', 'Location'));
      $fields->push(new TextField('Email'));
      $fields->push(new TextField('JobTitle', 'Job Title'));
      $fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
      $fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

return $fields;
}

}
?>

P.S. I did find this link (http://silverstripe.org/archive/show/141863#post141863) but wasn't able to reply as topic was archived.

- Rich McNabb

Avatar
RichMcNabb

Community Member, 34 Posts

31 August 2010 at 12:53pm

Edited: 31/08/2010 1:01pm

Updated code to this but am now getting - Fatal error: Class 'Location' not found in /var/www/cdpconz/sapphire/core/model/DataObject.php on line 1207

<?php

class Staff extends DataObject {

static $db = array(
'FirstName' => 'Text',
      'LastName' => 'Text',
      'Email' => 'Text',
      'Location' => "Enum('Wellington,Auckland,Christchurch,London','Wellington')",
      'JobTitle' => 'Text',
      'JobDescription' => 'Text',
);
   
static $has_one = array(
'Page' => 'StaffPage',
      'StaffProfileImage' => 'CustomImage',
      'Location' => 'Location',
      
);
static $fields = array(
'FirstName' => 'FirstName',
      'LastName' => 'LastName',
      'Email' => 'Email',
      'JobTitle' => 'JobTitle',
      'JobDescription' => 'JobDescription',
);

public function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push(new TextField('FirstName', 'First name'));
$fields->push(new TextField('LastName', 'Last name'));
      $fields->push(new DropDownField('Location', 'Location', singleton('Staff')->dbObject('Location')->enumValues()));
      $fields->push(new TextField('Email'));
      $fields->push(new TextField('JobTitle', 'Job Title'));
      $fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
      $fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

return $fields;
}

}
?>

Avatar
EdP

Community Member, 13 Posts

2 September 2010 at 2:45am

You need to tell the dropdown where to get its options. If you've only got four hard-coded options then you could use an ENUM, otherwise something from a table.

Here's a link to some documentation: [url]http://doc.silverstripe.org/dropdownfield[/url]

Ed

Avatar
RichMcNabb

Community Member, 34 Posts

2 September 2010 at 9:16am

Hi Ed,

Thanks for your reply I did get the drop down field working using the ENUM using that link you posted. The problem I am having now is: Fatal error: Class 'Location' not found in /var/www/cdpconz/sapphire/core/model/DataObject.php on line 1207

I am calling this from the StaffPage.ss template using the $Location. Any Ideas?

- Rich

Avatar
Martijn

Community Member, 271 Posts

2 September 2010 at 9:39am

Your using Location in your $db array and in your $has_one array. So SS will probably search for Location in the $has_one array.

Avatar
RichMcNabb

Community Member, 34 Posts

2 September 2010 at 9:51am

Hi Martijn,

Thanks for your message.
I am quite new to SS so wasn't sure if I need to put Location in the $has_one = array or the $fields = array? Hopefully it is in the right place in the $has_one.

To access the $Location from the $has_one array how would I do that?

- Rich

Avatar
Martijn

Community Member, 271 Posts

2 September 2010 at 10:02am

If you don't have a seperate Location DataObject, you don't.

Also if you want to add a seperate Location DataObject with a hasone relation, you have to avoid giving the same name to the relation as a DataObject field.

Avatar
RichMcNabb

Community Member, 34 Posts

2 September 2010 at 10:16am

Hi Martijn,

Thanks your help!! I got it to work by moving it from $has_one = array to $fields = array. One last question if you have time. The staff members are displayed in order of when the were added into the CMS. Is there an easy way to have them displayed by last name? Really appreciate your help. Cheers!

BTW I have paste the code below in case anyone else is having the same issue and wants some help.

<?php

class Staff extends DataObject {

static $db = array(
'FirstName' => 'Text',
'LastName' => 'Text',
'Email' => 'Text',
'Location' => "Enum('Wellington,Auckland,Christchurch,London','Wellington')",
'JobTitle' => 'Text',
'JobDescription' => 'Text',
);

static $has_one = array(
'Page' => 'StaffPage',
'StaffProfileImage' => 'CustomImage',

);
static $fields = array(
'FirstName' => 'FirstName',
'LastName' => 'LastName',
    'Location' => 'Location',
'Email' => 'Email',
'JobTitle' => 'JobTitle',
'JobDescription' => 'JobDescription',
);

public function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push(new TextField('FirstName', 'First name'));
$fields->push(new TextField('LastName', 'Last name'));
$fields->push(new DropDownField('Location', 'Location', singleton('Staff')->dbObject('Location')->enumValues()));
$fields->push(new TextField('Email'));
$fields->push(new TextField('JobTitle', 'Job Title'));
$fields->push(new TextareaField('JobDescription', 'Job Description - write a couple of paragraphs to describe your role at CDP.'));
$fields->push(new ImageField('StaffProfileImage', 'Staff profile image'));

   return $fields;
   }

}
?>

Go to Top