Associating Image & File Uploads From Form With Data Object?

Silverstripe Version: 3.6.5

SilverStripe form upload field needs to map uploads to a data object.

_I’ve been struggling with this for several days now. I have a SilverStripe form that is meant to update Project data objects through the front-end of the site. The form has a few bulk upload fields (I’m using this module here: GitHub - unclecheese/silverstripe-dropzone). I have things setup so that, when images and files are uploaded, they go to the proper folder in assets (in this case, it’s assets/projects/[projectid]/[folder name]). However, the uploads are not associated with a Project–any Project. In order to display the currently uploaded files on the front end and to show the files as attached to a Project in the admin, I want to be able to associate uploads to a Project (via ID of the Project or some other relation).

I’ve tried various things, including solutions found in the StackOverflow post: php - SilverStripe 3.6.5 - Associate uploaded files from a front-end SilverStripe form to data object - Stack Overflow

But so far, no luck. I either get errors or the files upload to the correct asset folder but fail to be associated with a Project.

View the StackOverflow post in the link for all the relevant code (it’s more than should be pasted here).

If anyone has any suggestions (solutions, a better method–anything), I’d be grateful because this has been giving me trouble for days now. Thanks in advance!
_

Hiya! The StackOverflow answer is along the right lines - you need to save the data into the relation when you submit the form. Where you have

$project->ProjectSummary = $data['ProjectSummary'];

I think you should add in

$project->AddtionalProjectImages()->addMany($data['MyFile']);

Also I’d change it to MyFiles, because there are multiple :slight_smile:

This assumes that you are either returning the objects or their IDs in the $data array.

Let me know how that goes!

The initial solution

use the relation name as the first argument

should definitely work though. Not sure why it doesn’t. Also, you have unanswered questions on the Stack Overflow issue :slight_smile:

Maybe you need to use the Form::saveInto() method - that is, in your update() method, call something like

$form->saveInto($project);
$project->write();

In place of manually writing the DB entries. Some things to try, anyway