doctrine / DoctrineBundle

Symfony Bundle for Doctrine ORM and DBAL
https://www.doctrine-project.org/projects/doctrine-bundle.html
MIT License
4.72k stars 454 forks source link

Incorrect string value saving emoji #1792

Closed Guervyl closed 7 months ago

Guervyl commented 7 months ago

I have a Symfony project using doctrine. When the text has emoji to insert in the database I get the error An exception occurred while executing a query: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF0\x9F\xA4\x97Gr...' for column ....

I have search the web and the said to change the database charset to utf8mb4 and I did change to utf8mb4_general_ci. The database I'm using is mariadb 10.6.5. I can confirm that the problem is not the database because using phpmyadmin I can execute the insert query successfully and it saves with the emoji. But using Doctrine I keep getting the error.

The stack trace is:

at C:\wamp64\www\invoice-manager\vendor\doctrine\dbal\src\Driver\API\MySQL\ExceptionConverter.php:117
  at Doctrine\DBAL\Driver\API\MySQL\ExceptionConverter->convert(object(Exception), object(Query))
     (C:\wamp64\www\invoice-manager\vendor\doctrine\dbal\src\Connection.php:1938)
  at Doctrine\DBAL\Connection->handleDriverException(object(Exception), object(Query))
     (C:\wamp64\www\invoice-manager\vendor\doctrine\dbal\src\Connection.php:1880)
  at Doctrine\DBAL\Connection->convertExceptionDuringQuery(object(Exception)

And the text is 🤗Gracias por comunicarse....

The real mysql error is An exception occurred while executing a query: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\\xF0\\x9F\\xA4\\x97 G...' for column....

If I remove the emoji it works fine.

greg0ire commented 7 months ago

Invalid datetime format? :thinking:

ostrolucky commented 7 months ago

Sorry this has nothing to do with bundle

Guervyl commented 7 months ago

Actually it is a doctrine error. I leave a video to see a live error https://drive.google.com/file/d/11q61P4phZWWW2McDauut5ilW5ZOMPiBl/view?usp=sharing

ostrolucky commented 7 months ago

Doctrine != doctrine-bundle. We just provide wiring for doctrine/dbal and doctrine/orm. Look at this:

image There are no usages of Doctrine\Bundle, so this is a wrong repository.

But also, this is unlikely a problem in any of the doctrine repositories. This is more like issue with db encoding/configuration. Better to create a stackoverflow post I would say.

stof commented 7 months ago

and also better providing a reproducing case

Guervyl commented 7 months ago

Cool. I just found a fix. I had to pass the query parameters charset=utf8mb4&collation=utf8mb4_unicode_ci to the connection string.