Jump to:

2011 Posts in 1436 Topics by 620 members

Form Questions

SilverStripe Forums » Form Questions » Possible bugfix many_many relations management via ModelAdmin

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

Page: 1
Go to End
Author Topic: 354 Views
  • esakrielaart
    Avatar
    Community Member
    56 Posts

    Possible bugfix many_many relations management via ModelAdmin Link to this post

    Hello all,

    I think there might be an error in the sapphire/forms/ManyManyComplexTableField.php, which results in a many-many relation to only be managed from the sourceclass containing the static $many_many and not the sourceclass containing the static $belongs_many_many. In the mentioned file I changed (okay, I did not changed but copied the code in there to a new file outside the core folders) named FixedManyManyComplexTableField.php the following:

    Old:

    $belongsManyManyRelations = $singleton->uninherited( 'belongs_many_many', true );
    if( isset( $belongsManyManyRelations ) && array_key_exists( $this->name, $belongsManyManyRelations ) ) {
       $this->manyManyParentClass = $class;
       $manyManyTable = $belongsManyManyRelations[$this->name] . '_' . $this->name;
       break;
    }

    New

    $belongsManyManyRelations = $singleton->uninherited( 'belongs_many_many', true );
    if( isset( $belongsManyManyRelations ) && array_key_exists( $this->name, $belongsManyManyRelations ) ) {
       $this->manyManyParentClass = $class;
       $manyManyTable = $this->name."_".$class;
       break;
    }

    Now I am not sure if this change is a result of to short thinking but I just feel to share this as it frustrated me for a while now. Please let me know if you developers can comfirm this change sounds reasonable.

    The used test case was simple, just two classes (SideA and SideB) extending from DataObject and linking to eachother using many_many and belongs_many_many. Then both getCMSFields functions were changed accordingly:

    (This is for class SideA, change all B to A for class SideB)

    public function getCMSFields() {
       $fields = parent::getCMSFields();
          
       $extraFields = new FixedManyManyComplexTableField(
          $this,
          'SideB',
          'SideB'
       );
          
       $fields->replaceField(
          'SideB',
          $extraFields
       );
       return $fields;
    }

    Hope this might help some more people out there.

    Friendly regards,
    Maurice

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Possible bugfix many_many relations management via ModelAdmin Link to this post

    Hi Maurice,

    The place to raise / discuss this would be on the google dev list or raise it on open.silverstripe.org with a patch.

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