问题描述
我有假设的动物园扩展,其中动物模型与照片字段和FrontEnd(FE)插件具有典型的CRUD操作。 照片字段是典型的FAL的 FileReference ,它可以在后端(BE)和普通的TCA IRRE配置下正常工作。
我可以成功将文件上传到存储设备,它在 Filelist 模块中可见,我可以在我的动物编辑过程中使用它,无论如何,我不能在我的FE插件中创建 FileReference 。
我目前的方法是这样的: p>
/ **
* @param \Zoo\Zoo\Domain\Model\Animal $ animal
* /
public function updateAction(\ Zoo\Zoo\Domain\Model\Animal $ animal){
//从上传的`photo` form's $ _FILES
$ file = $ this-> getFromFILES('tx_zoo_animal','photo');
$ b $ if 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:使目标存储可配置
//这将上传的文件完全添加到存储中
$ fileObject = $ storage-> addFile($ file ['tmp_name'],$ storage - > getRootLevelFolder(),$ file ['name']);
//这里我卡住了......下面的行不起作用(抛出异常1:/)
//这是因为$ fileObject是FileInterface的类型,FileReference是必需的
$ animal-> addPhoto($ fileObject);
}
$ this-> animalRepository-> update($ animal);
$ this-> redirect('list');
}
试图通过这行创建引用抛出异常:
$ animal-> addPhoto($ fileObject);
我该如何解决这个问题?
检查: DataHandler 方法()也不起作用,因为FE用户不可用。
TL; DR 如何添加 FileReference 到动物现有模型(刚刚创建的)FAL记录? 编辑:使用 \ TYPO3 \CMS\Extbase\Domain\Model\FileReference 如果在TYPO3 6.2.11或7.2或以上。
'foreign_match_fields'=> array $(
'fieldname'=>'photo',
'tablenames'=>'tx_zoo_domain_model_animal',
'table_local'=>'sys_file',
),
<因此,最后添加创建的 $ fileRef 而不是 $ fileObject
$ fileRef = GeneralUtility :: makeInstance('\Zoo\Zoo\Domain\Model\FileReference');
$ fileRef-> setOriginalResource($ fileObject);
$ animal-> addPhoto($ fileRef);
不要告诉任何人你做了什么。
I have the hypothetical Zoo extension in which I've Animal model with photo field and FrontEnd (FE) plugin with typical CRUD actions. photo field is typical FAL's FileReference and it works perfectly in backend (BE) with common TCA IRRE config.
I'm able to successful upload the file to the storage, it's visible in the Filelist module, and I can use it in BE during my Animal editing, anyway I can't create FileReference within my FE plugin.
My current approach looks like this:
/** * @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'); }
anyway attempt to create reference by this line throws exception:
$animal->addPhoto($fileObject);
How can I resolve this?
Checked: DataHandler approach (link) won't work also, as it's unavailable for FE users.
TL;DR
How to add FileReference to Animal model from existing (just created) FAL record?
You need to do several things. This issue on forge is where I got the info, and some stuff is taken out of Helmut Hummels frontend upload example (and the accompanying blogpost) which @derhansen already commented.
I'm not entirely sure if this is everything you need, so feel free to add things. This does not use a TypeConverter, which you should probably do. That would open further possibilities, for example it would be easily possible to implement deletion and replacement of file references.
You need to:
- Create a FAL file reference object from the File object. This can be done using FALs resource factory.
- Wrap it in a \TYPO3\CMS\Extbase\Domain\Model\FileReference (method ->setOriginalResource)
EDIT: This step is unnecessary as of TYPO3 6.2.11 and 7.2, you can directly use the class \TYPO3\CMS\Extbase\Domain\Model\FileReference.
But, because the extbase model misses a field ($uidLocal) in 6.2.10rc1, that won't work. You need to inherit from the extbase model, add that field, and fill it. Don't forget to add a mapping in TypoScript to map your own model to sys_file_reference.
config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
The class would look like this (taken from the forge issue):
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { /** * We need this property so that the Extbase persistence can properly persist the object * * @var integer */ protected $uidLocal; /** * @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource */ public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) { $this->originalResource = $originalResource; $this->uidLocal = (int)$originalResource->getUid(); } }
Add this to the TCA of the image field, in the config-section (adapt to your table and field names of course):
'foreign_match_fields' => array( 'fieldname' => 'photo', 'tablenames' => 'tx_zoo_domain_model_animal', 'table_local' => 'sys_file', ),
EDIT: Use \TYPO3\CMS\Extbase\Domain\Model\FileReference in this step if on TYPO3 6.2.11 or 7.2 or above.
So at the end add the created $fileRef instead of $fileObject
$fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference'); $fileRef->setOriginalResource($fileObject); $animal->addPhoto($fileRef);
Don't tell anyone what you have done.
这篇关于TYPO3 6.2 - 如何在前端(FE)创建FileReference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!