Skip to content

Commit

Permalink
Default to real-utf8 when no charset is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 25, 2019
1 parent d1f8a28 commit 5b88b4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ConnectionFactory.php
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
Expand Down Expand Up @@ -43,6 +44,22 @@ public function createConnection(array $params, Configuration $config = null, Ev

$connection = DriverManager::getConnection($params, $config, $eventManager);

if (! isset($params['pdo']) && ! isset($params['charset'])) {
$params = $connection->getParams();
$params['charset'] = 'utf8';
$driver = $connection->getDriver();

if ($driver instanceof AbstractMySQLDriver) {
$params['charset'] = 'utf8mb4';

if (! isset($params['defaultTableOptions']['collate'])) {
$params['defaultTableOptions']['collate'] = 'utf8mb4_unicode_ci';
}
}

$connection = new $connection($params, $driver, $connection->getConfiguration(), $connection->getEventManager());
}

if (! empty($mappingTypes)) {
$platform = $this->getDatabasePlatform($connection);
foreach ($mappingTypes as $dbType => $doctrineType) {
Expand Down
20 changes: 20 additions & 0 deletions Tests/ConnectionFactoryTest.php
Expand Up @@ -49,6 +49,26 @@ public function testContainer()
FakeDriver::$exception = null;
}
}

public function testDefaultCharset()
{
$factory = new ConnectionFactory([]);
$params = ['driverClass' => FakeDriver::class];

$connection = $factory->createConnection($params);

$this->assertSame('utf8', $connection->getParams()['charset']);
}

public function testDefaultCharsetMySql()
{
$factory = new ConnectionFactory([]);
$params = ['driver' => 'pdo_mysql'];

$connection = $factory->createConnection($params);

$this->assertSame('utf8mb4', $connection->getParams()['charset']);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE-2.0.md
Expand Up @@ -9,6 +9,8 @@ PHP and Symfony version support
Symfony 3.4 LTS and 4.1 or newer.
* Support for Twig 1.34 and below as well as 2.4 and below (for 2.x) releases
was dropped.
* When no charset parameter is defined, it now defaults to `utf8mb4` on the
MySQL platform and to `utf8` on all other platforms.

Commands
--------
Expand Down

1 comment on commit 5b88b4b

@ggeorgiev-sfly
Copy link

@ggeorgiev-sfly ggeorgiev-sfly commented on 5b88b4b Dec 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this initializes the connection class twice on each request. Always.
I'm building a custom connection class to support master-slave replication on shards and the constructor does some heavy lifting that I don't see any sense in doing twice.
For some reason the charset is never included in the initial parameter list.

Please sign in to comment.