在 TYPO3 8.7 中,当尝试通过 CLI 命令行工具,使用 external_import 扩展导入数据时,可能会遇到诸如 “User doesn’t have enough rights for synchronizing table…” 或 “The temporary cache file could not be written” 等错误。这些错误通常与权限验证和缓存写入有关。
问题分析
这些问题通常发生在 CLI 环境下,因为 CLI 环境与后端环境的初始化方式不同。后端环境通常会自动处理权限验证和缓存配置,但在 CLI 环境下,这些都需要手动进行初始化。
- 权限问题: 在 CLI 环境中,默认情况下没有进行后端用户认证,因此 external_import 扩展可能无法获得足够的权限来操作数据库表。
- 缓存问题: external_import 扩展在导入数据时,可能会尝试写入临时缓存文件。如果 CLI 环境没有正确配置缓存目录或权限,则可能导致缓存写入失败。
解决方案
解决这些问题的关键在于在 CLI 脚本中手动初始化后端认证。以下是具体的步骤:
-
引入 bootstrap 类: 首先,在你的 Command 类中引入 TYPO3cmsCoreCoreBootstrap 类。
use TYPO3CMSCoreCoreBootstrap;
-
初始化后端认证: 在你的 Command 类的 execute() 方法中,或者在调用 external_import 之前,调用 Bootstrap::getInstance()->initializeBackendAuthentication() 方法。
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; }
这段代码确保了在执行数据导入操作之前,TYPO3 的后端认证已经正确初始化,从而解决了权限不足的问题。
完整示例代码
以下是一个完整的示例代码,展示了如何在 Command 类中使用 external_import 扩展,并解决权限和缓存问题:
<?php namespace CobwebSomethingCommand; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; use SymfonyComponentConsoleStyleSymfonyStyle; use TYPO3CMSCoreCoreBootstrap; use TYPO3CMSCoreUtilityGeneralUtility; use TYPO3CMSExtbaseObjectObjectManager; class ImportFormationCommand 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(ObjectManager::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); } }
注意事项
- 缓存配置: 确保你的 TYPO3 实例已经正确配置了缓存目录,并且 CLI 脚本具有写入缓存目录的权限。你可以在 typo3conf/LocalConfiguration.php 文件中找到缓存配置。
- 用户权限: 确保执行 CLI 脚本的用户具有足够的权限来操作数据库表。你可以通过 TYPO3 后端管理界面来配置用户权限。
- 错误日志: 如果仍然遇到问题,请查看 TYPO3 的错误日志,以获取更详细的错误信息。错误日志通常位于 typo3temp/var/log/ 目录下。
总结
通过在 CLI 脚本中初始化后端认证,可以有效解决 TYPO3 8.7 中使用 external_import 扩展导入数据时遇到的权限不足和缓存写入失败的问题。确保你的 CLI 脚本具有足够的权限,并且缓存配置正确,可以确保数据导入顺利进行。