ModelAdmin relation field not saving

Silverstripe Version: 4*

I’m trying to create a simple Newsletter in the Admin CMS area. Basically I need a simple form with the additional option to select the groups from group (DB). I added the Newsletter data object and added the relation. When adding a new newsletter I can select the groups, but they are not saved in the relational table. I have been reading the documentation but I can’t find the correct solution.

NewsLetterAdminUI.php

class NewsLetterAdminUI extends ModelAdmin {
    private static $managed_models = [
        NewsLetter::class
];
    private static $url_segment = 'newsletter';
    private static $menu_title = 'Newsletter';
    private static $menu_icon_class = 'font-icon-news';

NewsLetter.php

class NewsLetter extends DataObject
{
    private static $db = [
        'Subject' => 'Varchar',
        'Content' => 'Text',
        'SentDate' => 'Datetime'
    ];

    private static $many_many  = [
        'Group' => Group::class,
    ];

    private static $summary_fields = [
        'ID',
        'Subject',
        'Content',
        'SentDate',
        'Group.ID' // I want to list the groups that received the email
    ];

/* last non working attempt */
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->insertAfter('Naslov',   new TagField(
            'Groups',
            _t(__CLASS__ . '.Groups', 'Groups'),
            Group::get()
        ));
        $fields->removeByName('Root.Edit.Groups'); // trying to remove the Group tab in the edit form
        return $fields;
    }

Additionally: is there an option “on save”, “on before save”,… where I could add a simple send email function?

You have an onBeforeWrite() and onAfterWrite() method available.

With regard to the saving issue. It could be a simple typo. Your relation is named Group but your tagfield is using Groups as the field name.

1 Like

Hi thnx for help. I checked and tried singular/plural. No change. I can select the groups, but they don’t get saved. Do I need to set something else in the relation? Should I extend the Group object and define the relation from other side too? And perhaps the function that concats the selected groups for the summary view?

Sry I had a second typo. The group is saving now. :slight_smile:
I still need to represent the groups in summay table.

Thank you. It works nicely :slight_smile:
There is only one small additional task. I need to hide the “Group” tab when editing the form.
Is there a simple explanation or a manual on how the “paths” to tabs and fields work?

removeByName(root.main.how.are.these.referenced)

You can take advantage of a bit of Silverstripe magic here. If you create a new function in your Newsletter class, along the lines of this:

public function getGroupNames()
{
  $groups = $this->Group()->column('Title');
  return implode(', ', $groups);
}

Then you should be able to just update your summary_fields declaration to:

    private static $summary_fields = [
        'ID',
        'Subject',
        'Content',
        'SentDate',
        'GroupNames'
    ];

Run a dev/build and hopefully it’ll all be good.

(Note: that’s all entirely untested, off the top of my head… so you may need to fix some of it!)

1 Like

If you’re just looking to remove a tab that’s been automatically added, you should be able to just use the relation name, eg:

$fields->removeByName('Group');

Just for info, if you need to remove multiple, you can also pass in an array:

$fields->removeByName(['Group', 'SomeOtherField']);

Thank you. It removes the tab.
Is there something in the documentation where I could learn the “root.main…” ? What root means? What main means? And if there are any others?