arderoma
arderoma

Reputation: 427

TYPO3 FileReference does not save the tablename on the DB. Uploading file from frontend on TYPO3

In my custom extension on TYPO3 10.4 I'm trying to upload a file (image) from the frontend. The file gets uploaded just fine, the rows on the DB seemed to be inserted just fine but there is some data missing.

This is my form:

    <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>

The model:

     /**
     * 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;
    }

The controller:

     /**
     * 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);
    }

Surprisingly, just that easy, this seems to work just fine. I get the image uploaded on the server, the correct UID of that sys_file_reference on my custom table, the sys_file_reference gets the correct UID of that sys_file... but as you can see in the pic that follows there are a few data missing, "tablename" and "table_local" and as soon as I add that data manually the relationships work (the first rows, where this data is not missing is from rows created from the backend, working fine)

enter image description here

My question is, why? What do I do to fix that?

Upvotes: 0

Views: 573

Answers (1)

Georg Ringer
Georg Ringer

Reputation: 7939

The problem is that extbase doesn't know those values, therefore you need to state those in the TCA. Given this example

'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']
    ),
],

The foreign_match_fields part is the relevant one which is not needed if you don't handle file uploads in the Frontend.

Upvotes: 0

Related Questions