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.

Archive /

Our old forums are still available as a read-only archive.

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

Relation many to many between pages


Go to End


3 Posts   4043 Views

Avatar
pga

Community Member, 8 Posts

13 February 2008 at 8:36am

Hi Everyone,

I have to make relation "many to many" between two kind of pages:
between products and applications pages.

For this I created two files in mysite/code/ folder:

mysite/code/ProductsPage.php file:

class ProductsPage extends Page {
	static $db = array();	
	static $has_one = array();
	static $belongs_many_many = array('Applications' => 'AppsPage');
	function getCMSFields()
	{
		$fields = parent::getCMSFields();	
		$applicationList = DataObject::get('AppsPage');
		$fields->addFieldToTab('Root.Content.Applications', new CheckboxSetField('Applications', '', $applicationList));
		return $fields;
	}
}
class ProductsPage_Controller extends Page_Controller {
	function ShowApps()
	{		
		return DataObject::get('AppsPage', "ParentID = {$this->ID}");
	}
}

mysite/code/AppsPage.php file:

class AppsPage extends Page {
	static $db = array();
	static $has_one = array();
	static $many_many = array('Products' => 'ProductsPage');
	function getCMSFields()
	{
		$fields = parent::getCMSFields();
		$productList = DataObject::get('ProductsPage');
		$fields->addFieldToTab('Root.Content.Products', new CheckboxSetField('Products', '', $productList));
		return $fields;
	}
}
class AppsPage_Controller extends Page_Controller {
	function ShowProducts()
	{
		return DataObject::get('ProductsPage', "ParentID = {$this->ID}");
	}
}

Almost everything above works perfectly. I can select multiple products for application,
and I see my selections in the products settings when I use administration panel.

Question is - how to display list of Products on Application site and list of Applications on
product site. Unfortunately defined by me ShowProducts() and ShowApps() methods doesn't work.

I've based on:
http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management
http://doc.silverstripe.com/doku.php?id=datamodel&s=relation
http://doc.silverstripe.com/doku.php?id=recipes:many_many-example

Unfortunately examples on websites above are little different. There is no examples
of relation many to many between pages, only between pages and some abstract categories.

Could anybody help me how to display data from relation in template?

Thank you,
Pawel

Avatar
blueskies

Community Member, 44 Posts

16 February 2008 at 12:17pm

Edited: 16/02/2008 12:17pm

Try:

function ShowApps(){
	return $this->getManyManyComponents('Applications');
}

function ShowProducts()	{
	return $this->getManyManyComponents('Products');
}

Note the reference to Applications and not AppsPage (from: static $belongs_many_many = array('Applications' => 'AppsPage');)

getManyManyComponents makes the magic happen to get stuff and spit it out to the template. Then you can have a regular control in your template like so:

<% control ShowApps%>
$Title<br />
$Link
<% end_control %>

Avatar
Ingo

Forum Moderator, 801 Posts

18 February 2008 at 11:14pm

or even simpler: use $Applications and $Products directly in your templates,
and spare youself the Show*() functions ;)