Merge pull request #9194 from sminnee/manymany-add-joinobject

FIX: Set many-many-through joinRecord on newly added records.
This commit is contained in:
Loz Calver 2020-07-02 09:30:53 +01:00 committed by GitHub
commit ceeba9f1ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -217,6 +217,11 @@ class ManyManyThroughList extends RelationList
$record = $hasManyList->createDataObject($extraFields ?: []);
$record->$localKey = $itemID;
$hasManyList->add($record);
// Link the join object to the $item, as if it were queried from within this list
if ($item instanceof DataObject) {
$item->setJoin($record, $this->manipulator->getJoinAlias());
}
}
/**

View File

@ -6,6 +6,7 @@ use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ManyManyThroughList;
use SilverStripe\ORM\Tests\ManyManyThroughListTest\Item;
use SilverStripe\ORM\Tests\ManyManyThroughListTest\PolyItem;
use SilverStripe\ORM\Tests\ManyManyThroughListTest\PolyJoinObject;
use SilverStripe\ORM\Tests\ManyManyThroughListTest\Locale;
@ -75,6 +76,14 @@ class ManyManyThroughListTest extends SapphireTest
$this->assertNotNull($item2->getJoin());
$this->assertEquals('join 2', $item2->getJoin()->Title);
$this->assertEquals('join 2', $item2->ManyManyThroughListTest_JoinObject->Title);
// Check that the join record is set for new records added
$item3 = new Item;
$this->assertNull($item3->getJoin());
$parent->Items()->add($item3);
$expectedJoinObject = ManyManyThroughListTest\JoinObject::get()->filter(['ParentID' => $parent->ID, 'ChildID' => $item3->ID ])->first();
$this->assertEquals($expectedJoinObject->ID, $item3->getJoin()->ID);
$this->assertEquals(get_class($expectedJoinObject), get_class($item3->getJoin()));
}
/**