【问题标题】:TYPO3 10: Get data from external database sourceTYPO3 10:从外部数据库源获取数据
【发布时间】:2020-06-13 17:48:25
【问题描述】:

在 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

【问题讨论】:

    标签: mysql typo3 typo3-extensions


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      相关资源
      最近更新 更多