'Problems with new created sys_file_refences
I'm trying to create sys_file_references in a scheduler task in TYPO3 7.6.
But in the created sys_file_references are only Title and Description to see, the file is missing.
My model class contains this:
/**
* uid of a sys_file
*
* @var integer
*/
protected $originalFileIdentifier;
/**
* setOriginalResource
*
* @param \TYPO3\CMS\Core\Resource\FileReference $originalResource
* @return void
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) {
$this->originalResource = $originalResource;
$this->originalFileIdentifier = (int)$originalResource->getOriginalFile()->getUid();
}
/**
* @return \TYPO3\CMS\Core\Resource\AbstractFile|\TYPO3\CMS\Core\Resource\FileReference|\TYPO3\CMS\Core\Resource\Folder|\TYPO3\CMS\Core\Resource\ResourceInterface
* @throws \TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
*/
public function getOriginalResource()
{
if ($this->originalResource === null) {
$this->originalResource = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance()->getFileReferenceObject($this->getUid());
}
return $this->originalResource;
}
/**
* setFile
*
* @param \TYPO3\CMS\Core\Resource\File $falFile
* @return void
*/
public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
$this->originalFileIdentifier = (int)$falFile->getUid();
}
Task code:
$contentElement = $contentRepository->findByUid(123);
if( file_exists($pathToFile)) {
/** @var File $file */
$file = $resourceFactory->getFileObjectFromCombinedIdentifier('1:/'.$pathToFile);
/** @var SysFileReference $newFileReference */
$newFileReference = $objectManager->get('VENDOR\\MyExtension\\Domain\\Model\\SysFileReference');
$newFileReference->setFile($file);
$newFileReference->setTitle('The Title');
$newFileReference->setDescription('Lorem Ipsum Dolor');
$contentElement->addMedia($newFileReference);
$contentRepository->update($contentElement);
$persistanceManager->persistAll();
}
In the created sys_file_reference is the file missing:
(source: honrath.de)
The $file I try to attach:
(source: honrath.de)
Solution 1:[1]
It is very tricky to create file references with Extbase. Your best way is direct database manipulation:
protected function createFileReference($contentUid, $pid, $fieldName, File $file)
{
$data = [
'sys_file_reference' => [
StringUtility::getUniqueId('NEW') => [
'table_local' => 'sys_file',
'uid_local' => $file->getProperty('uid'),
'tablenames' => 'tt_content',
'uid_foreign' => $contentUid,
'fieldname' => $fieldName,
'pid' => $pid,
]
]
];
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($data, []);
$dataHandler->admin = true;
$dataHandler->process_datamap();
}
The example above is for the Backend mode. You should use DataHandler
there because it would also update various system tables.
Solution 2:[2]
This function deletes (set deleted=0) for a sys_file_reference
protected function deleteFileReference($uid)
{
$cmd['sys_file_reference'][$uid]['delete'] = 1;
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start([], $cmd);
$dataHandler->admin = true;
$dataHandler->process_cmdmap();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | User366 |
Solution 2 |