【发布时间】:2021-12-27 16:08:18
【问题描述】:
我正在创建一个插件FileManager,其中所有上传都存储在一个表中。这个插件有一个AttachmentBehavior 附加了一个hasMany 关联。
我在 Articles/add.php 和 Articles/edit.php 模板中使用多文件输入来上传将链接到文章的文件:
// Example in Articles/edit.php
echo $this->Form->create($article, ['type' => 'file']);
echo $this->Form->control('title', /*[...]*/);
echo $this->Form->control('body', /*[...]*/);
echo $this->Form->control('pieces_jointes', ['type' => 'file', 'multiple' => true, 'name' => 'pieces_jointes[]']);
我可以用文件添加新文章,没问题。
我可以编辑没有文件的文章添加文件,没有问题。
但是当我编辑已经有文件的文章以添加更多文件时,出现错误“无法使用 Laminas\Diactoros\UploadedFile 类型的对象作为数组”
修补实体Article 时会出现此错误。
这是我的控制器:
// in ArticlesController.php
public function edit($id)
{
$article = $this->Articles->findById($id)->firstOrFail();
if ($this->request->is(['post', 'put'])) {
debug($article); // $article->pieces_jointes is an array of entities of my files table.
debug($this->request->getData()); // $this->request->getData()->pieces_jointes is an array of UplaodedFile objects
$article = $this->Articles->patchEntity($article, $this->request->getData()); // The error occurs here
if ($this->Articles->save($article)) {
return $this->redirect(/*[...]*/);
}
}
$this->set(compact('item'));
}
我不太清楚发生了什么。 有谁可以解释我并帮助我解决这个问题?
【问题讨论】:
标签: php cakephp-4.x