Jump to:

22978 Posts in 11505 Topics by 2825 members

General Questions

SilverStripe Forums » General Questions » Displaying/Editing Many-to-Many Extra Fields

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

Page: 1 2
Go to End
Author Topic: 6448 Views
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Displaying/Editing Many-to-Many Extra Fields Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    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?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    Thanks for this info, Sean.

  • Bongorak
    Avatar
    Community Member
    5 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

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

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    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.

  • MarijnKampf
    Avatar
    Community Member
    161 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    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?

  • jhirm
    Avatar
    Community Member
    21 Posts

    Re: Displaying/Editing Many-to-Many Extra Fields Link to this post

    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?

    6448 Views
Page: 1 2
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.