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