Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » $many_many_extraFields

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1662 Views
  • Gene
    Avatar
    Community Member
    41 Posts

    $many_many_extraFields Link to this post

    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.

  • 181920
    Avatar
    Community Member
    8 Posts

    Re: $many_many_extraFields Link to this post

    Hey Gene, did you have any luck with this?

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: $many_many_extraFields Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: $many_many_extraFields Link to this post

    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

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: $many_many_extraFields Link to this post

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

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: $many_many_extraFields Link to this post

    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

    1662 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.