Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » No ParentID Relation after inserting new Items with GridField. Please help

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

Page: 1
Go to End
Author Topic: 385 Views
  • flywalker
    Avatar
    Community Member
    4 Posts

    No ParentID Relation after inserting new Items with GridField. Please help Link to this post

    Hello,

    after update to SS 3 I needed to change from ComplexTableField to GridField

    here my code

    class Staff extends DataObject {
       
       public static $db = array(
          'Name' => 'Varchar',
       );
       
       public static $has_one = array(
          'Parent'=>'StaffPage',
       );
       
       public static $summary_fields = array(
             'Name' => 'Name'
       );

    public function getCMSFields() {
          
          $fields = parent::getCMSFields();
          $fields->removeByName(ParentID');
          return $fields;
    }
    }

    class StaffPage extends Page {
       
       public static $db = array();
       
       static $has_one = array();
       
       public static $has_many = array(
          "Staff"=>"Staff"
       );
       
       public function getCMSFields() {      
          $fields = parent::getCMSFields();
          
          $config = GridFieldConfig_RecordEditor::create();
          
          $staffField = new GridField(
                'Staff',
                'Staff',
                $this->Staff(),
                $config
          );
          $fields->addFieldToTab('Root.Staff', $kuechenField);
          return $fields;
       }
       
       public function Staff() {
          return Staff::get()->filter("ParentID", $this->ID)->sort("Name");
       }
       
    }

    class StaffPage_Controller extends Page_Controller {}

    everything works fine (edit, delete, view)
    But I have a problem with adding new staff
    I see the formular, put the data in the formular, do save
    CMS shows the first page in in the site tree (after editing existing staff, it stays on staffpage) seems to be a problem
    Looking in the datatabase I see the new staff but without StaffPage relation. ParentID is 0
    Probebly the first problem depends on this missing relation I dont know
    So I have first to fix the ParentID problem.

    Any ideas how can I do this?

    In the old version I had

    public function getCMSFields() {
          $fields = parent::getCMSFields();
          $table = new ComplexTableField(
             $this,
             "Staff",
             "Staff",
             array("Name"=>"Name"),
             null,
             "Staff.ParentID = {$this->ID}"
          );
          $fields->addFieldToTab("Root.Content.Staff", $table);
          return $fields;
       }

    so i missing "Staff.ParentID = {$this->ID}" with GridField

    Thanks a lot in andvance

  • flywalker
    Avatar
    Community Member
    4 Posts

    Re: No ParentID Relation after inserting new Items with GridField. Please help Link to this post

    nobody knows the solution?

  • flywalker
    Avatar
    Community Member
    4 Posts

    Re: No ParentID Relation after inserting new Items with GridField. Please help Link to this post

    If I am in a wrong topic, please move to the right one. Thanks

  • flywalker
    Avatar
    Community Member
    4 Posts

    Re: No ParentID Relation after inserting new Items with GridField. Please help Link to this post

    I hoped this forum has more active members who gives some support to outher users
    It seems I have to kook after another better comunity if I have some SS questions

    After a lot of googeling i found one solutions that helps
    If somebody need it as well:

    public function getCMSFields() {
    .....

    if(!$this->ParentID) {
    $fields->push(new HiddenField("ParentID", "ParentID", Controller::curr()->CurrentPageID()));
    }
    return $fields;
    }

    I dont know if it is the best one solutions but it works

    385 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.