新创建的sys_file_refences的问题

时间:2019-02-14 16:58:33

标签: typo3 extbase typo3-7.6.x

我正在尝试在TYPO3 7.6的调度程序任务中创建sys_file_references。

但是在创建的sys_file_references中只有标题和说明可以看到,该文件丢失了。

我的模型类包含以下内容:

    /**
     * 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();
    }

任务代码:

            $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();
            }

在创建的sys_file_reference中缺少文件: a busy cat

我尝试附加的$ file: a busy cat

2 个答案:

答案 0 :(得分:1)

使用Extbase创建文件引用非常棘手。最好的方法是直接操作数据库:

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();
}

上面的示例适用于后端模式。您应该在那里使用DataHandler,因为它还会更新各种系统表。

答案 1 :(得分:0)

  

此函数删除sys_file_reference的(设置为Deleted = 0)

    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();
    }