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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

$many_many_extraFields


Go to End


6 Posts   2921 Views

Avatar
Gene

Community Member, 41 Posts

4 October 2010 at 6:49pm

Hi,

I'm trying to figure out a way to provide a UI for editing one of my $many_many_extraFields. I have two classes: CenterPage and TeamMember. They are setup as follows...

class CenterPage extends Page {

    static $belongs_many_many = array(
        'TeamMembers' => 'TeamMember',
    );

}

TeamMember extends Page {

    static $many_many = array(
        'Centers' => 'CenterPage',
    );
	
    static $many_many_extraFields = array(
        'Centers' => array(
            'Type' => "Enum('Teacher, Supervisor, Director', 'Teacher')",
        ),
    );

    function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Content.Centers', $centers = new ManyManyDataObjectManager(
            $this,
            'Centers',
            'CenterPage',
            array(
                'Title' => 'Title',
                'Type' => 'Type'
            )
        ));
        $centers->setPermissions(array());
        $centers->setPluralTitle('Centers');
		
        return $fields;
    }
}

So it would be great to have a dropdown to select the TeamMember Type. The first thing that came to mind was using a Custom DOM Action (popup) with a dropdownfield. I'm having a tough time wrapping my head around implementing it. Any help would be appreciated.

Avatar
181920

Community Member, 8 Posts

13 October 2010 at 10:46am

Hey Gene, did you have any luck with this?

Avatar
Gene

Community Member, 41 Posts

13 October 2010 at 10:58am

Yeah I did, but I definitely went for the fastest solution over the cleanest.

What I did was return a dropdown with the choices for the extra field in one of the columns in the DOM. Then I just included some javascript that attached to the 'change' event of the dropdown and made an ajax call to save the changes. The main downside being that you had to make sure the record was selected and saved first before you could edit the many_many_extraFields column. Other than that it works fine for now. When I have some time I might properly subclass the DOM.

Let me know if you want to see any code.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

13 October 2010 at 1:57pm

The way you do it is to pass the DOM to your getCMSFields() function..

public function getCMSFields($dom) {
$parent_page_id = $dom->controller->ID
}

And then you can run a query based on that ID and set the values of your form fields as needed.

Then, in your onAfterWrite() method, you can inspect the request and update the many_many join table as needed. It's a little kludgy, but not impossible.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
Gene

Community Member, 41 Posts

19 October 2010 at 12:03pm

Perfect, thanks for pointing me in the right direction. That's definitely a less kludgy than what I was doing before.

Avatar
Garrett

Community Member, 245 Posts

12 March 2011 at 3:48am

Hey there Gene,

I've got a similar issue. I need a description field for each many-many relationship I've created, and a way to edit them.

I’ve got is a DataObject called a BusinessNeed. A BusinessNeed just has a Name, really. There are about 15 of these.

On the other side of things I’ve got page type called a ProductPage. This has Content, links, a file download, etc. There are about 5 of these, but more could be added. A ProductPage can be associated with any number of BusinessNeeds, and by dint of that, vice versa. This part I know how to do, I just put a tab on the ProductPage page type that shows the many-many list with checkboxes to associate the ProductPage with the different BusinessNeeds and I have my relationship(s). What I don't have is a GUI.

I saw @unclecheese's response, but I'm afraid this whole thread isn't showing me enough. I don't know what it means to "pass the DOM to your getCMSFields() function". Any way you can show me some code??

Thanks,
Garrett