Setting Unicode in Zend_Db and Doctrine with MySQL

by jon on August 26, 2008

When your using a persistence layer like Zend_Db or Doctrine, setting the character set is crucial in order for everything to turn out as expected in your database. I've spent too much time trying to sort character encoding issues in the past, so I'm putting it here as a reference.

In Zend_Db, after configuring your database, in order for arabic / persian / non-ascii characters to persist properly, you need to configure the character set:

 
// setup database
$db = Zend_Db::factory($config->db->adapter, $config_values);
// for Unicode support
$db->query("SET NAMES 'utf8'");
Zend_Db_Table::setDefaultAdapter($db);
 

In Doctrine, a similar declaration is needed to setup the right character set. These lines would be in the config.php file included in the sample project, or whenever you initialize your Doctrine_Manager:

 
$connection = Doctrine_Manager::connection(DB_PATH);
$connection->setCharset('UTF8');
 

{ 1 comment… read it below or add one }

Vyus May 19, 2010 at 12:33 pm

Great tip, thanks!

Leave a Comment

Previous post:

Next post: