'How to access an external database source in a TYPO3 10 module?

Within a TYPO3 10 extension I would like to access an external MySQL-database that contains a table with two fileds like "name" and "firstName".

For this I added the external database to my LocalConfiguration.php like explained here: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/Configuration/Index.html

Now I try to access the database in my Controller with the following statement:

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);
}

Taken from here: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/QueryBuilder/Index.html

But there is no result in the above statement. When I use the statement with the TYPO3-database the results are as expected:

// 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);
}

Is there anybody who could give me a hint how to access a database included with doctrine-dbal? Or use something similar like the PDO-statements of PHP in TYPO3.



Solution 1:[1]

I found a solution if someone is interested: The external table has to be mapped in "LocalConfiguration.php" as shown here – in my case:

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

Using this you are able to use external databases like a charm, thanks TYPO3!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Urs Bo