Jump to:

5451 Posts in 1673 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SOLVED - SS3 Page has_many Dataobjects - relation lost

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

Page: 1
Go to End
Author Topic: 1206 Views
  • monkee
    Avatar
    Community Member
    20 Posts

    SOLVED - SS3 Page has_many Dataobjects - relation lost Link to this post

    Hello

    I have a "MonkeeBlogPage" wich has_Many "MonkeeBlogEntries"
    "MonkeeBolgEntry" has_one MonkeeBlogPage

    Now in SS2.4, using DataObjectManager the Relation would be saved and added automatically.
    With GridField in SS3 this is not the case.

    Any Solution for that case? If i add a DataObject trought a Gridfield on a Page, it is not very Convenient that i have to choose the belonging Page on every new Dataobject, that should happen automatically. How can i accomplish this?

    <?php
    class MonkeeBlogPage extends Page {

       public static $db = array(
       );

       public static $has_one = array(
       );

       public static $has_many = array(
          'MonkeeBlogEntries' => 'MonkeeBlogEntry',
          'MonkeeBlogCategory' => 'MonkeeBlogCategory'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();

          $gridFieldConfig = GridFieldConfig::create()->addComponents(
              new GridFieldToolbarHeader(),
              new GridFieldAddNewButton('toolbar-header-right'),
              new GridFieldSortableHeader(),
              new GridFieldDataColumns(),
              new GridFieldPaginator(10),
              new GridFieldEditButton(),
              new GridFieldDeleteAction(),
              new GridFieldDetailForm()
           );

          /* Blog Entries */
          $blogGridConfig = $gridFieldConfig;
          $blogGridConfig->addComponent(new GridFieldSortableRows('SortOrder'));

          $blogGridField = new GridField("MonkeeBlogEntries", "Monkee Blog Entries", DataObject::get('MonkeeBlogEntry'), $blogGridConfig,$this);   
          $fields->addFieldToTab("Root.Main", $blogGridField,'Content'); // add the grid field to a tab in the CMS   
          
           $fields->removeByName('Content');

          return $fields;
       }

    }

    and

    <?php
    class MonkeeBlogEntry extends DataObject {
       
       static $db = array(
          'Title' => 'VarChar',
          'Content' => 'HTMLText',
          'Datum' => 'Date',
          'SortOrder' => 'Int'
       );
       
       static $has_one = array(
          'MonkeeBlogPage' => 'MonkeeBlogPage',
          'TitelBild' => 'Image'      
       );

       static $many_many = array(
          'MonkeeBlogCategories' => 'MonkeeBlogCategory'
       );

       static $defaults = array(
          'Datum' => 'now'
       );

       function getCMSFields() {

          $datefield = new DateField('Date');
          $datefield->setConfig('showcalendar','true');

          $gridFieldConfig = GridFieldConfig::create()->addComponents(
              new GridFieldToolbarHeader(),
              new GridFieldAddNewButton('toolbar-header-right'),
              new GridFieldSortableHeader(),
              new GridFieldDataColumns(),
              new GridFieldPaginator(10),
              new GridFieldEditButton(),
              new GridFieldDeleteAction(),
              new GridFieldDetailForm()
        );

          return new FieldList(
             new TextField('Title'),
             new HTMLEditorField('Content'),
             $datefield,
             new ImageField('TitelBild'),
             new GridField("MonkeeBlogCategories", "Kategorien", DataObject::get('MonkeeBlogCategory'), $gridFieldConfig)
          );
       }
       

    }

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: SOLVED - SS3 Page has_many Dataobjects - relation lost Link to this post

    Monkee,

    You shouldn't be using Dataobject::get() calls anymore as they are now deprecated in favor of DataList::create(). But for this particular case there is a really easy way to do this. Instead of...

    $blogGridField = new GridField("MonkeeBlogEntries", "Monkee Blog Entries", DataObject::get('MonkeeBlogEntry'), $blogGridConfig,$this);

    do

    $blogGridField = new GridField("MonkeeBlogEntries", "Monkee Blog Entries", $this->MonkeeBlogEntries(), $blogGridConfig,$this);

    Also, instead of building your own gridFieldConfig (unless there are certain customizations you want to do) its quicker and easier to use GridFieldConfig_RelationEditor::create(), like so.

    $blogGridField = new GridField("MonkeeBlogEntries", "Monkee Blog Entries", $this->MonkeeBlogEntries(), GridFieldConfig_RelationEditor::create(),$this);

    The relationEditor is a "pre made" GridFieldConfig.

  • monkee
    Avatar
    Community Member
    20 Posts

    Re: SOLVED - SS3 Page has_many Dataobjects - relation lost Link to this post

    IOTI - Thanks a LOT! You are definitely my Hero for today...
    ... actually i tried

    $this->MonkeeBlogEntries()

    but was missing the $this at the end of new GridField... Damn - so close ;-)

    Unfortunately i need my own config to add Sorting Capability to the GriedfFeld - but thanks for the tipp, works like a charm!
    --> Update: Just realized:

       $blogGridConfig = GridFieldConfig_RelationEditor::create();
       $blogGridConfig->addComponent(new GridFieldSortableRows('SortOrder'));


    works perfectly!
    Here's the Link to the Sortable GridField Module: https://github.com/UndefinedOffset/SortableGridField

    Sincerly

    Monkee

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