misha
misha

Reputation: 301

Symfony Doctrine: emojis are not stored

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

Upvotes: 5

Views: 3231

Answers (3)

gwelchc
gwelchc

Reputation: 31

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.

Upvotes: 3

Łukasz D. Tulikowski
Łukasz D. Tulikowski

Reputation: 1503

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';

Upvotes: 0

Simon Kerber
Simon Kerber

Reputation: 43

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

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

Upvotes: -1

Related Questions