Add field to member table or new table with 1-1 relation

Silverstripe Version:
4.*
Question:
I’m trying to add a new unique integer field to member table. Alternatively a new table with 1-1 relation to member table. I need an extra userid field.

VsMember.php

class VsMember extends Member
{
    private static $db = [
        "vs_member_id" => "Int"
    ];

    /*     private static $has_one = [
        'VsMember' => Member::class,
        Member::class => VsMember::class
    ]; */
    /*     private static $belongs_to = [
        'VsMember' => Member::class.'.ID'
    ]; */
    /*     public function updateCMSFields(FieldList $fields) {
        $member = TextField::create('vs_member_id', 'VS Member', null, 4);
        $fields->addFieldsToTab(
            'Root.Main',
            array($member)
        );
    } */
    public function updateCMSFields(FieldList $fields)
    {
        $fields->addFieldToTab("Root.Main",
            new TextField("VsMember", "VS Member")
        );
    }

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

So this creates a new table and “enum(‘SilverStripe\Security\Member’, ‘VsMember’)” in member.className. I would prefer to have an extra field in my member table.
As for the CMSfields I tried this example and it works. I don’t know how to set it on the member/add and member/edit tab

_config.yml

SilverStripe\Core\Injector\Injector:
  SilverStripe\Security\Member:
    Myvendor:
      class: VsMember

The simplest way to do this is with an extension. Subclassing / replacing the member class can lead to all kinds of headaches, so is best avoided in most cases.

Take a look at the example on this page: https://docs.silverstripe.org/en/4/developer_guides/extending/extensions/

That shows the addition of a DoB field, but it’s the same idea.

2 Likes

Great, thank you.
It’s what I needed.
The only thing that bothers me a bit is:
_config.php

SilverStripe\Security\Member::add_extension(MyMemberExtension::class);

works.
app/_config/app.yml

SilverStripe\Security\Member:
  extensions:
    - MyMemberExtension

or

SilverStripe\Security\Member:
  extensions:
    class: MyMemberExtension

doesn’t work. :confused:

Is your extension class namespaced? If so, you’ll need the fully qualified classname in the yml file, eg:

SilverStripe\Security\Member:
  extensions:
    - MyName\Extension\MyMemberExtension

Also, I think you need to ?flush to regenerate the config manifest whenever the YAML has changed.

1 Like

Yes, indeed. Run a http://example.com/dev/build?flush if you’re adding / changing the yml config

Hi, I use /dev/build?flush :slight_smile:
Now it works with yml. I guess I must have had some wrong tab or whitespace…
Anyway thx for your help.