Skip to content

Commit

Permalink
Merge pull request #1070 from nicolas-grekas/utf8default
Browse files Browse the repository at this point in the history
Default to real-utf8 when no charset is provided
  • Loading branch information
alcaeus committed Nov 26, 2019
2 parents d1f8a28 + 5b88b4b commit f6c5c76
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

0 comments on commit f6c5c76

Please sign in to comment.