Encoding database connection in CakePHP

After many CakePHP projects done trying to downplay a little problem, I stopped and face it.
I use to work with database, php files and data in the same charset (UTF-8) to avoid problems. And the default installation of CakePHP its not setting up that parameter. So, from phpMyAdmin I was working in UTF-8 and from CakePHP was reading with a different encoding.

utf-8 charset

The problem explained in the introduction happend because I use to desing database with Dia, and sometimes I'm inserting some records like constant tables. Then I transform the Dia file to a SQL script file with the tool tedia2sql. And finaly executing that SQL into the database with phpMyAdmin. That constants and the strings introduced from phpMyAdmin are read incorrectly from CakePHP so accents (') or ñ characters are missed and strange characters are in their place.

Like it is not so big problem I was trying to avoid it, but in 5 minutes of googling I found the solution and it works perfect, the only thing is when the application is already in use and the data introduced is  already in a different charset than UFT-8. In that case I wouldn't do anything.

The solution is to add the line 11, 'encoding' => 'utf8'.


	var $default = array(
		'driver' =--> 'mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'database_name',
		'encoding' => 'utf8'

I hope this helps someone!

Wed, Jun 27th 2012, 00:07 | CakePHP, Tricks


Add New Comment

Your name:
Your e-mail:
Your web page:
I would like to receive new comment notification.
Security code: