afterSave() 尝试在 octoberCMS 中获取非对象的 属性 时出错

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

我正在尝试使用 fileupload 小部件获取上传文件的路径,然后将该文件复制到自定义文件夹中,但是在创建新记录时出现错误 trying to get 属性 'path' 非对象 当 afterSave() 调用时。

型号:

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

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

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

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

原因是 \System\Models\File 在主模型提交其更改后可以使用 deferred。

如果您需要更多帮助,请告诉我。