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.

Form Questions /

Possible bugfix many_many relations management via ModelAdmin


Reply


2 Posts   393 Views

Avatar
esakrielaart

Community Member, 56 Posts

5 January 2012 at 6:50am

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

Avatar
swaiba

Forum Moderator, 1799 Posts

5 January 2012 at 8:05am

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.