【问题标题】:How to create a file relation in TYPO3 v9 in a CLI command from Symfony?如何使用 Symfony 的 CLI 命令在 TYPO3 v9 中创建文件关系?
【发布时间】:2019-07-19 18:14:38
【问题描述】:

我创建了一个迁移命令,使用 TYPO3 9.5.4 中的新 Symfony 命令将数据从另一个 cms 导入到 TYPO3。唯一的问题是在我导入的数据和他们的图像之间建立关系。 TYPO3 documentation 中的示例不适用于我。

我创建我的新实体(例如产品)并调用我的方法“addBackendFileRelation”来创建上传文件(数据库中存在记录)和我的新产品(数据库中存在记录)之间的文件关系。

public function addBackendFileRelation( int $uid, string $table, string $field, File $file ): bool {
    $resourceFactory = GeneralUtility::makeInstance( ResourceFactory::class );

    try {
        $fileObject = $resourceFactory->getFileObject( $file->getUid() );
        $objectUid = $fileObject->getUid();
    } catch ( FileDoesNotExistException $e ) {
        return false;
    }
  
    $record = BackendUtility::getRecord( $table, $uid );
    $newId = 'NEW1234';
    $data = [];
    $data['sys_file_reference'][$newId] = [
        'table_local' => 'sys_file',
        'uid_local' => $objectUid,
        'tablenames' => $table,
        'uid_foreign' => $uid,
        'fieldname' => $field,
        'pid' => $record['pid'],
    ];
    $data[$table][$uid] = [
        $field => $newId,
        'pid' => $record['pid'],
    ];
    $dataHandler = GeneralUtility::makeInstance( DataHandler::class );
    $dataHandler->start( $data, [] );
    $dataHandler->process_datamap();

    foreach ( $dataHandler->errorLog as $log ) {
        var_dump( $log );
    }

    $fileRefUid = $dataHandler->substNEWwithIDs[$newId];

    return (int)$fileRefUid > 0;
}

来自 DataHandler 的 errorLog 打印以下内容:
[1.2.1]: Attempt to modify table 'sys_file_reference' without permission

那么我可以做些什么来创建关系呢?为 cli 创建后端用户对我不起作用。

[编辑] 解决方案

使用教程(link from the comments) 的示例,它可以工作。我在尝试创建关系之前调用了方法:

    public function execute() {
      // authenticate CommandLineUserAuthentication user for DataHandler usage
      $GLOBALS['BE_USER']->backendCheckLogin();

      // [...]

      $this->addBackendFileRelation( $uid, $table, $field, $file );

      // [...]
    }

【问题讨论】:

标签: php symfony typo3 command-line-interface typo3-9.x


【解决方案1】:

解决方案在评论和编辑的问题中。在尝试使用 DataHandler 之前,只需使用 $GLOBALS['BE_USER']->backendCheckLogin()

【讨论】:

    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2018-05-30
    相关资源
    最近更新 更多