mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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.
This commit is contained in:
parent
4b6a03bb0b
commit
0991477e5a
@ -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;
|
||||
|
||||
if($this->upload->isError()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if($this->relationAutoSetting) {
|
||||
if(!$hasOnes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file = $this->upload->getFile();
|
||||
|
||||
if($this->relationAutoSetting) {
|
||||
if(!$hasOnes) return false;
|
||||
|
||||
// save to record
|
||||
$record->{$this->name . 'ID'} = $file->ID;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user