Hi,
Just wondering if there is any example code of working with ModelAdmin and $belongs_many_many relationships.
Cheers
Robin
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
Hi,
Just wondering if there is any example code of working with ModelAdmin and $belongs_many_many relationships.
Cheers
Robin
I just started playing around with SilverStripe myself.
But this page from the manual was usefull to me.
Hope it helps.
Hi
Thank you reply. I think the problems I was having where more with ModelAdmin . What I found I had to do is remove the automatically generated tab's and rebuild the forms.
ModelAdmin is amazing for prototyping. What I'm finding is most of the forms that are automatically generated need to reworked in the long run so my hack is working OK for me on multiple levels.
Cheers
Robin
@robinp
any chance of sharing some examples?
Hi guys..
I got this working on v2.3. It's an example of products which could have many categories:
// ---->
class Category extends DataObject {
static $belongs_many_many = array(
'Products' => 'Product'
// ...
);
// ---->
class Product extends DataObject {
static $many_many = array(
'Categories' => 'Category'
);
function getCMSFields() {
$fields = parent::getCMSFields();
// add categories tab..
$categoryTablefield = new ManyManyComplexTableField(
$this,
'Categories',
'Category',
array( 'Name' => 'Name' )
);
$categoryTablefield->setAddTitle( 'A Category' );
$fields->addFieldToTab( 'Root.Categories', $categoryTablefield );
return $fields;
}
// ...
}
// ---->
class MyCRM extends ModelAdmin {
protected static $managed_models = array(
'Category',
'Product',
);
static $url_segment = 'crm';
static $menu_title = 'My CRM';
}
// ---->
Hope that helps..
Hi Geyer,
Thanks for post the code that is basically what I've found myself doing. .
The only thing I'm doing differently is removing the tab that SilverStrip makes after the first save. With line like
$fields->removeByName('Categories');
So the start of my getCMSFields() is like this
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('Categories');
// add categories tab..
.....
}
Cheers
Robin