Show related fields in cms with one-to-one relation

silverstripe-4
Tags: #<Tag:0x00007f10ca0b6dc8>

#1

The problem is similar to that one php - SilverStripe display has_one relation values - Stack Overflow

The fields of the one-to-one related object that is connected by ‘belongs_to’ are being shown empty in getCMSFields. It should be noted that the related object is of an extended SilverStripe\Security\Member class. Here are the essential pieces of code:

  1. SilverStripe\Security\Member class extension:
    class AdsMemberExtension extends DataExtension
    {
    private static $db = [
    ‘Nickname’ => ‘Varchar’,
    ];

     private static $has_one = [
         'Account' => DataObject::class,
     ];
    

    }

  2. The main class:
    class AdvertiserAccount extends DataObject
    {

    private static $belongs_to = [
    ‘Member’ => ‘SilverStripe\Security\Member.Account’,
    ];

     public function getCMSFields()
     {
     	$fields = parent::getCMSFields();
     	$fields->addFieldsToTab('Root.General', [
     		TextField::create('Member.Nickname', 'Nickname'),
     	]);
     	...
     }
    

    }

I’m surprised that text field ‘Nickname’ leaves blank although $this->Member->Nickname contains the correct data. Please, tell me what’s wrong?


#2

As far as I know dot notation is for reading rather than writing. You can use it in $summary_fields to display the value of a related object, and you can use dot notation for sorting (in some situations at least) but you can’t edit related DataObjects using dot notation out of the box. It could be a neat feature but might open a can of worms regarding validation etc.

There might be a module out there that lets you have this kind of functionality. From a cursory look this one might do the trick:


#3

Great! It looks quite relevant, thank you so much! I’ll try it and give you a feedback.