From 422a99770c32f912e13ed2b4744c6f16a76e1c32 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 9 Mar 2018 23:47:16 -0600 Subject: [PATCH] Attach uploaded files directly to parent if exists If the parent of the file attachment relationship already exists, then attach the file directly to it in this request instead of going through deferred bindings. Will continue to attach files through deferred binding if the parent relation doesn't exist. --- modules/backend/formwidgets/FileUpload.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 1e16dd382..cef13c9c2 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -394,7 +394,15 @@ class FileUpload extends FormWidgetBase $file->is_public = $fileRelation->isPublic(); $file->save(); - $fileRelation->add($file, $this->sessionKey); + /** + * Attach directly to the parent model if it exists, + * else attach via deferred binding + */ + if (@$fileRelation->getParent()->exists) { + $fileRelation->add($file); + } else { + $fileRelation->add($file, $this->sessionKey); + } $file = $this->decorateFileAttributes($file);