TYPO3 FileReference 不会在 DB 上保存表名在TYPO3上从前端上传文件

问题描述

在我在 TYPO3 10.4 上的自定义扩展中,我试图从前端上传文件(图像)。文件上传得很好,数据库上的行似乎插入得很好,但缺少一些数据。

这是我的表格:

    <f:form method="post" action="create" name="blackboard"
                              object="{blackboard}" enctype="multipart/form-data">
         <f:form.textfield placeholder="Titel*" required="true" property="title"></f:form.textfield>
         <f:form.upload property="image" name="image" />
         <f:form.submit class="btn btn-primary" value="{f:translate(key: 'submit',default: 'Absenden')}"></f:form.submit>
    </f:form>

模型:

     /**
     * image
     * 
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     * @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
     */
    protected $image = null;
     /**
     * Returns the image
     * 
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     */
    public function getimage()
    {
        return $this->image;
    }

    /**
     * Sets the image
     * 
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     * @return void
     */
    public function setimage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image)
    {
        $this->image = $image;
    }

控制器:

     /**
     * action create 
     * @param Blackboard
     */
    public function createAction(Blackboard $blackboard)
    {
        $blackboard->setPid($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['hebo_blackboards']['BlackboardsstoragePId']);
        $blackboard->setUser($GLOBALS['TSFE']->fe_user->user['uid']);
        $this->blackboardRepository->add($blackboard);
    }

令人惊讶的是,就这么简单,这似乎工作得很好。我将图像上传到服务器上,我的自定义表上该 sys_file_reference 的正确 UID,sys_file_reference 获得该 sys_file 的正确 UID... tablename”和“table_local”,一旦我手动添加该数据,关系就会起作用(第一行,这些数据不丢失的行来自后端创建的行,工作正常)

enter image description here

我的问题是,为什么?我该怎么做才能解决这个问题?

解决方法

问题是 extbase 不知道这些值,因此您需要在 TCA 中说明这些值。鉴于这个例子

'extra_files' => [
    'label' => 'A file','config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
        'extra_files',[
            'foreign_match_fields' => [
                'tablenames' => 'tx_yourtable_domain_model_fo','table_local' => 'sys_file'
            ]
        ],$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
    ),],

foreign_match_fields 部分是相关部分,如果您不在前端处理文件上传,则不需要。