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

时间:2018-08-17 13:02:50

标签: typo3 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上传或编写自己的转化记录。我希望我只是错过了一些琐碎的事情。

2 个答案:

答案 0 :(得分:0)

看看tt_address中的update script,了解如何进行更新。

简而言之:您必须阅读文件的所有条目并将它们从上载目录移到文件存储中,然后必须添加file_reference来将sys_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++;
    }
}

答案 1 :(得分:0)

就我而言,这是一个非常愚蠢而简单的错误。在编辑表单中,我忘记添加以下内容:enctype =“ multipart / form-data”

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