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.

Archive /

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

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

Troubles pushing dropdown field into database


Reply


4 Posts   1822 Views

Avatar
Andrew Houle

Community Member, 133 Posts

4 November 2008 at 9:25am

This is more or less an extension on this forum discussion: http://www.silverstripe.com/site-builders-forum/flat/150429

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:

<?php

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(
'MyCourseTypeID',
'CourseType',
$map)
);

      

      return $fields;
   }
   
}

?>

Any help you could give me would be very appreciated.

Avatar
Willr

Forum Moderator, 5513 Posts

4 November 2008 at 10:11am

You have used

'MyCourseTypes' => 'CourseTypes'


In the Database and have got

MyCourseTypeID


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

Avatar
Andrew Houle

Community Member, 133 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!

Avatar
Andrew Houle

Community Member, 133 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 Degree.ss template:

<% if MyCT %>
<% control MyCT %>
$CourseType
<% 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.