Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » FormField for editing has_many relationships

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

Page: 1
Go to End
Author Topic: 678 Views
  • jthomerson
    Avatar
    Community Member
    5 Posts

    FormField for editing has_many relationships Link to this post

    I have a data model that basically boils down to:

    • Page has_many Views
    • View has_many PageReferences
    • PageReference has_one Page and has an integer "Sort"

    I've played with a couple of the *ComplexTableField form field options. They are really nice for some things, but not what I need. I need something that will allow management of the "View has_many PageReferences" and "PageReference has_one Page" relationships. At its core, I need something that will:

    • List all of the current values contained in a has_many relationship (list all the page references a view has)
    • Allow re-ordering of those values (pagereferences in this case)
    • Allow deletion of some of the values in the list
    • Allow the addition of a new value by a popup (in this case, a popup that would allow you to select a page from your SiteTree)

    Of course, that's only the View->PageReference relationship. I'll also need a very similar form control for listing the views a page contains and allowing you to add a new one. But, I think if I can solve the problem above, this secondary problem is so similar that it will be no problem.

    I figure that I'll need to create a custom FormField for this. My question is: where should I start? What should it extend? And do you have any documentation on what to do / to avoid when making a FormField?

    I've attached a mockup of roughly what I want this tab to look like.

    Attached Files
  • swaiba
    Avatar
    Forum Moderator
    1784 Posts
  • jthomerson
    Avatar
    Community Member
    5 Posts

    Re: FormField for editing has_many relationships Link to this post

    Very cool. Thanks for the tip. I may need to add some ability to order (probably drag and drop with jQuery), but this gets me leaps and bounds towards what I need!

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: FormField for editing has_many relationships Link to this post

    Very welcome!

    If you add the drag drop reorder with this I would be eternally grateful if you'd report back with your enhancments

  • jthomerson
    Avatar
    Community Member
    5 Posts

    Re: FormField for editing has_many relationships Link to this post

    Actually, it looks like it already support sorting:

    https://github.com/ajshort/silverstripe-itemsetfield/blob/master/docs/en/index.md

    I haven't tried the sorting yet, but this looks very promising!

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: FormField for editing has_many relationships Link to this post

    so it does!

    I'd forgotten that - in that case - if you get a working example please post it asap

    couldn't wait - here is my test for the many many sorting - works great trick is to add a field to the many many relationship...

    <?php

    class ObjectA extends DataObject {
       static $db = array(
          'Name' => 'Varchar',
          'Description' => 'Varchar(255)',
       );

       static $has_one = array(
          'ObjectB' => 'ObjectB'
       );

       static $has_many = array(
          'ObjectC' => 'ObjectC'
       );

       static $many_many = array(
          'ObjectD' => 'ObjectD'
       );

       static $many_many_extraFields = array(
          'ObjectD' => array(
                'Sort' => 'Int'
          )
       );

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

          $fields->replaceField('ObjectB',new HasOnePickerField($this,'ObjectB'));
          $fields->replaceField('ObjectC',new HasManyPickerField($this,'ObjectC'));
          $fields->replaceField('ObjectD',new ManyManyPickerField($this,'ObjectD','',array('Sortable'=>true)));

          return $fields;
       }
    }

    class ObjectB extends DataObject {
       static $db = array(
          'Name' => 'Varchar',
          'Description' => 'Varchar(255)',
       );

       static $has_one = array(
          'ObjectA' => 'ObjectA'
       );
    }

    class ObjectC extends DataObject {
       static $db = array(
          'Name' => 'Varchar',
          'Description' => 'Varchar(255)',
       );
       static $has_one = array(
          'ObjectA' => 'ObjectA'
       );
    }

    class ObjectD extends DataObject {
       static $db = array(
          'Name' => 'Varchar',
          'Description' => 'Varchar(255)',
       );

       static $belongs_many_many = array(
          'ObjectA' => 'ObjectA'
       );
    }

    678 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.