diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index e2cac70cd..3b174fde0 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -1,5 +1,6 @@ vars['imageWidth'] = $this->imageWidth; } + protected function getColumn() + { + $model = $this->model; + $columnName = $this->columnName; + $relations = Str::evalHtmlArray($columnName); + $last_relation = array_pop($relations); + + while ( $relations ) + { + $relation = array_shift($relations); + $model = $model->$relation; + } + return $model->$last_relation(); + } + protected function getFileList() { - $columnName = $this->columnName; - $list = $this->model->$columnName()->withDeferred($this->sessionKey)->orderBy('sort_order')->get(); + $list = $this->getColumn()->withDeferred($this->sessionKey)->orderBy('sort_order')->get(); /* * Set the thumb for each file @@ -97,8 +112,7 @@ class FileUpload extends FormWidgetBase public function onRemoveAttachment() { if (($file_id = post('file_id')) && ($file = File::find($file_id))) { - $columnName = $this->columnName; - $this->model->$columnName()->remove($file, $this->sessionKey); + $this->getColumn()->remove($file, $this->sessionKey); } } @@ -189,8 +203,7 @@ class FileUpload extends FormWidgetBase if (!$uploadedFile->isValid()) throw new SystemException('File is not valid'); - $columnName = $this->columnName; - $fileRelation = $this->model->$columnName(); + $fileRelation = $this->getColumn(); $file = new File(); $file->data = $uploadedFile;