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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Displaying/Editing Many-to-Many Extra Fields

Go to End

14 Posts   11943 Views

Ben Gribaudo

Community Member, 181 Posts

17 June 2009 at 7:13am


How can I display and allow editing of $many_many_extraFields when using a control like the ManyManyComplexTableField?

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 many_many_extraFields property.

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?


Ben Gribaudo

Community Member, 181 Posts

20 June 2009 at 4:30am

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


Forum Moderator, 922 Posts

20 June 2009 at 1:21pm

Edited: 20/06/2009 1:29pm

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.


Ben Gribaudo

Community Member, 181 Posts

23 June 2009 at 12:15am

Thanks for this info, Sean.


Community Member, 5 Posts

17 December 2009 at 6:06pm

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


Community Member, 89 Posts

20 January 2010 at 4:53pm

Edited: 20/01/2010 4:53pm

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

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?



Community Member, 176 Posts

25 March 2010 at 10:22pm

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?


Community Member, 21 Posts

15 September 2010 at 6:06am

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