5

I am trying to save text with emojis. However emojis are not stored in text. Instead of emojis I get ? in database.

 # Doctrine Configuration
 doctrine:
     dbal:
         driver: pdo_mysql
         host: '%database_host%'
         port: '%database_port%'
         dbname: '%database_name%'
         user: '%database_user%'
         password: '%database_password%'
         charset: utf8mb4

Post class where I want to save emojis

/**
 * Post
 *
 * @ORM\Table(name="post", options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post
    {
    /**
     * @ORM\Column(type="integer", name="id")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="text", nullable=false, name="text")
     */
    private $text;

 }

Example

POST body

 {"text": "test  test"}

text column in database

 test ? test
misha
  • 191
  • 1
  • 4
  • 15
  • Did you manage to solve this by now? Would you mind answering your own question? I have set charset to utf8mb4 on tables and set connection charset to utf8mb4 but till I get ??? instead of emojis. – KoviNET Jan 26 '20 at 12:05

3 Answers3

3

I know it has been a while, but this solved it for me (Symfony 5):

  • On config/packages/doctrine.yaml, under dbal you must add the key-value pair charset: utf8mb4. The configuration is documented here: https://symfony.com/doc/current/reference/configuration/doctrine.html
  • If that doesn't work, you must check that in .env (or .env.local) the DATABASE_URL isn't overwriting the charset. If it is overwriting it, you just change it to utf8mb4.

This is asuming that the database is in utf8mb4. If it's not (and you can't change it) you must escape the emoji characters.

gwelchc
  • 31
  • 5
0

You neet to set character_set_client _connection and _results.

Example MySQL query may look like this

SET character_set_client = 'utf8mb4';
SET character_set_results = 'utf8mb4';
SET character_set_connection  = 'utf8mb4';
Łukasz D. Tulikowski
  • 1,440
  • 1
  • 17
  • 36
-2

You might need to change your MySQL tables to charset=utf8mb4.

How to insert utf-8 mb4 character(emoji in ios5) in mysql?