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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

many_many relationship (between "Task" and "Member") with editing in two places


Go to End


823 Views

Avatar
jr_dev

Community Member, 2 Posts

29 November 2016 at 9:03am

Edited: 29/11/2016 9:14am

Hi everyone

I've recently been working on understanding SilverStripe but I've encountered an issue I couldn't solve so far.

It basically involves 3 classes:

  • Task
  • Member Extension
  • MemberTask

The idea is to be able to have an editable list of tasks through TaskAdmin. When creating/editing a task through the TaskAdmin it should not only be possible to change name/description etc, but also to assign/remove them to/from a Member. (which is working)

The problem is that I also want to be able to see (and edit) the assigned Tasks when looking at a Member. This doesn't work and I'm not sure why.

My Task DataObject in its simplest form:

class Task extends DataObject
{

    private static $db = array (
        'Name' => 'Varchar',
        'Description' => 'Text',
    );

    private static $has_one = array (
        'TaskImage' => 'Image'
    );

    private static $many_many = array (
        'Members' => 'Member'
    );
}

So far so good.

MyMemberExtension and TaskMember is where I don't seem to get it working:

MyMemberExtension:

<?php

class MyMemberExtension extends DataExtension {

    private static $many_many = array (
        'Tasks' => 'Task'
    );

    public function updateCMSFields(FieldList $fields) {

        $source = Task::get()->map('ID','Title')->toArray();

        $fields->addFieldsToTab('Root.Main', array(
            TextField::create('Name'),
            TextField::create('Description'),
            ListBoxField::create('Tasks', 'Tasks')->setMultiple(true)->setSource($source)
        ));

    }
}

}

MemberTask:

class MemberTask extends DataObject
{

    private static $has_one = array (
        'Member' => 'Member',
        'Task' => 'Task',

    );

    private static $belongs_many_many = array (
        'Members' => 'Member',
        'Task' => 'Task',

    );

}

Anyone has an idea what I'm doing wrong? My suspicion is that I messed up MemberTask and the updateCMSFields in the Member Extension.