afterSave() gives error trying to get property of non-object in octoberCMS

試著忘記壹切 提交于 2019-12-10 23:23:34

问题


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);
    }

回答1:


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.



来源:https://stackoverflow.com/questions/51924175/aftersave-gives-error-trying-to-get-property-of-non-object-in-octobercms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!