Zoo
扩展程序,其中我有一个带有photo
字段的Animal
模型和一个带有典型CRUD操作的前端(FE)插件。 photo
字段是典型的FAL的FileReference
类型,在后端(BE)中与常见的TCA IRRE配置完美配合使用。我能够成功地将文件上传到存储中,在文件列表模块中可见,并且在我的动物编辑期间可以在BE中使用它,但是我无法在我的FE插件中创建
FileReference
。我目前的方法看起来像这样:
/**
* @param \Zoo\Zoo\Domain\Model\Animal $animal
*/
public function updateAction(\Zoo\Zoo\Domain\Model\Animal $animal) {
// It reads proper uploaded `photo` from form's $_FILES
$file = $this->getFromFILES('tx_zoo_animal', 'photo');
if ($file && is_array($file) && $file['error'] == 0) {
/** @type $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
$storageRepository = GeneralUtility::makeInstance('\TYPO3\CMS\Core\Resource\StorageRepository');
$storage = $storageRepository->findByUid(5); // TODO: make target storage configurable
// This adds uploaded file to the storage perfectly
$fileObject = $storage->addFile($file['tmp_name'], $storage->getRootLevelFolder(), $file['name']);
// Here I stuck... below line doesn't work (throws Exception no. 1 :/)
// It's 'cause $fileObject is type of FileInterface and FileReference is required
$animal->addPhoto($fileObject);
}
$this->animalRepository->update($animal);
$this->redirect('list');
}
无论如何,试图通过这行代码创建引用都会抛出异常:
$animal->addPhoto($fileObject);
我该如何解决这个问题?
检查过了:DataHandler
方法的途径(链接)也行不通,因为它对前端用户不可用。
简述
如何从已有的FAL记录中添加FileReference
到Animal
模型中?
uploadAction
中的代码不同,它使用通用类型转换器将上传的文件(或文件)转换为对象,直接在创建Animal
模型时使用。在前端和后端都能完美运行。 - derhansen