From 0991477e5a07e80b80840dba428e2761d887dd17 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sun, 31 Aug 2014 15:55:51 +1200 Subject: [PATCH] Add support for directly saving into a File object saveInto() assumed you were either saving into a hasOne or if you wanted to create a new object. This made it impossible to have a FileField on an object that you wanted to upload to. This adds a check so that if the local object is a file, save to that. --- forms/FileField.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/forms/FileField.php b/forms/FileField.php index c9a302425..9294371be 100644 --- a/forms/FileField.php +++ b/forms/FileField.php @@ -99,29 +99,41 @@ class FileField extends FormField { } public function saveInto(DataObjectInterface $record) { - if(!isset($_FILES[$this->name])) return false; - $fileClass = File::get_class_for_file_extension(pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION)); + if(!isset($_FILES[$this->name])) { + return false; + } + + $fileClass = File::get_class_for_file_extension( + pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION) + ); if($this->relationAutoSetting) { // assume that the file is connected via a has-one $hasOnes = $record->has_one($this->name); // try to create a file matching the relation $file = (is_string($hasOnes)) ? Object::create($hasOnes) : new $fileClass(); + } else if($record instanceof File) { + $file = $record; } else { $file = new $fileClass(); } $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->getFolderName()); - if($this->upload->isError()) return false; - $file = $this->upload->getFile(); + if($this->upload->isError()) { + return false; + } if($this->relationAutoSetting) { - if(!$hasOnes) return false; + if(!$hasOnes) { + return false; + } + + $file = $this->upload->getFile(); - // save to record $record->{$this->name . 'ID'} = $file->ID; } + return $this; }