5099 Posts in 1519 Topics by 1116 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 573 Views |
-
SOLVED - SS3 Page has_many Dataobjects - relation lost

20 September 2012 at 11:45pm Last edited: 21 September 2012 12:27am
Hello
I have a "MonkeeBlogPage" wich has_Many "MonkeeBlogEntries"
"MonkeeBolgEntry" has_one MonkeeBlogPageNow 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)
);
}
}
-
Re: SOLVED - SS3 Page has_many Dataobjects - relation lost

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.
-
Re: SOLVED - SS3 Page has_many Dataobjects - relation lost

21 September 2012 at 12:20am Last edited: 21 September 2012 12:26am
IOTI - Thanks a LOT! You are definitely my Hero for today...
... actually i triedbut was missing the $this at the end of new GridField... Damn - so close ;-)$this->MonkeeBlogEntries()
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/SortableGridFieldSincerly
Monkee
| 573 Views | ||
|
Page:
1
|
Go to Top |

