Ad

AfterSave() Gives Error Trying To Get Property Of Non-object In OctoberCMS

- 1 answer

i am trying to get path of uploaded file using fileupload widget and then copying that file in custom folder but when creating new record it gives error trying to get property 'path' of non-object when afterSave() calls.

MODEL:

 public $attachOne = [
        'file' => ['System\Models\File']
    ];

    public function afterSave()
    {
        $path = $this->file->path;
        log::info($path);
    }
Ad

Answer

replace this afterSave method in your model and it will not show the error you are having.

public function afterSave()
{
    $sessionKey = post('_session_key');
    $file = $this->file()->withDeferred($sessionKey)->first();
    if($file){
        log::info($file->getPath());
    }
}

the reason is \System\Models\File is available with deferred after master model is commited its changes.

Let me know if you need more help into that.

Ad
source: stackoverflow.com
Ad