symfony and doctrine: default table collation, a better solution

by prettyscripts on 2010-03-08 11:09:13

symfony

as of symfony 1.4 and if working with doctrine, there are still problem setting default and correct table encoding and collations with symfony doctrine:build task.

i have previously posted 2 solution. the 1st involves hacking the core code, which needs to be done with every symfony upgrade. the 2nd involves adding options to set default encoding and collation in schema.yml, which has to be done on every table! with the latter, tables created by plugins won't have the correct encoding. you will ended up with a database of tables with mixed collations.

i believe i have found a better solution. add a function to 'config/ProjectConfiguration.class.php':

PHP:

public function configureDoctrine(Doctrine_Manager $manager) {
    $manager->setCollate('utf8_unicode_ci');
    $manager->setCharset('utf8');
}

Tags: collation, database, doctrine, encoding, symfony

8 comments

Comment by Mark @ 2010-04-07 07:29:15
*****
Thank you very much. That's exactly what I was looking for!
Comment by Enjay @ 2010-06-03 19:56:01
*****
This is very neat, thanks!
Comment by Marcos @ 2010-12-22 09:15:24
*****
Thank you very much.
Comment by Hamza @ 2011-01-06 03:51:02
*****
Thank you ! it's a a better solution !!
Comment by Mario Awad @ 2011-01-21 08:25:25
*****
Excellent. Thanks for the tip. Just a small suggestion, use "utf8_general_ci" instead of "utf8_unicode_ci" as it's faster and the accuracy of sorting is not needed in most cases. More info here: http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci

Thanks again. Cheers.
Comment by prettyscripts @ 2011-01-21 10:36:44
@mario, thanks for your feedback. i was working on a project that requires handling of asian language. at the time my research suggested that utf8_unicode_ci is a better option. however your linked info says that it makes no difference. hmmm....
Comment by Nate Michael @ 2011-01-29 10:06:41
***--
The setting doesn't have to be per-table. You can add a global setting for every table at the top of your schema.yml file. Like this:

---- schema.yml
options:
collate: utf8_general_ci
charset: utf8
attributes:
use_dql_callbacks: true

User:
actAs:
Timestampable: ~
SoftDelete: ~
columns:
first_name: { type: string(32) }
last_name: { type: string(32) }
----
Comment by ocsi @ 2011-02-15 00:10:28
*****
That's a good and easy solution!

Nate Michael:
It's an another option, but, in your case the schema.yml sould contain all the tables from the database. ( And some plugin creates new tables, without the global schema.yml.) So you sould merge these partial schema files. ( or in each file set the charset and collate.)

Thanks again!

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)