I have a problem when adding a second third party Oracle database to the TYPO3 Configuration in a TYPO3 9.5. After adding the configuration I cannot install any extensions (including core extensions) in ExtensionManager anymore.
In LocalConfiguration.php I add a second database (it's the database of a patent management system - no "TYPO3 style"):
'DB' => [
'Connections' => [
'Default' => [
....
],
'MyDB' => [
'charset' => 'utf8',
'dbname' => 'xxx',
'driver' => 'oci8',
'host' => 'xxx',
'password' => 'xxx',
'port' => '1521',
'user' => 'xxx',
],
],
'TableMapping' => [
'SCHEMA.TABLE1' => 'MyDB'
],
'extTablesDefinitionScript' => 'extTables.php',
],
I use QueryBuilder in the action in my controller and I can query the table without any problems.
BUT installing any extension in ExtensionManager leads to the following error:
(1/1) TypeError
Argument 1 passed to TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener::getDatabaseType() must be of the type string, null given, called in C:\inetpub\typo3_src-9.5.7\typo3\sysext\core\Classes\Database\Schema\EventListener\SchemaColumnDefinitionListener.php on line 41
in C:\inetpub\typo3_src-9.5.7\typo3\sysext\core\Classes\Database\Schema\EventListener\SchemaColumnDefinitionListener.php line 93
*
* #param string $typeDefiniton
* #return string
*/
protected function getDatabaseType(string $typeDefiniton): string
{
$dbType = strtolower($typeDefiniton);
$dbType = strtok($dbType, '(), ');
at TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener->getDatabaseType(null)
in C:\inetpub\typo3_src-9.5.7\typo3\sysext\core\Classes\Database\Schema\EventListener\SchemaColumnDefinitionListener.php line 41
public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $event)
{
$tableColumn = $event->getTableColumn();
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
$dbType = $this->getDatabaseType($tableColumn['type']);
if ($dbType !== 'enum' && $dbType !== 'set') {
return;
}
at TYPO3\CMS\Core\Database\Schema\EventListener\SchemaColumnDefinitionListener->onSchemaColumnDefinition(object(Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs))
As soon as I remove the second database from LocalConfiguration.php the ExtensionManager is working again - but of course the QueryBuilder does not know the table any more :enttäuscht:
Related
I am trying to query an additional external database connection in one of my repositories. In LocalConfiguration.php I've defined two connections (Default, External).
[...]
'DB' => [
'Connections' => [
// Local MySQL database
'Default' => [
// ...
],
// External MSSQL database
'External' => [
'charset' => 'utf-8',
'dbname' => 'DBNAME',
'driver' => 'sqlsrv',
'host' => 'someExternalIP',
'password' => 'somePassword',
'port' => 1433,
'user' => 'someUser',
],
],
],
[...]
In my repository I want to query the external database (via Doctrine).
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('dbo.SomeTable');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->select('*')
->from('dbo.SomeTable');
Do I have to explicitly tell the QueryBuilder to use that particular connection? Right now I am getting an Doctrine\DBAL\Exception\ConnectionException error, as the system tries to connect via the Default-Connection.
An exception occurred while executing 'SELECT * FROM `dbo`.`SomeTable`':
SELECT command denied to user 'myLocalUser'#'localhost' for table 'SomeTable'
Check out $GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'] where you can explicitly define what tables are located in which database. See also this for some more details https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Configuration/Index.html
The other option is actually to use ask the Connection by name, and create a query builder out of that.
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('External')->createQueryBuilder(...)
I personally would go with the latter, as it is more explicit within the actual callers code what is used.
To work with external DB, you have to :
configure the mapping with external database and table mapping in LocalConfiguration.php
define the TCA for external tables in myExt/Configuration/TCA/MyExternalTableName.php
configure the external tables/columns mapping in ext_typoscript_setup.txt
and then, the queries in repositories will work.
Sample LocalConfiguration.php :
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'LOCAL-DB',
'driver' => 'mysqli',
'host' => '127.0.0.1',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
'externalDb' => [
'charset' => 'utf8',
'dbname' => 'EXTERNAL-DB',
'driver' => 'mysqli',
'host' => 'localhost',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
],
'TableMapping' => [
'MyexternalTable1' => 'externalDb',
'MyexternalTable2' => 'externalDb',
...
]
]
Sample columns mapping in myExt/ext_typoscript_setup.txt :
plugin.tx_myext {
persistence {
classes {
Vendor\MyExt\Domain\Model\LocalModel {
mapping {
tableName = ExternalTableName
recordType = \Vendor\MyExt\Domain\Model\LocalModel
columns {
col1.mapOnProperty = uid
col2.mapOnProperty = name
...
}
}
}
}
}
}
For a project I use Slim 3 - Twig and Eloquent. On development mode, all works perfectly, but on production, hosted in the OVH shared server, I can't access to the database.
I'm hundred percent sure of credentials and the database was not created right now.
This is my code :
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($config['db']);
$capsule->bootEloquent();
$capsule->setAsGlobal();
Where $config['db'] contains the informations required by Eloquent :
$config = [
'settings' => [
'debug' => true,
'displayErrorDetails' => true
],
'db' => [
'driver' => 'mysql',
'host' => '****.mysql.db',
'database' => '****',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]
]
What I have to do to make it work ?
Ok ... I get the solution.
... Suspense ...
I have to remove spaces around the big arrows in my beautiful formatted array.
Yes. I'm gonna cry.
I tried yii2 and postgres database but when I try to query my table i got an error . How to fix this?
LINK for my error:
LINK for my database connection:
maybe you don't set defaultSchema on connection string
return [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=db_name',
'username' => 'db_username',
'password' => 'db_password',
'charset' => 'utf8',
'schemaMap' => [
'pgsql'=> [
'class'=>'yii\db\pgsql\Schema',
'defaultSchema' => 'public' //specify your schema here
]
], // PostgreSQL
];
see to here
OR
change your tableName() function in model like this
/**
* #inheritdoc
*/
public static function tableName()
{
return 'schemaName.table_name';
}
I can see how to have multiple connections using a configuration file in app/config/database.php and this is well documented.
Is this the only way or can you also define multiple connections using the .env file?
First, you'll need to configure your connections. You need to create a config directory in your project and add the file config/database.php. Like this:
<?php
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead2',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Once you've added your connection configurations, you can access them by getting the database manager object out of the container and calling ->connection('connection_name'). See below for a full example.
<?php
namespace App\Http\Controllers;
use Illuminate\Database\DatabaseManager;
class StatsController extends Controller
{
/**
* #return array
*/
public function getLatest()
{
// Resolve dependencies out of container
/** #var DatabaseManager $db */
$db = app('db');
$database1 = $db->connection('mysql');
$database2 = $db->connection('mysql2');
// Look up 3 newest users and 3 newest blog posts
$threeNewestUsers = $database1->select("SELECT * FROM users ORDER BY created_at DESC LIMIT 3");
$threeLatestPosts = $database2->select("SELECT * FROM blog_posts ORDER BY created_at DESC LIMIT 3");
return [
"new_users" => $threeNewestUsers,
"new_posts" => $threeLatestPosts,
];
}
}
http://andyfleming.com/configuring-multiple-database-connections-in-lumen-without-facades/
Edits/Additions at bottom...
Environment:
Mac OS X 10.6 Snow Leopard
PHP 5.3
Kohana 3.0.4
When I try to configure and use a connection to a postgresql database on localhost I get the following error:
ErrorException [ Warning ]: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)
Here is the configuration of the database in /modules/database/config/database.php (note the third instance named 'pgsqltest')
return array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
/**
* The following options are available for MySQL:
*
* string hostname
* string username
* string password
* boolean persistent
* string database
*
* Ports and sockets may be appended to the hostname.
*/
'hostname' => 'localhost',
'username' => FALSE,
'password' => FALSE,
'persistent' => FALSE,
'database' => 'kohana',
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
'alternate' => array(
'type' => 'pdo',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn
* string username
* string password
* boolean persistent
* string identifier
*/
'dsn' => 'mysql:host=localhost;dbname=kohana',
'username' => 'root',
'password' => 'r00tdb',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
'pgsqltest' => array(
'type' => 'pdo',
'connection' => array(
/**
* The following options are available for PDO:
*
* string dsn
* string username
* string password
* boolean persistent
* string identifier
*/
'dsn' => 'mysql:host=localhost;dbname=pgsqltest',
'username' => 'postgres',
'password' => 'dev1234',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
);
And here is the code to create the database instance, create a query and execute the query:
$pgsqltest_db = Database::instance('pgsqltest');
$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute();
I'm continuing to research a solution for this error but thought I'd ask to see if someone else has already found a solution. Any ideas are welcome.
One other note is that I know my build of PHP can access this postgresql db since I'm able to manage the db using phpPgAdmin. But I have yet to determine what phpPgAdmin is doing differently to connect to the db than what Kohana 3 is attempting.
Bart
///////////// EDIT ONE /////////////
Based on Matt's comment I changed the following in the configuration of the 'pgsqltest' database instance.
from
'dsn' => 'mysql:host=localhost;dbname=pbeeep',
to
'dsn' => 'pgsql:host=localhost;dbname=pbeeep',
I also changed the execution of the query.
from
$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute();
to
$query = DB::query(Database::SELECT, 'SELECT * FROM test')->execute($pgsqltest_db);
Now I get the following error
PDOException [ 0 ]: could not find driver
I'm not sure if this is progress or not but it's more info to share.
My first comment is that you have Kohana configured to use mysql 'type' => 'mysql',. Try updating that for now and get back to us.