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

SS3 has_many relations


Go to End


1203 Views

Avatar
zenmonkey

Community Member, 545 Posts

25 July 2012 at 3:45am

I'm having a little trouble understanding how the new GridField replaces some of the old interfaces. I'm trying to manage a $has_many relation on a page but when I create a new record using the GridField it doesn't automatically create the relation (just shows a drop down)

My DataObject Looks like this

class FAQ extends DataObject {

	

	static $db = array(
		'Question' => 'Varchar',
		'Answer' => 'HTMLText'
	);
	
	static $has_one = array (
		'FaqPage' => 'FaqPage'
	);
	
	static $summary_fields = array(
		'Question' => 'Question',
		'Answer' => 'Answer'
	);	

	
}

and the page looks like
class FaqPage extends Page {

	/**
	 * Static vars
	 * ----------------------------------*/
	
	static $description = 'Holds Frequently Asked Questions';



	/**
	 * Data model
	 * ----------------------------------*/

	static $has_may = array (
		'FAQs' => 'FAQ'
	);		


	/**
	 * Common methods
	 * ----------------------------------*/

	 public function getCMSFields() {
		 
		 $fields = parent::getCMSFields();
		 
		 $FAQ = FAQ::get()->filter(array('FaqPageID' => $this->ID));
		 
		 $FAQField = new GridField("FAQs", "Frequently Asked Question", $FAQ, GridFieldConfig_RelationEditor::create());
		 
		
		 
		 $fields->addFieldToTab("Root.FAQs",$FAQField);
		 
		 return $fields;
		 
	 }

	
}

Not sure what I'm missing and the documentation doesn't appear to be clear

Thanks