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