Setting charset to utf8mb4 in the config file

2 months ago

woytam
Member | 11
+
0
-

Hi,

I'm reopening question about removing automatic query SET NAMES 'utf8mb4', as asked in Setting charset to utf8mb4 in the config file

What to set to remove this query?

2 months ago

woytam
Member | 11
+
0
-

I have found a solution, but is a little hacky.
If you properly set your dns connection:
dsn: ‘mysql:host=127.0.0.1;dbname=test;charset=utf8mb4’

you can in your config.neon add charset under options section

database:
    dsn: ....
    options:
        charset: ''

If you set charset to something what eveluates in if statement to false, it will work.

This hack builds on top of this if in Nette\Database\Drivers\MySqlDriver:41

<?php
$charset = isset($options['charset'])
     ? $options['charset']
     : (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
if ($charset) {
     $connection->query("SET NAMES '$charset'");
}
?>