Bug #78044
closedCan't create \TYPO3\CMS\Extbase\Domain\Model\FileReference from \TYPO3\CMS\Core\Resource\File nor from \TYPO3\CMS\Extbase\Domain\Model\File
0%
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
I see 3 possible solutions for the
TYPO3\CMS\Extbase\Domain\Model\FileReference::setOriginalResource()
method:
- 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
) - It requires
\TYPO3\CMS\Core\Resource\File
(or\TYPO3\CMS\Core\Resource\AbstractFile
) and calls "getUid()" directly - 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
)
Updated by Claus Due about 8 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.
Updated by Timo Kiefer about 8 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);
Updated by Christian Eßl almost 5 years ago
- Related to Feature #90374: Add setOriginaFile / setFile setter to TYPO3\CMS\Extbase\Domain\Model\FileReference added
Updated by Bernhard Eckl over 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! :)
Updated by Gerrit Code Review over 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
Updated by Georg Ringer 4 months ago
- Status changed from Under Review to Closed
closed as duplicate of #88833
Updated by Gerrit Code Review 4 months ago
- Status changed from Closed to Under Review
Patch set 3 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
Updated by Gerrit Code Review 4 months ago
Patch set 4 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
Updated by Gerrit Code Review 4 months ago
Patch set 5 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
Updated by Georg Ringer 4 months ago
- Status changed from Under Review to Closed