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.

Customising the CMS /

SOLVED - SS3 Page has_many Dataobjects - relation lost


Reply


3 Posts   1678 Views

Avatar
monkee

Community Member, 21 Posts

20 September 2012 at 11:45pm

Edited: 21/09/2012 12:27am

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)
      );
   }
   

}

Avatar
copernican

Community Member, 189 Posts

21 September 2012 at 12:12am

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.

Avatar
monkee

Community Member, 21 Posts

21 September 2012 at 12:20am

Edited: 21/09/2012 12:26am

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