Bug #48965
closedFileReference can't be created in Frontend context
Added by Nico de Haen almost 11 years ago. Updated over 4 years ago.
100%
Description
There is no API for creating a FileReference without calling TCEmain according to the Wiki thus it is not possible to create it in frontend context
Files
Screenshot_2014-05-11_16.02.22.jpg (81.3 KB) Screenshot_2014-05-11_16.02.22.jpg | Simon no-lastname-given, 2014-05-11 16:03 |
Updated by Frank Nägler almost 10 years ago
this is also a problem within a command controller.
Updated by Mathias Brodala almost 10 years ago
- Target version set to next-patchlevel
Updated by Frans Saris almost 10 years ago
- Is Regression set to No
The only thing what currently is missing for this is:
/** * Class FileReference */ class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { /** * We need this property so that the Extbase persistence can properly persist the object * * @var integer */ protected $uidLocal; /** * @param \TYPO3\CMS\Core\Resource\FileReference $originalResource */ public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) { $this->originalResource = $originalResource; $this->uidLocal = $originalResource->getOriginalFile()->getUid(); } }
When setting the foreign_match_fields in TCA and using a model that has the above modifications your able to persist a sys_file_reference
Have a look at Helmut's approuch here https://github.com/helhum/upload_example
Updated by Simon no-lastname-given almost 10 years ago
i got the same error within a command controller:
$fileIdentifier = 'example.jpg'; $storage = $storageRepository->findByUid(1); $file = $storage->getFile($fileIdentifier); $fileReference = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\FileReference'); $fileReference->setOriginalResource($file); $model->addImage($fileReference);
attached a screenshot with the sys_file_reference table.
Updated by Frans Saris almost 10 years ago
Hi Simon,
You need to use your own FileReference object that implements setOriginalResource() like described above.
$fileReference = $this->objectManager->get('MyVendor\\MyExtension\\Domain\\Model\\FileReference'); $fileReference->setOriginalResource($file);
And can you post the TCA here. Maybe we can spot the error.
gr. Frans
Updated by Lars A over 9 years ago
I have also an empty File Reference in the database :/
File is filled
File Reference has only a filled uid_local (also uid, tstamp and crdate).
My TCA is like the one of Helmuts example.
My only differents is, that i use 6.1(.8)
Updated by Frans Saris over 9 years ago
You need at least 6.2 to get this to work.
Updated by Daniel Ostmann over 9 years ago
Thank you Frans, for me it works perfectly with an own FileReference.
Take a look into https://github.com/helhum/upload_example!
Just define in your own FileReference a property uidLocal. That's it.
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { /** * We need this property so that the Extbase persistence can properly persist the object * * @var integer */ protected $uidLocal; /** * @param \TYPO3\CMS\Core\Resource\FileReference $originalResource */ public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) { $this->originalResource = $originalResource; $this->uidLocal = $originalResource->getOriginalFile()->getUid(); } }
Updated by Jan Kornblum over 9 years ago
What am i doing wrong? I've used an own FileReference object extended as described above and mapped it to sys_file_reference. But i get the following error:
#1: PHP Catchable Fatal Error: Argument 1 passed to XXX\YYY\Domain\Model\FileReference::setOriginalResource() must be an instance of TYPO3\CMS\Core\Resource\FileReference, instance of TYPO3\CMS\Core\Resource\File given, called in /zzz/AnyController.php...
Updated by Thomas Christiansen over 9 years ago
Today i ran into the same problem and i finally got it working. Although i had to adapt your solutions a little bit.
This is my FileReference Model:
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { /** * We need this property so that the Extbase persistence can properly persist the object * * @var integer */ protected $uidLocal; /** * @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource */ public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) { $this->originalResource = $originalResource; $this->uidLocal = (int)$originalResource->getUid(); } }
Note that the setter expects an object of type ResourceInterface and NOT FileReference.
Then i ran into an SQL Error, because the table of my extended model does of course not exist.
So i had to configure extbase table mapping:
plugin.tx_foo.persistence.classes.Vendor\Foo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
Also make sure, that your TCA includes this config:
'foreign_match_fields' => array( 'fieldname' => 'whatever_your_field_is_called', 'tablenames' => 'tx_foo_domain_model_bar', 'table_local' => 'sys_file', ),
Summarized: Piece of cake.. No seriously, this is not pretty :/
Btw i am working in a CommandController..
Updated by Daniel Ostmann over 9 years ago
Mmh, for me it wasn't necessary to define anything in TCA. I think it depends on what you wanna do with it. For me it works perfectly by just defining the $uidLocal property in my own FileReference model and use it instead of the original FileReference.
Updated by Jan Kornblum over 9 years ago
Do you know this thread (including the examples at github):
http://forum.typo3.org/index.php?t=msg&th=206859&goto=721954&#msg_721954
That worked perfectly for me!
Updated by Thomas Christiansen over 9 years ago
Ah ok i was not aware that there are two kinds of FileReferences as well, but basically i wrote nearly the same setFile-Function as Helmut here https://gist.github.com/helhum/9a274ac1c29b50eedd71
Although i put it in the setOriginalResource in my Model :)
Btw i am trying to set a FileReference to i file which i got via:
$storage = $storageRepository->findByUid(1);
$file = $storage->getFile('foo.jpg');
@Daniel Hinderink: I totally need the TCA Config, if I dont have it the columns "tablenames" and "table_local" in sys_file_reference are emtpy.
I was really confused about the $originalFileIdentifier property, because i didtn see the property mapping on uid_local in the typoscript.
So everything is working now, but i dont like the solution :)
Updated by Gerrit Code Review about 9 years ago
- Status changed from New to Under Review
Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/37417
Updated by Gerrit Code Review about 9 years ago
Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/37417
Updated by Gerrit Code Review about 9 years ago
Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/37417
Updated by Gerrit Code Review about 9 years ago
Patch set 1 for branch TYPO3_6-2 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/37463
Updated by Anonymous about 9 years ago
- Status changed from Under Review to Resolved
- % Done changed from 0 to 100
Applied in changeset 035f15ca16eb8e141bbf96f1c25692c25ac123ff.
Updated by Jan Kornblum over 4 years ago
- Target version deleted (
next-patchlevel) - TYPO3 Version changed from 6.2 to 9
I hope this won't be reopened wrongly, but it is still missing to create a new file reference based on a given file object. So still an own FileReference class must be used. A method "setFile()" shoul be added like below...
/**
* Class FileReference
*/
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
/**
* uid of a sys_file
*
* @var integer
*/
protected $originalFileIdentifier;
/**
* setOriginalResource
*
* @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource
* @return void
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) {
$this->originalResource = $originalResource;
$this->originalFileIdentifier = (int)$originalResource->getOriginalFile()->getUid();
}
/**
* setFile
*
* @param \TYPO3\CMS\Core\Resource\File $falFile
* @return void
*/
public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
$this->originalFileIdentifier = (int)$falFile->getUid();
}
}
Updated by Jan Kornblum over 4 years ago
New issue opened for this: https://forge.typo3.org/issues/88833
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
Updated by Jan Kornblum over 3 years ago
- Related to Feature #88833: Extend possibility to create FileReference in frontend with a given File object added