"Cannot use object of type Laminas\Diactoros\UploadedFile as array" 在 Cakephp 4 中使用多上传输入
"Cannot use object of type Laminas\Diactoros\UploadedFile as array" with multi-upload input in Cakephp 4
我正在创建一个插件 FileManager
,其中所有上传都存储在一个 table 中。这个插件有一个 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[]']);
我可以用文件添加新文章,没问题。
我可以编辑一篇没有文件的文章来添加文件,没问题。
但是当我编辑一篇已有文件的文章以添加更多文件时,出现错误“无法使用类型[=39=的对象” ] 作为数组
修补实体 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'));
}
我不太清楚发生了什么。
有没有人可以解释我并帮助我解决这个问题?
删除 AttachmentBehavior::beforeMarshal
中的关联文件模型似乎修复了错误:
// in AttachmentBehavior
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
foreach ($this->_fields as $field => $value) {
// Remove associated file Model (PiecesJointes, ...) in $options['associated']
$options['associated'] = collection($options['associated'])
->reject(function ($modelAssociated, $key) use ($value) {
return $modelAssociated == $value['alias'];
})
->toArray();
// [...]
}
}
但我需要确认我是对的 (?)
您不应该为上传字段和关联使用相同的名称 属性,这会在框架的各个地方发生冲突。
重命名表单中的字段等,使其使用既不匹配任何关联 属性 也不匹配任何列名称的名称,然后让您的行为和内容使用“外部”处理输入"名称,将输入转换为保存数据所需的结构后,将其转换为"内部"名称。
我正在创建一个插件 FileManager
,其中所有上传都存储在一个 table 中。这个插件有一个 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[]']);
我可以用文件添加新文章,没问题。
我可以编辑一篇没有文件的文章来添加文件,没问题。
但是当我编辑一篇已有文件的文章以添加更多文件时,出现错误“无法使用类型[=39=的对象” ] 作为数组
修补实体 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'));
}
我不太清楚发生了什么。 有没有人可以解释我并帮助我解决这个问题?
删除 AttachmentBehavior::beforeMarshal
中的关联文件模型似乎修复了错误:
// in AttachmentBehavior
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
foreach ($this->_fields as $field => $value) {
// Remove associated file Model (PiecesJointes, ...) in $options['associated']
$options['associated'] = collection($options['associated'])
->reject(function ($modelAssociated, $key) use ($value) {
return $modelAssociated == $value['alias'];
})
->toArray();
// [...]
}
}
但我需要确认我是对的 (?)
您不应该为上传字段和关联使用相同的名称 属性,这会在框架的各个地方发生冲突。
重命名表单中的字段等,使其使用既不匹配任何关联 属性 也不匹配任何列名称的名称,然后让您的行为和内容使用“外部”处理输入"名称,将输入转换为保存数据所需的结构后,将其转换为"内部"名称。