17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2623 Views |
-
Relation many to many between pages

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-exampleUnfortunately 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 -
Re: Relation many to many between pages

16 February 2008 at 12:17pm Last edited: 16 February 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 %> -
Re: Relation many to many between pages

18 February 2008 at 11:14pm
or even simpler: use $Applications and $Products directly in your templates,
and spare youself the Show*() functions ;)
| 2623 Views | ||
|
Page:
1
|
Go to Top |


