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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Displaying/Editing Many-to-Many Extra Fields


Go to End
Reply

14 Posts   7060 Views

Avatar
Ben Gribaudo

17 June 2009 at 7:13am Community Member, 181 Posts

Hello,

How can I display and allow editing of $many_many_extraFields when using a control like the [url=http://api.silverstripe.org/forms/fields-relational/ManyManyComplexTableField.html]ManyManyComplexTableField[/url]?

Background
When doing a many-to-many relationship, SilverStripe allows data to be stored in the junction table. This is set up using DataObject's static [url=http://api.silverstripe.org/sapphire/model/DataObject.html#$many_many_extraFields]many_many_extraFields property[/url].

The specific problem I'm trying to solve involves Quotes which may be related to any number of Qualities. Some of these relationships should be marked as primary. I've tried to do this using

   public static $many_many_extraFields = array(
      'Qualities' => array('Primary' => 'Boolean')
   );


but would like the CMS admin user to have a way to edit this property.

Any ideas?

Sincerely,
Ben

Avatar
Ben Gribaudo

20 June 2009 at 4:30am Community Member, 181 Posts

I'm wondering if this is an uncommon thing to do in SS. I'm curious...how many of you out there have used $many_many_extraFields?

Avatar
Sean

20 June 2009 at 1:21pm (Last edited: 20 June 2009 1:29pm), Forum Moderator, 921 Posts

AFAIK, there are currently no CRUD interfaces for many-to-many extra fields.

At the moment there's a way to add the extra data by setting the second parameter to the add() method when you're manipulating the relationship. Here's an example of adding a Member to a Group.

$member = new Member();
$group = DataObject::get_one('Group', "Code = 'forum-members'");

$group->Members()->add($member, array(
   'Position' => 'Player'
));

There's also a way to inspect which extra fields are available for a relation using the method many_many_extraFields($component) (found on DataObject) where $component is the relationship name that you want the extra fields spec for.

Unfortunately, in the end you'll have to do your own code to add or edit extra fields columns on the junction table.

There is a plan to rework ComplexTableField so that it works in a more robust way, this would most likely include scaffolding the extra fields if you're editing a many-to-many relationship.

Sean

Avatar
Ben Gribaudo

23 June 2009 at 12:15am Community Member, 181 Posts

Thanks for this info, Sean.

Avatar
Bongorak

17 December 2009 at 6:06pm Community Member, 5 Posts

Is it still the case that there is no CRUD interface for many_many_extraFields?

Avatar
MateuszU

20 January 2010 at 4:53pm (Last edited: 20 January 2010 4:53pm), Community Member, 89 Posts

What Sean remarked allows to modify, add and remove rows from the join table, which was enough for me.

$cs = $group->getManyManyComponents('Member'); // returns ComponentSet
$cs->add($otherObj, array('FieldOnJoinTable'=>1)); // adds a row
$cs->add($otherObj, array('FieldOnJoinTable'=>2)); // modifies a row
$cs->remove($otherObj);

When comes to querying though, this is a bit tricky, you need to use $join parameters of DataObject, or other getters. There are also other methods on ComponentSet like removeAll, setByIDList and so on, have a look at the ComponentSet. What else do you need?

m.

Avatar
MarijnKampf

25 March 2010 at 10:22pm Community Member, 164 Posts

I would be interested in a CRUD interface for extra fields.

I want to define a many-many relation with three additional fields: one enumeration and two HTMLtext fields. Is there an easy way to enable these to be edited through the admin interface?

Avatar
jhirm

15 September 2010 at 6:06am Community Member, 21 Posts

Yeah, this would be great. Any progress on this?

I actually want to use the data in the many_many_extraFields, not just filter my results based on it. This means that using a regular old $join on the DataObject won't work, since it doesn't actually join the tables. Had to resort to the old SQLQuery object. Am I missing something?

Go to Top