From ee179fe668a50c03197d8b345ee8c0aacd42c196 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 9 Apr 2008 11:11:03 +0000 Subject: [PATCH] Merged revisions 47622 via svnmerge from svn://svn.silverstripe.com/silverstripe/modules/sapphire/branches/2.2.0-mesq ........ r47622 | ischommer | 2008-01-04 21:44:39 +1300 (Fri, 04 Jan 2008) | 1 line fixed relation-setting to respect subclasses of File in has_one ........ git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@52399 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/FileField.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/forms/FileField.php b/forms/FileField.php index 391dc26f5..d2df56385 100755 --- a/forms/FileField.php +++ b/forms/FileField.php @@ -42,7 +42,7 @@ class FileField extends FormField { * trigger saving the ID of newly created file into "PlayerImageID" * on the record). * - * @var unknown_type + * @var boolean */ public $relationAutoSetting = true; @@ -103,21 +103,27 @@ class FileField extends FormField { public function saveInto(DataObject $record) { if(!isset($_FILES[$this->name])) return false; + 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 File(); + } else { + $file = new File(); + } + $this->upload->setAllowedExtensions($this->allowedExtensions); $this->upload->setAllowedMaxFileSize($this->allowedMaxFileSize); - $this->upload->load($_FILES[$this->name]); + $this->upload->loadIntoFile($_FILES[$this->name], $file); if($this->upload->isError()) return false; $file = $this->upload->getFile(); if($this->relationAutoSetting) { - $fieldName = $this->name . 'ID'; - // assume that the file is connected via a has-one - $hasOnes = $record->has_one($this->name); if(!$hasOnes) return false; // save to record - $record->$fieldName = $file->ID; + $record->{$this->name . 'ID'} = $file->ID; } }