File upload TYPO3 6.2-如何在前端(FE)中创建文件引用?
我有一个假设的File upload TYPO3 6.2-如何在前端(FE)中创建文件引用?,file-upload,typo3,extbase,typo3-6.2.x,fal,File Upload,Typo3,Extbase,Typo3 6.2.x,Fal,我有一个假设的Zoo扩展,其中我有Animal模型,带有photo字段和带有典型CRUD动作的前端(FE)插件照片字段是典型的FAL的文件引用,它在后端(BE)中与常见的TCA IRE配置完美结合 我能够成功地将文件上传到存储器,它在文件列表模块中可见,并且我可以在编辑动物时使用它,无论如何,我无法在FE插件中创建文件引用 我目前的做法如下: /** * @param \Zoo\Zoo\Domain\Model\Animal $animal */ public function update
Zoo
扩展,其中我有Animal
模型,带有photo
字段和带有典型CRUD动作的前端(FE)插件<代码>照片字段是典型的FAL的文件引用
,它在后端(BE)中与常见的TCA IRE配置完美结合
我能够成功地将文件上传到存储器,它在文件列表模块中可见,并且我可以在编辑动物时使用它,无论如何,我无法在FE插件中创建文件引用
我目前的做法如下:
/**
* @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
approach()也不起作用,因为FE用户无法使用它
TL;DR
如何从现有(刚刚创建的)FAL记录中将
FileReference
添加到Animal
模型中?您需要做几件事。这就是我得到信息的地方,一些东西从@derhansen已经评论过的地方被拿走了
我不完全确定这是否是你所需要的一切,所以请随意添加一些东西。这不使用类型转换器,您可能应该这样做。这将带来更多的可能性,例如,很容易实现文件引用的删除和替换
您需要:
- 从文件对象创建FAL文件引用对象。这可以使用FALs资源工厂完成
- 将其包装在
(方法\TYPO3\CMS\Extbase\Domain\Model\FileReference
)->setOriginalResource
- 编辑:从TYPO3 6.2.11和7.2开始,此步骤是不必要的,您可以直接使用类
\TYPO3\CMS\Extbase\Domain\Model\FileReference
但是,由于extbase模型在6.2.10rc1中遗漏了一个字段(config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
),因此这将不起作用。您需要继承extbase模型,添加该字段并填充它。别忘了在TypoScript中添加映射,将您自己的模型映射到$uidLocal
sys\u file\u reference
该类将如下所示(摘自forge版本):config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
- 将其添加到config部分的image字段的TCA中(当然要适应您的表和字段名):
- 编辑:如果是在TYPO3 6.2.11或7.2或更高版本上,请在此步骤中使用
因此,在末尾添加创建的\TYPO3\CMS\Extbase\Domain\Model\FileReference
而不是$fileRef
$fileObject
$fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference'); $fileRef->setOriginalResource($fileObject); $animal->addPhoto($fileRef);
- 不要告诉任何人你做了什么
/**
* Function to upload file and create file reference
*
* @var array $fileData
* @var mixed $obj foreing model object
*
* @return void
*/
private function uploadAndCreateFileReference($fileData, $obj) {
$storageUid = 2;
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
//Adding file to storage
$storage = $resourceFactory->getStorageObject($storageUid);
if (!is_object($storage)) {
$storage = $resourceFactory->getDefaultStorage();
}
$file = $storage->addFile(
$fileData['tmp_name'],
$storage->getRootLevelFolder(),
$fileData['name']
);
//Creating file reference
$newId = uniqid('NEW_');
$data = [];
$data['sys_file_reference'][$newId] = [
'table_local' => 'sys_file',
'uid_local' => $file->getUid(),
'tablenames' => 'tx_imageupload_domain_model_upload', //foreign table name
'uid_foreign' => $obj->getUid(),
'fieldname' => 'image', //field name of foreign table
'pid' => $obj->getPid(),
];
$data['tx_imageupload_domain_model_upload'][$obj->getUid()] = [
'image' => $newId,
];
$dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'TYPO3\CMS\Core\DataHandling\DataHandler'
);
$dataHandler->start($data, []);
}
其中$filedata=
$this->request->getArgument('file\u input\u field\u name')
及
$obj=//要为其创建文件的模型的对象
参考文献
这个例子不值得一个美丽的奖项,但它可能会帮助你。它在7.6.x中工作
private function uploadLogo(){
$file['name'] = $_FILES['logo']['name'];
$file['type'] = $_FILES['logo']['type'];
$file['tmp_name'] = $_FILES['logo']['tmp_name'];
$file['size'] = $_FILES['logo']['size'];
// Store the image
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getDefaultStorage();
$saveFolder = $storage->getFolder('logo-companies/');
$newFile = $storage->addFile(
$file['tmp_name'],
$saveFolder,
$file['name']
);
// remove earlier refereces
$GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_file_reference', 'uid_foreign = '. $this->getCurrentUserCompanyID());
$addressRecord = $this->getUserCompanyAddressRecord();
// Create new reference
$data = array(
'table_local' => 'sys_file',
'uid_local' => $newFile->getUid(),
'tablenames' => 'tt_address',
'uid_foreign' => $addressRecord['uid'],
'fieldname' => 'image',
'pid' => $addressRecord['pid']
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $data);
$newId = $GLOBALS['TYPO3_DB']->sql_insert_id();
$where = "tt_address.uid = ".$addressRecord['uid'];
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_address', $where, array('image' => $newId ));
}
您是否检查过-这与
uploadAction
中的代码不同,因为它使用通用类型转换器将上载的文件转换为对象,该对象在创建动物
模型时直接使用。在FE和BE中都能完美工作。@derhansen thx,这是你在谷歌搜索时看到的第一件事,TBH,我希望有更简单的解决方案来解决这个问题。。。基本的事情,无论如何,正如我所看到的,我需要深入了解赫尔穆特的poc。乔斯特,好吧,我不会告诉任何人;)最后,我意识到我过去完全错过了打字机转换器。#请回答你的问题和#请回答我的问题。我已经完成了所有步骤,在我的模型下创建了新文件FileRefence.php。删除输入错误后,所有内容都正常工作(3temp;)此外,我还必须更改模型中的set方法。谢谢你们两位先生:)事实上昨天它又救了我一天;)谢谢分享!我得到了错误致命错误:在/var/www/typo3_src-7.6.15/typo3/sysext/core/Classes/DataHandling/DataHandler.php的第8138行调用null上的成员函数writelog(),并在typo3上发现以下问题:。我做错了什么,或者为什么这对你有效而对我无效?:-)
private function uploadLogo(){
$file['name'] = $_FILES['logo']['name'];
$file['type'] = $_FILES['logo']['type'];
$file['tmp_name'] = $_FILES['logo']['tmp_name'];
$file['size'] = $_FILES['logo']['size'];
// Store the image
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getDefaultStorage();
$saveFolder = $storage->getFolder('logo-companies/');
$newFile = $storage->addFile(
$file['tmp_name'],
$saveFolder,
$file['name']
);
// remove earlier refereces
$GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_file_reference', 'uid_foreign = '. $this->getCurrentUserCompanyID());
$addressRecord = $this->getUserCompanyAddressRecord();
// Create new reference
$data = array(
'table_local' => 'sys_file',
'uid_local' => $newFile->getUid(),
'tablenames' => 'tt_address',
'uid_foreign' => $addressRecord['uid'],
'fieldname' => 'image',
'pid' => $addressRecord['pid']
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $data);
$newId = $GLOBALS['TYPO3_DB']->sql_insert_id();
$where = "tt_address.uid = ".$addressRecord['uid'];
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_address', $where, array('image' => $newId ));
}