首页 > 解决方案 > TYPO3 10:从外部数据库源获取数据

问题描述

在 TYPO3 10 扩展中,我想访问一个外部 MySQL 数据库,该数据库包含一个表,其中包含两个字段,如“name”和“firstName”。

为此,我将外部数据库添加到我的 LocalConfiguration.php 中,如下所述: https ://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/Configuration/Index.html

现在我尝试使用以下语句访问控制器中的数据库:

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;


$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tableName');
$statement = $queryBuilder
    ->select('name')
    ->from('tableName')
    ->execute();
while ($row = $statement->fetch()) {
    // Do something with that single row
    debug($row);
}

取自这里:https ://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/QueryBuilder/Index.html

但是上面的说法没有结果。当我将语句与 TYPO3 数据库一起使用时,结果与预期的一样:

// use TYPO3\CMS\Core\Utility\GeneralUtility;
// use TYPO3\CMS\Core\Database\ConnectionPool;
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
$statement = $queryBuilder
   ->select('uid', 'header', 'bodytext')
   ->from('tt_content')
   ->execute();
while ($row = $statement->fetch()) {
   // Do something with that single row
   debug($row);
}

有没有人可以给我一个提示如何访问包含在教义-dbal 中的数据库?或者使用类似 TYPO3 中 PHP 的 PDO 语句。非常感谢,Urs

标签: mysqltypo3typo3-extensions

解决方案


如果有人感兴趣,我找到了一个解决方案:外部表必须映射到“LocalConfiguration.php”中,如下所示——在我的例子中:

'TableMapping' => [
   'tableName' => 'Syslog'
] 

使用它,您可以像魅力一样使用外部数据库,感谢 TYPO3!


推荐阅读