Save $many_many_extraFields when adding a DataObject to a list

Assuming you have a $many_many relation and SortOrder defined as $many_many_extraFields you can add an item to this ManyManyList and pass the extraFields as an array:

private static $many_many = ['Images' => 'Image];

private static $many_many_extraFields = [
    'Images' => ['SortOrder' => 'Int']

public function doSomething() {
    //do something

    $image = Image::create();

    //save image and stuff


    //add to many_many relation

    $this->Images()->add($image, ['SortOrder' => $currentSort]);

Rate this post

Post your comment


  • Sander 17/10/2017 11:30am (7 years ago)

    Note: Entering an existing $image in the add() method, will update the existing record in the relation table.

RSS feed for comments on this page | RSS feed for all comments