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

Missing ParentID Field During Get


Reply

2 Posts   1082 Views

Avatar
SuperStromboli

24 May 2011 at 7:54am (Last edited: 24 May 2011 7:55am), Community Member, 3 Posts

Hi Everyone,

Long time stalker, first time poster. I'm still a SilverStripe noob so please be gentle. I'm working with a Course class and a page that has_many Courses. Defined below:

<?php
   class Course extends DataObject {
      static $db = array(
         "Name" => "Varchar",
         "Number" => "Int",
         "Description" => "Text",
         "Active" => "Boolean"
      );
      
      static $has_one = array(
         "Category" => "CourseCategory"
      );
      
      public function getCMSFields_forPopup()
      {
         $categories = DataObject::get("CourseCategory");
         $dropdown = new DropdownField('Category');
         
         if ($categories) {
            $map = $categories->toDropDownMap('ID', 'Name');   
            $dropdown = new DropdownField('CategoryID', 'Category', $map);
         }
         
         return new FieldSet(
            $dropdown,
            new TextField('Name'),
            new TextField('Number'),
            new TextareaField('Description'),
            new CheckboxField('Active')
         );
      }
   }

<?php
class CoursesPage extends StandardPage {

   static $has_many = array(
      "Courses" => "Course"
   );

   public function getCMSFields() {
      $fields = parent::getCMSFields();
      
      $course_fields = new DataObjectManager(
         $this,
         "Courses",
         "Course",
         array("Category.Name" => "Category", "Number" => "Number","Name" => "Name", "Description" => "Description", "Active" => "Active"),
         "getCMSFields_forPopup"
      );
      
      $course_category_fields = new DataObjectManager(
         $this,
         "Categories",
         "CourseCategory",
         array("Name" => "Name"),
         "getCMSFields_forPopup"
      );

      $fields->addFieldToTab("Root.Content.Courses", $course_fields);
      $fields->addFieldToTab("Root.Content.Categories", $course_category_fields);
      
      return $fields;
   }

}

class CoursesPage_Controller extends StandardPage_Controller {

   public function init() {
      parent::init();
      Requirements::themedCSS('courses');
   }
   
}
?>

The front-end errors out with the following when I try to call Courses:

Error at sapphire/core/model/MySQLDatabase.php line 525: Couldn't run query:
SELECT "Course"."ClassName", "Course"."Created", "Course"."LastEdited", "Course"."Name", "Course"."Number", "Course"."Description", "Course"."Active", "Course"."CategoryID", "Course"."ID", CASE WHEN "Course"."ClassName" IS NOT NULL THEN "Course"."ClassName" ELSE 'Course' END AS "RecordClassName"
FROM "Course"
WHERE ("ParentID" = '11')

Unknown column 'ParentID' in 'where clause' ({SITE_URL}/courses)

At one point I actually dropped Course and CourseCategory from the Database because they were giving me so many problems. Now I feel like that missing field is a product of myself messing with the database. I'm hoping there's an easy solution to this or I've overlooked something.

Many thanks!

Avatar
SuperStromboli

25 May 2011 at 3:42am Community Member, 3 Posts

Figured it out. I missed the fact that a $has_many relationship requires a $has_one on the related class.