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.

Customising the CMS /

SilverStripe Book: CRM-Customisation mixed with ManyManyComplexTableField


Reply


3 Posts   939 Views

Avatar
david_b

Community Member, 3 Posts

6 May 2011 at 5:14am

Hello hello,

I'm right now extending the ModelAdmin based upon how it is written in the SilverStripe book. Now I included a ManyManyComplexTableField as well and so far everything works just fine.

But I still have a few questions to my code. You can find my 3 Files here:
NewsletterAdmin.php http://pastebin.com/tkgWQdgE

<?php
class NewsletterAdmin extends ModelAdmin {
   static $managed_models = array(
      'NewsletterContact',
      'NewsletterGroup',
   );
   
   static $url_segment = 'newsletter';
}
?>

NewsletterContact.php http://pastebin.com/0FzfQqJm

<?php
class NewsletterContact extends Member {
   static $singular_name = 'Kontakt';
   static $plural_name = 'Kontakte';
   
   static $db = array(
      'Gender' => 'Enum("männlich, weiblich")',
      'NameTitle' => 'Varchar(100)'
   );
   
   static $many_many = array(
      'NewsletterGroups' => 'NewsletterGroup',
   );
   
   static $field_labels = array(
      'Gender' => 'Geschlecht',
      'NameTitle' => 'Anrede',
   );
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->removeByName('Permissions');
      $fields->removeByName('Groups');
      
      $fields->removeByName('Password');
      $fields->removeByName('Locale');
      $fields->removeByName('DateFormat');
      $fields->removeByName('TimeFormat');
      $fields->removeByName('MemberUserDetailsHeader');
      

      $newsletterGroupTablefield = new ManyManyComplexTableField(
         $this,
         'NewsletterGroups',
         'NewsletterGroup',
         array(
            'Title' => 'Title'
         ),
         'getCMSFields'
      );
      
      $fields->addFieldToTab( 'Root.NewsletterGroups', $newsletterGroupTablefield );
      
      return $fields;
   }
}
?>

NewsletterGroup.php http://pastebin.com/N3kBhkZt

<?php
class NewsletterGroup extends DataObject {
   static $singular_name = 'NewsletterGruppe';
   static $plural_name = 'NewsletterGruppen';

   static $db = array(
      'Title' => 'Varchar(255)',
   );
   static $belongs_many_many = array(
      'NewsletterContacts' => 'NewsletterContact',
   );
   
   static $field_labels = array(
      'Title' => 'Titel',
   );
   
function getCMSFields() {
$fields = new FieldSet();
$fields->push( new TextField( 'Title' ) );
return $fields;
}
}
?>

My Questions:
If I create a new NewsletterContact it first only says "Add" in the lower right corner and only if I hit "Add" I get the 3 new buttons "Back", "Delete" and "Save". This wouldn't really matter to me, but on hitting "Add" it only saves me the Name and the Title of the NewsletterContacts but not the NewsletterGroup in the ManyManyComplexTableField. Is this normal behaviour or is there a way, that I can already save all the data on the first click?

And my second question: On first glance, am I making any serious mistakes, which one should never ever make?

Thanks in advance
Cheers,
David

Avatar
swaiba

Forum Moderator, 1805 Posts

10 May 2011 at 8:05pm

Is this normal behaviour or is there a way, that I can already save all the data on the first click?

I guess so, if we were to edit one of the child objects we wouldn't expect it to save the parent would we? If you want to change the save behaviour you can use onBeforeWrite/onAfterWrite to do so.

And my second question: On first glance, am I making any serious mistakes, which one should never ever make?
Not that I can see

Avatar
david_b

Community Member, 3 Posts

11 May 2011 at 7:50am

Thanks for your answer.