如何更新在 extbase 中存储为字符串的图像?

How to update images stored as strings in extbase?

我最近创建了一个具有文件上传功能的扩展。我决定将它存储为一个字符串。我是这样用的:

在控制器中:

    public function initializeAction() {
        if ($this->arguments->hasArgument('blog')) {
            $this->arguments->getArgument('blog')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('image', 'array');
        }
    } 

模型中:

  /**
   * Returns the image
   * 
   * @return string $image
   */
  public function getImage()
  {
        return $this->image;
  }

  /**
   * Sets the image
   * 
   * @param \array $image
   * @return void
   */
  public function setImage(array $image)
  {
        die(debug::var_dump($image));
        if (!empty($image['name']))
        {
              $imageName = $image['name'];
              $imageTempName = $image['tmp_name'];
              $basicFileUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Utility\File\BasicFileUtility');
              $imageNameNew = $basicFileUtility->getUniqueName($imageName, \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('uploads/tx_myextension/'));
              \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($imageTempName, $imageNameNew);
              $this->image = basename($imageNameNew);
        }
  }

TCA:

   'config' => [
        'type' => 'group',
        'internal_type' => 'file',
        'uploadfolder' => 'uploads/tx_myextension',
        'show_thumbs' => 1,
        'size' => 1,
        'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
        'disallowed' => ''
    ],

在我的表格中:

<f:form action="update" name="blog" object="{blog}" >
<f:form.upload property="image" class="form-control" />
...

现在 创建新对象时效果很好,但是当我尝试更改此图像(使用 updateAction)时,我收到此错误消息:

 Exception while property mapping at property path "image":
 No converter found which can be used to convert from "string" to "array".

我想避免通过 FAL 上传或自己编写转换。我希望我只是错过了一些微不足道的东西。

查看来自 tt_addressupdate script 以了解如何进行更新。

简而言之:您必须读取文件的所有条目并将它们从上传目录移动到您的文件存储,然后您必须添加一个 file_referencesys_file 与您的域连接起来型号:

GeneralUtility::upload_copy_move(
    PATH_site . 'uploads/tx_myextension/' . $imageName,
    PATH_site . 'fileadmin/tx_myextension/' . $imageName);

$fileObject = $this->storage->getFile('fileadmin/tx_myextension/' . $imageName);
if ($fileObject instanceof \TYPO3\CMS\Core\Resource\File) {
    $this->fileRepository->add($fileObject);
    $dataArray = [
        'uid_local' => $fileObject->getUid(),
        'tablenames' => 'tx_your_domain_model',
        'fieldname' => 'image',
        'uid_foreign' => 'your model uid',
        'table_local' => 'sys_file',
        'cruser_id' => 999,
        'pid' => 'your_model_pid',
        'sorting_foreign' => $imageCount,
        'sys_language_uid' => 0
    ];

    if ($this->getDatabaseConnection()->exec_INSERTquery('sys_file_reference', $dataArray)) {
        $imageCount++;
    }
}

我犯了一个非常愚蠢和简单的错误。在编辑表单中我忘了添加:enctype="multipart/form-data"

<f:form action="update" enctype="multipart/form-data" name="blog" object="{blog}" >