Project

General

Profile

Actions

Bug #78044

open

Can't create \TYPO3\CMS\Extbase\Domain\Model\FileReference from \TYPO3\CMS\Core\Resource\File nor from \TYPO3\CMS\Extbase\Domain\Model\File

Added by Philipp Seßner over 7 years ago. Updated about 1 year ago.

Status:
Under Review
Priority:
Must have
Assignee:
-
Category:
Extbase
Target version:
-
Start date:
2016-09-22
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
7
PHP Version:
Tags:
Complexity:
Is Regression:
No
Sprint Focus:

Description

Use Case: I am uploading a file and want to create a file reference to that file, to add it to my model.

Code:

$folder = ResourceFactory::getInstance()->retrieveFileOrFolderObject($folderPath);
$movedFile = $folder->addFile($attachment['tmp_name'], hash_file('md5', $attachment['tmp_name']), DuplicationBehavior::RENAME);

$fileReference = $objectManager->get(TYPO3\CMS\Extbase\Domain\Model\FileReference::class);
$fileReference->setOriginalResource($movedFile);

Exception:

Call to undefined method TYPO3\CMS\Core\Resource\File::getOriginalFile()

Reason:
That is because the requested \TYPO3\CMS\Core\Resource\ResourceInterface (which does not implement method getOriginalFile()) is implemented by \TYPO3\CMS\Core\Resource\FileReference (which is probably expected), aswell as \TYPO3\CMS\Core\Resource\File
The breaking commit is https://forge.typo3.org/projects/typo3cms-core/repository/revisions/159fd37d509e00983a4348a2e860b3579f5f1b5b

Suggested Solutions:
I see 3 possible solutions for the TYPO3\CMS\Extbase\Domain\Model\FileReference::setOriginalResource() method:
  1. It requires \TYPO3\CMS\Core\Resource\FileReference (like before, but I don't favour this solution as I cannot find an easy way to get an object of the ...\Resource\FileReference from a \TYPO3\CMS\Core\Resource\File)
  2. It requires \TYPO3\CMS\Core\Resource\File (or \TYPO3\CMS\Core\Resource\AbstractFile) and calls "getUid()" directly
  3. It requires \TYPO3\CMS\Extbase\Domain\Model\File (this would be package compliant, and you can easily get an object of the required class from \TYPO3\CMS\Core\Resource\File)

Related issues 1 (1 open0 closed)

Related to TYPO3 Core - Feature #90374: Add setOriginaFile / setFile setter to TYPO3\CMS\Extbase\Domain\Model\FileReferenceUnder Review2020-02-15

Actions
Actions #1

Updated by Claus Due over 7 years ago

I think the right solution here is to add `getOriginalFile` to the ResourceInterface and File class. On File class this can simply return self.

Essentially: the method signature was changed for a very good reason (PHP7 compatibility) and requiring interfaces is more consistent than requiring either of the specific classes (not least due to the PHP7 signature issue). Whereas the fact that we call a method that is not declared on the interface is bad in and of itself, and fixing that also fixes this issue.

Actions #2

Updated by Timo Kiefer over 7 years ago

My workaround for this is:

            $file = $targetFolder->addUploadedFile($_FILES['file'], DuplicationBehavior::RENAME);
            $fileResourceReference = new \TYPO3\CMS\Core\Resource\FileReference(array('uid_local' => $file->getUid()));

            /**
             * @var $fileReference FileReference
             */
            $fileSysReference = $this->objectManager->get(FileReference::class);
            $fileSysReference->setOriginalResource($fileResourceReference);
Actions #3

Updated by Christian Eßl about 4 years ago

  • Related to Feature #90374: Add setOriginaFile / setFile setter to TYPO3\CMS\Extbase\Domain\Model\FileReference added
Actions #4

Updated by Bernhard Eckl about 2 years ago

Thank you Timi Kiefer for your comment, it saved me so many hours and I already spent a day for it. Before I did the file reference using setFile (own FileReference Model) but then I was not able to do a $file->getOriginalResource()->getPublicUrl() (missing uid) for just uploaded files in the current action. Now it works! :)

Actions #5

Updated by Gerrit Code Review about 1 year ago

  • Status changed from New to Under Review

Patch set 2 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/78166

Actions

Also available in: Atom PDF