Please I really need some advice to implement this. Don’t even need the code… I just need some ideas.
When user is creating a new record, I don’t know the id of this to link the uploaded files. How can I link the the uploaded files to this record? I think maybe I can upload the files and store the files IDs on a session and then after user saves the record I’m able to link them. But I don’t think this is right. Can someone help me? Have anyone done this before?
Hi @KonApaz, actually I know I can use the id after save the record on controller but since the user will upload the files on the create form before saves it, I just can’t use the ID.
I was thinking to let the user complete the form, and if him need to attach files I call save method on the upload action and then link the id to files.
about timeout of session I mentioned that because in this case the files will haven’t any record!
The unique Id you suggested, I thing, has the same problem with sessions. How to generate a unigue Id when the user will going to create two records after of uploaded files? all the files will be attached to the first saved record,
please explain your solution, maybe I didn’t understand something
I generate UUID in model’s afterConstruct and assign it to parent record’s id (yes, my PKs are not integer).
When the files are being uploaded, I already know the id to attach to.
And the ‘main’ record is saved with that predefined id, of course.
So no matter how many forms user has opened, all they will be saved with different ids, and the uploaded files also will be saved with different parent ids.
Example (pseudocode):
$record = new MyModel; // here ID is already assigned in afterconstruct
echo $record->id; // result is UUID
if (is_uploaded_file(...)) {
$image = new MyModelImage;
$image->parentId = $record->id;
$image->save();
}
I think you got the idea.
Pros:
you can display uploaded files even if record’s validation has failed
no need to deal with session (which is very unreliable)
almost bulletproof
…
Cons:
possible orphan files (daily cleanup required)
chance of collision (very small)
small degrade in performance (integer PKs are faster)