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.

Data Model Questions /

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

No ParentID Relation after inserting new Items with GridField. Please help


Go to End


4 Posts   1440 Views

Avatar
flywalker

Community Member, 4 Posts

14 November 2013 at 9:06pm

Edited: 14/11/2013 9:08pm

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

Avatar
flywalker

Community Member, 4 Posts

15 November 2013 at 6:46am

nobody knows the solution? :(

Avatar
flywalker

Community Member, 4 Posts

15 November 2013 at 8:12pm

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

Avatar
flywalker

Community Member, 4 Posts

17 November 2013 at 7:51am

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