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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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   14761 Views

Avatar
Ben Gribaudo

Community Member, 181 Posts

17 June 2009 at 7:13am

Hello,

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

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 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?

Sincerely,
Ben

Avatar
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 curious...how many of you out there have used $many_many_extraFields?

Avatar
Sean

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.

Sean

Avatar
Ben Gribaudo

Community Member, 181 Posts

23 June 2009 at 12:15am

Thanks for this info, Sean.

Avatar
Bongorak

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?

Avatar
MateuszU

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
$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

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?

Avatar
jhirm

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