This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

billyZduke
billyZduke

I was able to extend the User plugin (using the additional fields in the user table, as opposed to the userplus profile model) and the User Account component. I've got my front end registration form working and new Users are being inserted into the db upon submit. The problem is, two of the fields I've added are file type fields for image uploads (of documentation required for registration on my site).

I am using a non-Ajax reg form submit, and I am able to retrieve the uploaded file data for both fields. When I check the server, the files themselves have been written to directories under /storage/app/uploads/public, and I can see the images in a browser if I go to the public URL. However, I cannot seem to attach these images to the User record that gets created, which seems like it should be fairly simple, but between dozens of Google results for both October & Laravel, I've tried a number of different ways to do this that so far have not worked.

The files, though they do end up saved to the server,never get an entry in the system_files table. The attachOne relationships exist in the User object (along with the original Rainlab.User's avatar field), and I can see all three fields accounted for when I dump the User object (their names, not the relation values).

In my custom Account component, I'm creating a new \System\Models\File and taking the Input::file() data and running the fromPost() method on each image. Am I on the right track here? Seems like I've been banging at this for weeks from every direction...

Here's the test I'm attempting to run in my custom Account component, which, when I finally have a successful version, I'll move over to the onRegister function (and have onRun call that when post() is not empty):

class Account extends UserAccount
{
    protected $uploadedFiles = [];

    ...

    public function onRun()
    {
        parent::onRun();
        ...
        $this->uploadedFiles = Input::file();
        foreach ($this->uploadedFiles as $fileField => $uploadedFileObject):
            $fo = new FileObject();
            $this->uploadedFiles[$fileField] = $fo->fromPost($uploadedFileObject);
        endforeach;

        $newUser = User::where('email','anEmail@ThatIsDefinitelyIn.TheDatabase')->first();
        foreach ($this->uploadedFiles as $fileField => $uploadedFile):
            $uploadedFile->save();
            $newUser->setRelationValue($fileField,$uploadedFile);
        endforeach;
        $newUser->save();
    }
    ...
}

Last updated

billyZduke
billyZduke

Still experimenting. Realizing I should have put this in Plug Development channel but can't move it now... So, if I use the following instead of setRelationValue(), I DO get entries for the uploaded files in the system_files table:

            $this->sessionKey = Input::get('_session_key');
            ...
            $newUser->$fileField()->withDeferred($this->sessionKey)->latest()->first();

The problem now becomes retrieval of the correct user model. If I move the latter foreach() loop into the onRegister function, and attempt to access the user created by Auth::register() or find the user by $data['email'], it doesn't work anymore. It will still create system_files rows, but most of the field information will be NULL.

It seems like there should be a way to do this where I don't actually have to create a 2nd user object, but onRegister() doesn't even use one until it needs to send an activation email...

Last updated

billyZduke
billyZduke

Okay, looks like I was being overzealous and trying too many things at once. This is how I finally got it to work:

First, in my custom Account component's onRun() method:

    if (!empty(post())):
        $this->uploadedFiles = Input::file();

        foreach ($this->uploadedFiles as $fileField => $uploadedFileObject):
            $fo = new FileObject();
            $this->uploadedFiles[$fileField] = $fo->fromPost($uploadedFileObject);
            // $this->uploadedFiles[$fileField]->is_public = false; // Tried this in the hopes of achieving security for these uploaded images , and though setting this here does indeed work in terms of setting the is_public field in system_files table to zero, it also keeps admins from seeing the uploaded images in the backend, which defeats the entire purpose of uploading them in the first place
        endforeach;

        $this->onRegister();
    endif;

Then in my onRegister() method:

        $user = Auth::register($data, $automaticActivation);

        foreach ($this->uploadedFiles as $fileField => $uploadedFile):
            // The following switch is used to add title data to the uploaded image, taken from other user-entered fields related to each image
            switch ($fileField):
                case 'id_photo':
                    $uploadedFile->title = $user->id_number;
                    break;
                case 'rec_photo':
                    $uploadedFile->title = $user->rec_number;
                    break;
            endswitch;
            $user->$fileField()->add($uploadedFile); // The simple add() method was the one that worked in the end, because I'd already retrieved all the uploaded file info from Input::file(), no session key needed
        endforeach;
        $user->save();

So the only problem left for me is that the "attachment_type" that gets saved with the system_file record is the type for my extended model as opposed to RainLab\User\Models\User, which, once again, prevents the attachments from appearing with the correct user records in the backend admin. So, off to fix that now...

Hoping someone else can benefit from my public investigations here. There seem to be an awful lot of what I'd consider obvious things that I've been trying to do on this project (my first built in OctoberCMS, but not my first with Laravel), that I seem to be the first person ever to try to do...

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.