Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to real-utf8 when no charset is provided #1070

Merged
merged 1 commit into from Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather use DriverManager::getConnection again rather than assuming the constructor argument here

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the "contract" of all connection classes: the wrapper_class parameter already requires this signature.

}

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