Bug #48965

FileReference can't be created in Frontend context

Added by Nico de Haen over 6 years ago. Updated 4 months ago.

Status:
Closed
Priority:
Must have
Assignee:
-
Category:
File Abstraction Layer (FAL)
Target version:
-
Start date:
2013-06-08
Due date:
% Done:

100%

TYPO3 Version:
9
PHP Version:
Tags:
Complexity:
Is Regression:
No
Sprint Focus:

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

Screenshot_2014-05-11_16.02.22.jpg View (81.3 KB) Simon no-lastname-given, 2014-05-11 16:03


Related issues

Related to TYPO3 Core - Feature #5718: Implement File upload support New 2009-12-11

Associated revisions

Revision 035f15ca (diff)
Added by Jost Baron over 4 years ago

[BUGFIX] Add property "uidLocal" to extbase FileReference model

This is needed so extbase persists FileReferences created in the
frontend correctly. Otherwise the field "uid_local" would always
be set to 0 in the database, creating an invalid file reference.

Resolves: #48965
Related: #5718

Releases: 6.2, master
Change-Id: I1e620c18d60546fdc081410b1d27a1b1de6f3e5b
Reviewed-on: http://review.typo3.org/37417
Reviewed-by: Helmut Hummel <>
Tested-by: Helmut Hummel <>
Reviewed-by: Anja Leichsenring <>
Tested-by: Anja Leichsenring <>
Reviewed-by: Christian Kuhn <>
Tested-by: Christian Kuhn <>

Revision aef204f2 (diff)
Added by Jost Baron over 4 years ago

[BUGFIX] Add property "uidLocal" to extbase FileReference model

This is needed so extbase persists FileReferences created in the
frontend correctly. Otherwise the field "uid_local" would always
be set to 0 in the database, creating an invalid file reference.

Resolves: #48965
Related: #5718

Releases: 6.2, master
Change-Id: I1e620c18d60546fdc081410b1d27a1b1de6f3e5b
Reviewed-on: http://review.typo3.org/37463
Reviewed-by: Christian Kuhn <>
Tested-by: Christian Kuhn <>

History

#1 Updated by Steffen Ritter almost 6 years ago

  • Target version deleted (6.2.0)

#2 Updated by Frank Naegler over 5 years ago

this is also a problem within a command controller.

#3 Updated by Mathias Brodala over 5 years ago

  • Target version set to next-patchlevel

#4 Updated by Frans Saris over 5 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

#5 Updated by Simon no-lastname-given over 5 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.

#6 Updated by Frans Saris over 5 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

#7 Updated by Lars A over 5 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)

#8 Updated by Frans Saris over 5 years ago

You need at least 6.2 to get this to work.

#9 Updated by Daniel Ostmann over 5 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();
    }

}

#10 Updated by Jan Kornblum about 5 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...

#11 Updated by Thomas Christiansen about 5 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..

#12 Updated by Daniel Ostmann about 5 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.

#13 Updated by Jan Kornblum about 5 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!

#14 Updated by Thomas Christiansen about 5 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: 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 :)

#15 Updated by Gerrit Code Review over 4 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

#16 Updated by Gerrit Code Review over 4 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

#17 Updated by Gerrit Code Review over 4 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

#18 Updated by Gerrit Code Review over 4 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

#19 Updated by Anonymous over 4 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#20 Updated by Benni Mack about 1 year ago

  • Status changed from Resolved to Closed

#21 Updated by Jan Kornblum 4 months 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();
    }

}

#22 Updated by Jan Kornblum 4 months ago

New issue opened for this: https://forge.typo3.org/issues/88833

Also available in: Atom PDF