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.
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.
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.