Dessert #6 - MySql & UTF-8
Posted on 16/9/06 by Felix Geisendörfer
The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.
Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.
Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).
Update: An enhanced version of this post can be found here: Enforce utf8 for multiple db connections.
This is one goes way back to people in the mail group struggling with their UTF-8 data being displayed incorrectly and you've probably already heard about it. The basic problem was that the database itself would manage & store the data in UTF-8 but use a different encoding for the communication with PHP. Soon several approaches on how to fix this behavior were published.
The best (and most portable) one as it turned out, was to execute "SET NAMES 'UTF8'" before any other MySql queries get executed. So the suggestion of putting this logic into the AppModel::__construct() function came up pretty quickely.
The implemenation I am using right now was inspired by a post on the German CakeBakery and also the comment of using a constant made by Reen. The only thing I added was to check if the Model is actually using a table in order to make sure we don't execute this function on a Web Model or another non-DB datasource.
function __construct($id = null, $table = null, $ds = null)
parent::__construct($id, $table, $ds);
if (!defined('MYSQL_SET_NAMES_UTF8') && $this->useTable!==false)
$this->execute("SET NAMES 'UTF8'");
You can skip to the end and add a comment.
You just create a file /app/app_model.php and paste the code from above into it - you're done.
I wrote up something about MySQL and UTF-8. If you want an explanation why you need SET NAMES utf8, have a look at http://www.bluetwanger.de/blog/2006/11/20/mysql-and-utf-8-no-more-question-marks/ :)
It just works...
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
Okay, I understand the concept, not the implementation. What page should I be putting this on or creating to put this on within my (app?) directory?