Skip to content

Commit

Permalink
FIX: Fix insertion of new records in many-many-through list.
Browse files Browse the repository at this point in the history
This change is specially important when using dot-syntax fieldnames to
access the join record of a many-many-through.
  • Loading branch information
Sam Minnee committed Aug 26, 2019
1 parent d357479 commit 0b37e97
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/GridFieldAddNewInlineButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\ManyManyThroughList;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use Exception;
Expand Down Expand Up @@ -189,9 +190,15 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
foreach ($value[self::POST_KEY] as $fields) {
/** @var DataObject $item */
$item = $class::create();

// Add the item before the form is loaded so that the join-object is available
if ($list instanceof ManyManyThroughList) {
$list->add($item);
}

$extra = array();

$form = $editable->getForm($grid, $record);
$form = $editable->getForm($grid, $item);
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
$form->saveInto($item);

Expand All @@ -205,8 +212,12 @@ public function handleSave(GridField $grid, DataObjectInterface $record)
$extra = array_intersect_key($form->getData(), (array) $list->getExtraFields());
}

$item->write();
$list->add($item, $extra);
$item->write(false, false, false, true);

// Add non-through lists after the write. many_many_extraFields are added there too
if (!($list instanceof ManyManyThroughList)) {
$list->add($item, $extra);
}
}
}
}

0 comments on commit 0b37e97

Please sign in to comment.