['System\Mode-6ren">
gpt4 book ai didi

php - 上传未保存的文件时为 "Trying to get property of non-object"

转载 作者:可可西里 更新时间:2023-11-01 00:29:20 25 4
gpt4 key购买 nike

我有一个模型具有使用 OctoberCMS 的 system_files 的文件关系。

public $attachOne = [
'return_file' => ['System\Models\File', 'public' => false, 'delete' => true]
];

在 fields.yaml 中我有表格

   return_file:
label: Attach File
type: fileupload
mode: file
span: right

现在,在保存之前或之后,我想将图像从其目录移动到我的插件中的自定义目录。 afterSave() 似乎没有检索文件路径来移动它。

但是在 system_files 中我看到在 MySQL workbench 中实际上已经上传了它。 enter image description here

然而,当我在后端点击保存时,我得到“试图获取非对象的属性”

这是 afterSave() 函数中的内容。

public function afterSave()

{

$custom_path = plugins_path() . '/acme/request/uploads/';
$file = $this->return_file->getPath();
$move_file = $file->move($custom_path);

}

是否可以在后台上传并在保存之前/之后移动文件?

最佳答案

问题是文件在 afterSave() 中还不完全存在,它仍然作为延迟绑定(bind)存在。试试这个:

$this->return_file()->withDeferred($this->sessionKey)->first()->getPath();

关于php - 上传未保存的文件时为 "Trying to get property of non-object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44204824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com