Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » ModelAdmin - add an existing DataObject in a Many_Many

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

Page: 1
Go to End
Author Topic: 2514 Views
  • alirobe
    Avatar
    Community Member
    35 Posts

    ModelAdmin - add an existing DataObject in a Many_Many Link to this post

    edit: I figured this out. For those interested, I installed DataObjectManager, renamed the folder to 'dataobject_manager', and added this code to the Business.php file

    function getCMSFields() {
          $f = parent::getCMSFields();
          $manager = new ManyManyDataObjectManager(
           $this, // Controller
             'Countries', // Source name
             'Country', // Source class
             array('Region'=>'Region','Name' => 'Name'),
             'getCMSFields_forPopup'      );      
          $f->removeFieldFromTab('Root', 'Countries'); // replace the tab with MMDOM tab
          $f->addFieldToTab('Root.Country', $manager);
          return $f; }

    Kinda new to this whole modeladmin thing, so excuse the probably daft question, but it seems fairly obvious that in a many_many relationship I'd want to be able to add an existing DataObject to my relationship, and I'm wondering how to implement this. Here's my current implementation (removed pointless stuff):

    ContactsManager.php

    class ContactsManager extends ModelAdmin {
       public static $managed_models = array(Business','Country');
       static $url_segment = 'contact-us';
       static $menu_title = 'Contact Us';
    }

    Country.php

    class Country extends DataObject {
       static $db = array('Name' => 'Varchar');
       static $belongs_many_many = array ('Businesses' => 'Business');
    }

    Business.php

    class Business extends DataObject {   
       static $db = array('Title' => 'Varchar');
       static $has_one = array('Logo' => 'Image');
       static $many_many = array('Countries'=>'Country');   
    }

    Below is what I get when I edit a country (when I want to add an existing business)

  • rainerh
    Avatar
    Community Member
    23 Posts

    Re: ModelAdmin - add an existing DataObject in a Many_Many Link to this post

    Hi,

    does there already exist a solution for this kind of problem? I would need it urgently....

    Greetings!

  • timcole
    Avatar
    Community Member
    32 Posts

    Re: ModelAdmin - add an existing DataObject in a Many_Many Link to this post

    I had exactly the same issue with many_many relationships in ModelAdmin. Seems that there is a bug basically in ModelAdmin. In case you didn't notice, Ali's edit (at the top of the first post) is a fix that works great.

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