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.

Data Model Questions /

Is there any example code of ModelAdmin and $belongs_many_many


Reply


6 Posts   1901 Views

Avatar
robinp

Community Member, 33 Posts

6 March 2009 at 8:02pm

Hi,

Just wondering if there is any example code of working with ModelAdmin and $belongs_many_many relationships.

Cheers

Robin

Avatar
Mad_Clog

Community Member, 78 Posts

13 March 2009 at 9:34pm

I just started playing around with SilverStripe myself.
But [url=http://doc.silverstripe.com/doku.php?id=recipes:many_many-example]this[/url] page from the manual was usefull to me.
Hope it helps.

Avatar
robinp

Community Member, 33 Posts

16 March 2009 at 9:03pm

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

Avatar
NickJacobs

Community Member, 144 Posts

17 March 2009 at 6:56pm

@robinp
any chance of sharing some examples?

Avatar
geyer

Community Member, 5 Posts

17 March 2009 at 9:04pm

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..

Avatar
robinp

Community Member, 33 Posts

18 March 2009 at 6:31pm

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