afterSave()给出错误,试图在octoberCMS中获取非对象的属性 [英] afterSave() gives error trying to get property of non-object in octoberCMS

查看:69
本文介绍了afterSave()给出错误,试图在octoberCMS中获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用fileupload小部件获取上载文件的路径,然后将该文件复制到自定义文件夹中,但是在创建新记录时,它给出错误试图获取非对象的属性路径" afterSave()调用.

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.

型号:

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

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

推荐答案

在模型中替换此afterSave方法,它不会显示您遇到的错误.

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

原因是\ System \ Models \ File可以在提交主模型更改后延期使用.

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.

这篇关于afterSave()给出错误,试图在octoberCMS中获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆