在 TYPO3 8.7 中,当尝试通过命令行界面 (CLI) 使用 external_import 导入数据时,可能会遇到诸如权限不足或缓存写入失败等错误。这些错误通常与 CLI 环境下缺少必要的后端认证初始化有关。以下将详细介绍如何解决这些问题。
问题描述
在使用自定义 Extbase 扩展,并通过 CLI 脚本执行外部导入时,可能会遇到以下错误:
- “The requested configuration was not found (table: tx_something_domain_model_formation, index: 0). Message: The temporary cache file “thisisthepathtothewebsites/typo3temp/var/Cache/Data/l10n/61794fcc21579208003962.temp” could not be written. [1334756737]”
- “User doesn’t have enough rights for synchronizing table tx_something_domain_model_formation.”
这些错误表明在 CLI 环境中,TYPO3 缺少必要的认证信息,导致无法访问数据库或写入缓存文件。
解决方案
解决此问题的关键是在 CLI 脚本中显式地初始化后端认证。这可以通过在执行导入操作之前添加以下代码来实现:
代码示例
以下是修改后的 Command 类示例,展示了如何集成上述解决方案:
<?php namespace VendorSomethingCommand; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; use SymfonyComponentConsoleStyleSymfonyStyle; use TYPO3CMSCoreCoreBootstrap; use TYPO3CMSCoreUtilityGeneralUtility; class FormationImportCommand extends Command { protected function configure() { $this->setDescription('Synchroniser les formations externe dans typo3.'); } protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); // 初始化后端认证 Bootstrap::getInstance()->initializeBackendAuthentication(); $arrayFormations = $this->getFormations(); $message = $this->importFormation($arrayFormations); print_r($message); $io->writeln('Test'); return 0; } private function getFormations() { $jsonPartOne = json_decode(file_get_contents(PATH_typo3conf . "ext/something/Ressources/Private/Assets/Json/apiPage1.json"), true); $jsonPartTwo = json_decode(file_get_contents(PATH_typo3conf . "ext/something/Ressources/Private/Assets/Json/apiPage2.json"), true); $jsonFormations = array_merge($jsonPartOne['elements'], $jsonPartTwo['elements']); return $jsonFormations; } private function importFormation(array $formations) { if (count($formations) === 0) { //TODO Erreur return; } $dataFormations = []; $dataGroupe = []; $dataformateurs = []; $dataFormationsGroupes = []; foreach ($formations as $formation) { $dataFormations[] = [ 'id_formation' => $formation['idFormation'], 'nom' => $formation['nom'], 'description' => $formation['description']['texteHtml'], 'duree' => $formation['dureeFormation']['dureeEnHeures'], ]; } $objectManager = GeneralUtility::makeInstance(TYPO3CMSExtbaseObjectObjectManager::class); $importer = $objectManager->get(CobwebExternalImportImporter::class); $importer->getExtensionConfiguration(); $importer->setContext('cli'); $importer->setDebug(true); $importer->setVerbose(true); return $importer->import('tx_something_domain_model_formation', 0, $dataFormations); } }
注意事项
- 确保在 use 语句中引入 TYPO3CMSCoreCoreBootstrap;。
- Bootstrap::getInstance()->initializeBackendAuthentication(); 必须在任何需要后端认证的操作之前调用,例如访问数据库或写入缓存。
- 此解决方案适用于 TYPO3 8.7。在其他版本中,可能需要不同的方法来初始化后端认证。
总结
通过在 CLI 脚本中添加 Bootstrap::getInstance()->initializeBackendAuthentication();,可以解决 TYPO3 8.7 中使用 external_import 时遇到的权限和缓存写入错误。这确保了外部导入功能在 CLI 环境下能够正常运行,从而实现自动化数据导入任务。在实际应用中,请根据您的具体需求调整代码,并确保所有依赖项都已正确安装和配置。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END