Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMSSerializer and UTF-8 encoding error ( Symfony2.4 )

Tags:

php

utf-8

symfony

I've got REST api which returns tasks/companies/notes/etc. On localhost it works fine, no matter what task description or company name I write, but I put my app on server and when I'm using signs like "łźążćę" there is error Your data could not be encoded because it contains invalid UTF8 characters." at app/endor/jms/serializer/src/JMS/Serializer/JsonSerializationVisitor.php line 36 .

Databases are the same, tables too, all config options are the same. How can I fix this?

like image 761
mmmm Avatar asked Apr 19 '14 13:04

mmmm


2 Answers

I had the same error when I use @VirtualProperty with method which cut string with substr(str, start, length) function. This function is bad choice if you use UTF8. Instead, use mb_substr.

like image 179
GFB Avatar answered Nov 19 '22 10:11

GFB


I found a solution for this problem by setting the database connection properties to transfer data with UTF-8 charset. This is how the doctrine section looks like in my app/config/config.yml

doctrine:
    dbal:
        default_connection: default

        connections:
            default:
                driver:   %database_driver%
                dbname:   %database_name%
                user:     %database_user%
                host:     %database_host%
                password: %database_password%
                charset:  UTF8
                options:
                    1002:  "SET NAMES 'UTF8'"
like image 23
tavi Avatar answered Nov 19 '22 10:11

tavi