mysql - 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);
}
但是上面的说法没有结果。当我将语句与 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
解决方案
如果有人感兴趣,我找到了一个解决方案:外部表必须映射到“LocalConfiguration.php”中,如下所示——在我的例子中:
'TableMapping' => [
'tableName' => 'Syslog'
]
使用它,您可以像魅力一样使用外部数据库,感谢 TYPO3!
推荐阅读
- java - 有没有办法改变对象内存地址?
- android - 为什么 BasicTextField(在撰写中)不能很好地工作?
- lua - 如何在 Lua 中为 neovim 打开 Google chrome 中的链接?
- midi - 使用
在 MusicXML 中指定 MIDI 力度 - qt - QML 自定义瓷砖服务器
- c++ - 如何保持两个进程之间的连接
- python - 如何使用 QSortFilterProxyModel 进行数据聚合
- react-native - 如何更改其他组件内的反应钩子状态
- parse-platform - Parse Server 将当前文件从 GridFS 迁移到 S3
- php - 如何从原始查询中获取 sql 结果