Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Is there any example code of ModelAdmin and $belongs_many_many

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1847 Views
  • robinp
    Avatar
    Community Member
    33 Posts

    Is there any example code of ModelAdmin and $belongs_many_many Link to this post

    Hi,

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

    Cheers

    Robin

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: Is there any example code of ModelAdmin and $belongs_many_many Link to this post

    I just started playing around with SilverStripe myself.
    But this page from the manual was usefull to me.
    Hope it helps.

  • robinp
    Avatar
    Community Member
    33 Posts

    Re: Is there any example code of ModelAdmin and $belongs_many_many Link to this post

    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

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Is there any example code of ModelAdmin and $belongs_many_many Link to this post

    @robinp
    any chance of sharing some examples?

  • geyer
    Avatar
    Community Member
    5 Posts

    Re: Is there any example code of ModelAdmin and $belongs_many_many Link to this post

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

  • robinp
    Avatar
    Community Member
    33 Posts

    Re: Is there any example code of ModelAdmin and $belongs_many_many Link to this post

    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

    1847 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.