Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Missing ParentID Field During Get

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

Page: 1
Go to End
Author Topic: 948 Views
  • SuperStromboli
    Avatar
    Community Member
    3 Posts

    Missing ParentID Field During Get Link to this post

    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!

  • SuperStromboli
    Avatar
    Community Member
    3 Posts

    Re: Missing ParentID Field During Get Link to this post

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

    948 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.