Select Page
This entry has been published on 2014-07-28 and may be out of date.

Last Updated on 2014-07-28.

Problem

Framework CakePHP sets strings to null which are loaded from database tables and contain special characters like German Umlaute.

Solution

In app/Config/database.php, add to your config array: ‘encoding’ => ‘utf8’

Example:

public $default = array(
		'datasource' => 'Database/Mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'asdf',
		'password' => 'meow',
		'database' => 'bsdf',
		'prefix' => '',
		'encoding' => 'utf8',
	);

It seems like this works even if your DB columns have another charset / collation like latin1_swedish_ci.

Notes

If you are using Ajax, make sure you clean the browser’s cache after modifying the .php file. Otherwise the changes might not be shown immediately.

Basically, it would be also a good idea to use UTF-8 in your DB and all your .php / .ctp files.

  • In MySQL, you have to change the default collation per DB, per table and also the columns themselves. phpMyAdmin is a good choice for doing this. Usually, no problem should appear if you e.g. change from latin to UTF-8, but to be safe create a backup first.
  • Use collation utf8_unicode_ci, NOT utf8_general_ci! It could end in wrong results.
  • For your files, if you use a PHP IDE like NuSphere PhpED: Right near the bar under the code window where the line numbers etc. are shown, you see the charset of the file. Click on it to change it to UTF-8. Windows Notepad has similar functions in the Open/Save dialogs.
  • Make sure you set the correct charset in the header of your PHP files. In CakePHP, UTF-8 is already set in app/Config.core.php.