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

chris10207
chris10207

Everything is in the title, i m looking to self backup a model by serializing it, saving it as a file and then attaching the file to the model itself. here is my code, but it is not working on the line $file->save() =>

"The file "1459" does not exist" on line 41 of /Users/johndoeishere/Sites/toto.titi/site/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/File/File.php

the code:

$result = Company::find($this->id)->first()->toJson();
$filepath = File::put( temp_path() . '/' . $this->uen . '.json', json_encode($result));
$file = new System\Models\File;
$file = $file->fromFile( $filepath );
$file->save(); 
$this->backup_file()->add($file);

Last updated

Vojta Svoboda
Vojta Svoboda

Try to dump $filepath before save and check write access to this folder. Then you can dump $file before save, if it's really file or no.

On second line - why do you use json_encode? Result is already JSON.

chris10207
chris10207

thanks for you help. yes the file $filepath is well written on the disk, i can see it and open it and contains the expected json content.

ok i managed to fix the code like this :

            $filename = uniqid('oc');
            $output = $company->toJson();
            $filepath = temp_path() . '/' . $this->uen . '.json';
            File::put( $filepath, $output);

            $file = new System\Models\File;
            $file->data = $filepath;
            $file->save();
            $this->backup_file()->add($file);

hope that helps somebody else later

1-3 of 3

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