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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Nested DataObjects Gridfield not saving the Page ID


Go to End


2 Posts   862 Views

Avatar
Praveen

Community Member, 49 Posts

20 February 2015 at 7:26pm

Edited: 20/02/2015 7:52pm

I have a nested Dataobjects to create Accordion and Accordion Tabs
I created a Accordion first and saved it and is working fine.
Now I add a new Accordion Tab to the Accordion it save without the AccordionPageID.

Accordion Page

class AccordionPage extends Page
{

	private static $has_many = array(
		"Accordions" => "Accordion",
		"AccordionTabs" => "AccordionTab",
	);
	private static $has_one = array(
		'RootFolder' => 'Folder'
	);

public function getCMSFields()
	{

		$fields = parent::getCMSFields();	

		//$fields->removeFieldFromTab('Root.Main', 'Content');

		$fields->addFieldToTab('Root', $accordion = new Tab('Accordions'));
		$accordion->setTitle(_t('AccordionPage.ACCORDION', 'Accodrions'));
		if ($rootFolder = $this->RootFolder()) {
			$accordionConfig = GridFieldConfig_RecordEditor::create();

			if(class_exists('GridFieldBulkManager')) {
				$accordionConfig->addComponent(new GridFieldBulkManager());
			}

			if(class_exists('GridFieldSortableRows')) {
				$accordionConfig->addComponent(new GridFieldSortableRows('SortOrder'));
			}
			$accordionField = new GridField('Accordions', 'Accordions', $this->Accordions(), $accordionConfig);
			$fields->addFieldToTab("Root.Accordions", $accordionField);
		} else {
			$fields->addFieldToTab(
				"Root.Accordions",
				new HeaderField(
					_t("AccordionPage.ACCORDIONNOTSAVED", "You may add accordions once you have saved the page for the first time."),
					$headingLevel = "3"
				)
			);
		}
		return $fields;
	}
}

Accordion DataObject

class Accordion extends DataObject
{

	private static $has_one = array(
		'AccordionPage' => 'AccordionPage',
		'AccordionFolder' => 'Folder'
	);

	private static $has_many = array(
		"AccordionTabs" => "AccordionTab",
	);
	function getCMSFields()
	{
		$fields = new FieldList(new TabSet('Root'));
		$fields->addFieldsToTab('Root.Main', array(
			new TextField('Name', _t('AccordionPage.ACCORDIONNAME','Accordion Name'), null, 255),
			new TextareaField('Description', _t('AccordionPage.DESCRIPTION','Description'))
		));
		if ($accordionFolder = $this->AccordionFolder()) {
			$accordionConfig = GridFieldConfig_RecordEditor::create();

			if(class_exists('GridFieldBulkManager')) {
				$accordionConfig->addComponent(new GridFieldBulkManager());
			}

			if(class_exists('GridFieldSortableRows')) {
				$accordionConfig->addComponent(new GridFieldSortableRows('SortOrder'));
			}
			$accordionField = new GridField('AccordionTabs', 'Tabs', $this->AccordionTabs(), $accordionConfig);
			$fields->addFieldToTab("Root.AccordionTabs", $accordionField);
		} else {
			$fields->addFieldToTab(
				"Root.AccordionTabs",
				new HeaderField(
					_t("AccordionPage.ACCORDIONTABNOTSAVED", "You may add tabs once you have saved the page for the first time."),
					$headingLevel = "3"
				)
			);
		}
		return $fields;
	}

AccordionTab DataObject

class AccordionTab extends DataObject
{
	private static $db = array(
		'TabName' => 'Varchar(255)',
		'Description' => 'Text',
		'SortOrder' => 'Int',
		'URLSegment' => 'Varchar(255)',
		'showContent' => 'Boolean',
		'TabContent' => 'HTMLText'
	);

	private static $has_one = array(
		'AccordionPage' => 'AccordionPage',
		'Accordion' => 'Accordion',
		'Folder' => 'Folder'
	);

	static $summary_fields = array(
		'TabName' => 'Title',
		'Description' => 'Description'
	);
	function getCMSFields()
	{
		$fields = new FieldList(new TabSet('Root'));
		$fields->addFieldsToTab('Root.Main', array(
			new TextField('TabName', _t('AccordionPage.TABNAME','Tab Title'), null, 255),
			new TextareaField('Description', _t('AccordionPage.DESCRIPTION','Description'))
		));
		return $fields;
	}

When I Add accordion Tab it does not save AccordionPage ID . it only saves the AccordionID.

Avatar
Praveen

Community Member, 49 Posts

26 February 2015 at 5:57pm

Edited: 26/02/2015 5:58pm

I used onBeforeWrite to popuplate the PageID

	public function onBeforeWrite() {
		parent::onBeforeWrite();
		$Accordion = $this->Accordion();
		$this->AccordionPageID = $Accordion->AccordionPageID;

	}