From c30b34e4d94560a7590104dbce20cabac7262285 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 31 Oct 2015 16:40:16 +1100 Subject: [PATCH] In special cases, has one/many will require a foreign key set For example: to pass any constraints imposed by the database This new logic emulates the "create" method on the relation object Fixes #1541 --- modules/backend/behaviors/RelationController.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index a2813d876..de09b21fe 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -913,6 +913,19 @@ class RelationController extends ControllerBehavior if ($this->viewMode == 'multi') { $newModel = $this->relationModel; + + /* + * In special cases, has one/many will require a foreign key set + * to pass any constraints imposed by the database. This emulates + * the "create" method on the relation object. + */ + if (in_array($this->relationType, ['hasOne', 'hasMany'])) { + $newModel->setAttribute( + $this->relationObject->getPlainForeignKey(), + $this->relationObject->getParentKey() + ); + } + $modelsToSave = $this->prepareModelsToSave($newModel, $saveData); foreach ($modelsToSave as $modelToSave) { $modelToSave->save(null, $this->manageWidget->getSessionKey());