Issue Bug Closed

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

Anonymous 09/22/16, 18:17 updated 07/15/24, 12:22

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)

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.