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.

Form Questions /

FormField for editing has_many relationships


Reply


6 Posts   723 Views

Avatar
jthomerson

Community Member, 5 Posts

3 February 2012 at 6:02pm

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
Avatar
swaiba

Forum Moderator, 1799 Posts

3 February 2012 at 10:52pm

Have you tried... https://github.com/ajshort/silverstripe-itemsetfield ?

Avatar
jthomerson

Community Member, 5 Posts

4 February 2012 at 2:24am

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!

Avatar
swaiba

Forum Moderator, 1799 Posts

4 February 2012 at 2:26am

Very welcome!

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

Avatar
jthomerson

Community Member, 5 Posts

4 February 2012 at 2:49am

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!

Avatar
swaiba

Forum Moderator, 1799 Posts

4 February 2012 at 2:59am

Edited: 05/02/2012 4:40am

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'
   );
}