TYPO3 6.2 - 如何在前端(FE)中创建FileReference?

时间:2015-02-24 23:01:47

标签: file-upload typo3 extbase typo3-6.2.x fal

我有假设的Zoo扩展名,其中Animal模型带有photo字段和FrontEnd(FE)插件,具有典型的CRUD操作。 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方法(link)也无法使用,因为它对FE用户不可用。

TL; DR

如何从现有(刚刚创建的)FAL记录中将FileReference添加到Animal模型?

3 个答案:

答案 0 :(得分:13)

你需要做几件事。这个issue on forge是我收到信息的地方,有些内容是从Helmut Hummels frontend upload example(以及accompanying blogpost)中删除的,而@derhansen已经评论过了。

我不完全确定这是否是您需要的所有内容,因此请随意添加内容。这不使用TypeConverter,你可能应该这样做。这将打开更多的可能性,例如,很容易实现删除和替换文件引用。

你需要:

  • 从File对象创建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中的字段($uidLocal),所以它不会起作用。您需要从extbase模型继承,添加该字段并填充它。不要忘记在TypoScript中添加映射以将您自己的模型映射到sys_file_reference

    config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
    

    该类看起来像这样(取自伪造问题):

     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();
          }
      }
    
  • 将其添加到配置部分的图像字段的TCA中(当然适应您的表格和字段名称):

    'foreign_match_fields' => array(
        'fieldname' => 'photo',
        'tablenames' => 'tx_zoo_domain_model_animal',
        'table_local' => 'sys_file',
    ),
    
  • 编辑:如果在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);
    
  • 不要告诉任何人你做了什么。

答案 1 :(得分:0)

以下是使用FAL和创建文件引用

在TYPO3中上传文件的完整功能
/**
 * 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 =   $这 - >请求 - > getArgument( 'file_input_field_name');

  

$ obj = //您要为其创建文件的模型的对象   参考

答案 2 :(得分:0)

This example does not deserve a beauty prize but it might help you. It works in 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 ));
}