Project

General

Profile

Actions

Bug #78044

closed

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 about 8 years ago. Updated 4 months ago.

Status:
Closed
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 (0 open1 closed)

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

Actions
Actions #1

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.

Actions #2

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);
Actions #3

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
Actions #4

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! :)

Actions #5

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

Actions #6

Updated by Georg Ringer 4 months ago

  • Status changed from Under Review to Closed

closed as duplicate of #88833

Actions #7

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

Actions #8

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

Actions #9

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

Actions #10

Updated by Georg Ringer 4 months ago

  • Status changed from Under Review to Closed
Actions

Also available in: Atom PDF