I manage to upload several files, but they are not connected to my model. In the table sys_file_reference the uid_foreign is missing.
My model is called Place and has the property images.
I followed the instructions in the manual except for the following modifications:
SQL
My images field is not of type varchar but int.
...
images int(11) unsigned NOT NULL default '0',
...
TCA
In TCA I define the maximum number and the local_table for sys_file_reference
class Place extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* images
*
* @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* @cascade remove
*/
protected $images;
/**
* __construct
*/
public function __construct()
{
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
}
/**
* Initializes all ObjectStorage properties
* Do not modify this method!
* It will be rewritten on each save in the extension builder
* You may modify the constructor of this class instead
*
* @return void
*/
protected function initStorageObjects()
{
...
$this->images = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Adds a FileReference
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
* @return void
*/
public function addImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
$this->images->attach($image);
}
/**
* Removes a FileReference
*
* @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $imageToRemove The FileReference to be removed
* @return void
*/
public function removeImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $imageToRemove) {
$this->images->detach($imageToRemove);
}
/**
* Returns the images
*
* @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $images
*/
public function getImages() {
return $this->images;
}
/**
* Sets the images
*
* @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $images
* @return void
*/
public function setImages(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $images) {
$this->images = $images;
}
...
}
Controller
In the controller I replaced the line $storage = ResourceFactory::getInstance()->getStorageObject(1); which did not work for me.
/**
* action create
*
* @param \GreenBits\FoejPlaces\Domain\Model\Place $newPlace
* @return void
*/
public function createAction(\GreenBits\FoejPlaces\Domain\Model\Place $newPlace)
{
$this->addFlashMessage('The object was created.', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING);
# A property name is needed in case specified in the Fluid Widget
# <mu:widget.upload property="images"/>
$uploadedFiles = $this->uploadFileService->getUploadedFiles('images');
# Process uploaded files and move them into a Resource Storage (FAL)
foreach($uploadedFiles as $uploadedFile) {
/** @var \Fab\MediaUpload\UploadedFile $uploadedFile */
$uploadedFile->getTemporaryFileNameAndPath();
// $storage = ResourceFactory::getInstance()->getStorageObject(1);
$resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
$storage = $resourceFactory->getDefaultStorage();
/** @var File $file */
$file = $storage->addFile(
$uploadedFile->getTemporaryFileNameAndPath(),
$storage->getRootLevelFolder(),
$uploadedFile->getFileName(),
\TYPO3\CMS\Core\Resource\DuplicationBehavior::RENAME
);
# Note: Use method `addUploadedFile` instead of `addFile` if file is uploaded
# via a regular "input" control instead of the upload widget (fine uploader plugin)
# $file = $storage->addUploadedFile()
$fileReference = $this->objectManager->get(\GreenBits\FoejPlaces\Domain\Model\FileReference::class);
$fileReference->setFile($file);
$newPlace->addImage($fileReference);
}
$this->placeRepository->add($newPlace);
// $this->redirect('list');
}
I manage to upload several files, but they are not connected to my model. In the table sys_file_reference the uid_foreign is missing. My model is called Place and has the property images. I followed the instructions in the manual except for the following modifications:
SQL My images field is not of type varchar but int.
TCA In TCA I define the maximum number and the local_table for sys_file_reference
Model
Controller In the controller I replaced the line $storage = ResourceFactory::getInstance()->getStorageObject(1); which did not work for me.