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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

How to choose a list of members to put into "groups"?


Reply

5 Posts   1327 Views

Avatar
silk

6 January 2010 at 8:45am Community Member, 18 Posts

Hello,

this really is a dumb question, but for some reason I cannot figure out how to get it to work.
My problem:

My domain provides several majordomo lists. Those have to be configured by mail. Now I want to write something to do it with a module: For each mailing list I want to build up a list of Members to keep track who is part of it. For adding members I want to have a dropdown list of Members to choose from. Sounds like a easy task for the DataObjectManager.

Unfortunatly I don't get it to work. Right now I have this:

class MailinglistePage extends Page {
static $db = array(
'Description'=>'Text',
'EMail'=>'Text'
);
static $has_one = array('Admin'=>'Member'
);

static $many_many = array('MailUser'=>'MailinglistenUser'
);

public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $f->addFieldToTab("Root.Content.Mitglieder", new DataObjectManager(
         $this,
         'MailUser',
         'MailinglistenUser',
         array('User' => 'User'),
         'getCMSFields_forPopup'
      ));
      return $f;
   }

}

I have added a new class to wrap the Member:

class MailinglistenUser extends DataObject
{
   static $db = array (
   );
   
   static $has_one = array (
      'User' => 'Member'
   );
   
   static $belongs_many_many = array(
    'Mailingliste' => 'MailinglistePage'
   );
      
   public function getCMSFields_forPopup()
   {
    $members = DataObject::get("Member");
    $memberList = array();
    foreach($members as $member){
    $memberList[$member->ID] = $member->getName();
    }   
    return new FieldSet(
    new DropdownField('User', 'User', $memberList)
);
   }
}

With this, the cms looks good. Everything looks ready to add new Members to the Mailinglist. Unfortunatly it does not work. When I select a new member it is not inserted correctly and does not show up in the list. This is not surprising as the table MailinglistePage_MailUser stays empty. Only thing what happens is, that table MailinglistenUser each time gets a new entry with default UserID 0.
My question: Do I approach the problem in a sensible way?
If yes: What am I doing wrong?
If no: Any suggestions how to do it better?

Avatar
UncleCheese

6 January 2010 at 1:05pm 4085 Posts

Without getting too far into it, the first thing I notice is that you're trying to manage a many_many relation with a DataObjectManager object. You need to use ManyManyDataObjectManager.

Make that change and let me know how you do.

Avatar
silk

6 January 2010 at 11:33pm Community Member, 18 Posts

Thank's for helping me.

I have followed your instructions. Additionally I have simplified my code to some extend:

class MailinglistenUser extends DataObject
{
   static $db = array (
    'Name'=>'Text',
    'Vorname'=>'Text',
    'Email'=>'Text'
   );
   
   static $has_one = array (
   
   );
   
   static $belongs_many_many = array(
    'Mailingliste' => 'MailinglistePage'
   );
      
   public function getCMSFields_forPopup()
   {
   
return new FieldSet(
new TextField('Name', 'Name'),
new TextField('Vorname', 'Vorname'),
new EmailField('Email', 'Email'));
   }
}

class MailinglistePage extends Page {
static $db = array(
'Description'=>'Text',
'EMail'=>'Text'
);
static $has_one = array('Admin'=>'Member'
);

static $many_many = array('MailUser'=>'MailinglistenUser'
);

public function getCMSFields()
   {
      $f = parent::getCMSFields();
      $f->addFieldToTab("Root.Content.Mitglieder", new ManyManyDataObjectManager(
         $this,
         'MailUser',
         'MailinglistenUser',
         array('Name' => 'Name',
         'Vorname'=>'Vorname',
         'Email'=>'Email'         
         ),
         'getCMSFields_forPopup'
      ));
      return $f;
   }
}

Now I can add MailinglistenUser and they show up in the cms. They are showing up in *all* MailinglistPages, though.
In the database the new User is created in the table MailinglistenUser, but the table MailinglistePage_MailUser is empty.

Avatar
UncleCheese

7 January 2010 at 2:54am 4085 Posts

Yeah, the way a MMDOM works is that every table will give you a global view of all the records, and you check off which ones are associated with the page using the checkbox in the right column.

Avatar
silk

7 January 2010 at 4:04am Community Member, 18 Posts

Oh, it already worked.. :-)
Thank's!