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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Troubles pushing dropdown field into database

Go to End

4 Posts   2174 Views

Andrew Houle

Community Member, 140 Posts

4 November 2008 at 9:25am

This is more or less an extension on this forum discussion:

I'm at the point where my dropdown list populates w/ the correct info, but it doesn't save to the database. Basically, I am setting up the following pages: Degrees, which have courses, which have course types. The degrees are a pagetype and courses and course types are data objects. Here is my code for the Course.php data object:


class Course extends DataObject {
	static $db = array(
		'CourseTitle' => 'Text',
		'CourseDescription' => 'Text',
		'CreditHours' => 'Decimal',
		'PreRequisites' => 'Text',
		'MyCourseTypeID' => 'Decimal',
		'CourseType' => 'Text',

	static $has_one = array(
		'MyCourseTypes' => 'CourseTypes'
	static $belongs_many_many = array(
		'Degrees' => 'Degree'
	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push( new TextField( 'CourseTitle' ) );
		$fields->push( new TextAreaField( 'CourseDescription', 'Description' ) );
		$fields->push( new NumericField( 'CreditHours' ) );
		$fields->push( new TextField( 'PreRequisites' ) );

$ctype = DataObject::get("MyCourseTypes");
   $map = $ctype->toDropDownMap('ID', 'CourseType');
   $fields->push( new DropdownField(


		return $fields;


Any help you could give me would be very appreciated.


Forum Moderator, 5523 Posts

4 November 2008 at 10:11am

You have used

'MyCourseTypes' => 'CourseTypes' 

In the Database and have got


In the DropdownField(). You need to add the missing s to MyCourseTypeID.

Andrew Houle

Community Member, 140 Posts

4 November 2008 at 10:34am

You guys rock! Thanks so much for getting back to me so quickly. I'm going to be using SilverStripe to redesign Niagara University's website. And we are thrilled so far w/ the cms choice we've made.

The change you wrote about in the last thread was successful in getting the correct ID numbers in the database. The code should also serve to place the CourseType in the Course DB as well, right? I'm looking into getting that to work. If not, I suppose I could just call the CourseType from that database?

Thanks again for your help!

Andrew Houle

Community Member, 140 Posts

5 November 2008 at 4:33am

For the sake of closing this thread, here's what I did to get the CourseType from the correct database. I added this function to Course.php:

function MyCT() {
return DataObject::get('MyCourseTypes', "`ID` = '{$this->MyCourseTypesID}'" );


And dropped this control in the template:

<% if MyCT %>
<% control MyCT %>
<% end_control %>
<% else %>
No Course Type<br /><br />
<% end_if %>

Hopefully this can help someone in the future. I would love to see more tutorials and/or documentation on the datamodel and extracting data from databases in the future.