I am using Doctrine with Symfony2. My config.yml file looks something like this:-
doctrine:
dbal:
driver: %data
Use code below to set collation, engine and charset (annotation example):
/**
* @ORM\Table(
* name="temporary",
* options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"}
* )
* @ORM\Entity
*/
Source: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-column
I suggest you try add this setting to your Doctrine configuration:
options:
1002: "SET NAMES 'UTF8' COLLATE 'utf8_unicode_ci'"
So it looks like this:
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
options:
1002: "SET NAMES 'UTF8' COLLATE 'utf8_unicode_ci'"
As a reference Doctrine Configuration: http://symfony.com/doc/2.0/reference/configuration/doctrine.html#reference-dbal-configuration
And in case you are using MySql: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
BTW. I had problems with displaying polish characters and adding only set names without collation (as below) helped.
options:
1002: "SET NAMES 'UTF8'
I've got this problems with fetching data which includes polish characters from database. It looks like:
effects of displaying data from db
my config.yml is like:
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
options:
1002: "SET NAMES 'UTF8'"
and I've been searching for an answer for couple of hours - Im so tired of this. "1002: SET NAMES 'utf8'" Doesnt work for me. But when I tryed to access my db from simple php script (out of symfony) the effect was the same but when I added line:
mysql_query("SET NAMES 'utf8'");
it worked properly. So it seems to be little strange. All the tables in my db have 'utf8_unicode_ci' set.
I met the same problems. By search the code, I find the code in vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
if ( ! isset($options['collate'])) {
$options['collate'] = 'utf8_unicode_ci';
}
So I changed it to 'utf8_general_ci', and it worked. All of tables' collation changed to utf8_general_ci
after rebuild, but I still have one question: when I changed the annotations, I got the following error message:
[Creation Error] The annotation @ORM\Table declared on class Gvsun\UserBundle\Entity\User does not have a property named "collation". Available properties: name, schema, indexes, uniqueConstraints, options
I search the documentation of Doctrine, but I didn't find the 'option' property, can someone tell me how to use options property, I think it might be able to change collate in annotations settings?
I have successfully used options: collate
in YML configuration of Symfony 2.7.
MyBundle\Entity\Company:
type: entity
repositoryClass: MyBundle\Repository\CompanyRepository
table: company
options:
collate: utf8_general_ci
if you are looking for a way to have migrations be created correctly you should setup the connection
you can set the default_table_options connection option to achieve this: in symfony this is done through:
doctrine:
dbal:
default_table_options:
charset: utf8
collate: utf8_general_ci
for those looking to do it in plain doctrine this translates to doctrine connection option defaultDatabaseOptions, and is passed to the entity manager together with your database credentials etc:
[
...
'driver' => ...
'user' => ...
...
'defaultDatabaseOptions' => [
'charset' => 'utf8',
'collate' => 'utf8_general_ci'
]
]