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 /

ModelAdmin - add an existing DataObject in a Many_Many


Reply


3 Posts   2635 Views

Avatar
alirobe

Community Member, 35 Posts

10 July 2009 at 9:58pm

Edited: 11/07/2009 12:31am

edit: I figured this out. For those interested, I installed [url=http://silverstripe.org/dataobjectmanager-module/]DataObjectManager[/url], 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)

Avatar
rainerh

Community Member, 23 Posts

24 November 2009 at 7:28am

Hi,

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

Greetings!

Avatar
timcole

Community Member, 32 Posts

23 December 2009 at 10:55am

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.