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

Data Model to Multiple Pages


Go to End


2 Posts   1206 Views

Avatar
Beedubsss

Community Member, 7 Posts

22 February 2016 at 10:02pm

Hi

I'm fairly new to Silverstripe and was wondering how Silverstripe manages Data Objects that can below to multiple pages. Essentially what I want to create for example is have a Project Data Model that is managed through the main tab of the cms. I would then like to show some of these projects on different pages. I was wonder how to create some sort of Data picker on pages that allows me to select some of the projects on each page? Obviously it is a has_many relationship to the page but what is someone able to point me in the right direction to allow cms users to pick the projects on each page? e.g. what cms field types do I need.

Cheers

Avatar
Devlin

Community Member, 344 Posts

22 February 2016 at 10:57pm

Edited: 22/02/2016 10:59pm

https://docs.silverstripe.org/en/3.2/tutorials/dataobject_relationship_management/

Let's assume you have:

class Page extends SiteTree {
	private static $many_many = array(
		'Projects' => 'Project'
	);
	public function getCMSFields() {
		// ..
		// Project Relation Editor
		$projectsField = new GridField(
			'Projects',
			'Projects',
			$this->Projects(),
			GridFieldConfig_RelationEditor::create()
		);
		$fields->addFieldToTab('Root.Projects', $projectsField);
		return $fields;
    }
}

class Project extends DataObject {
	private static $db = array(
		'Title' => 'Varchar',
	);
	private static $belongs_many_many = array(
		'Pages' => 'Page'
	);
}

You'll find that the Project Page has a new Tab "Projects" in the CMS, a text field named "Find Projects by Title" and a button named "Link existing."

The idea is to search for the project's name, select it in the popup list and then to click the button. After that, your project should be displayed in the list below and you have created a relation to that DataObject (and the DataObject to that page).