I use mysql5 and utf8 table to store Chinese data. Everything insert into the utf8 tables though cake can be fetch out by cake fine. But when I use other database tools (such as MySQL Query Browser) to query the data which insert by cake will got unreadable characters. I'm so confuse on it, and serach on the internet. I think there is something wrong with the mysql connections setting in cake. In my controller I query the MySQL connection setting by do this
... $msg = $this->MyModel->findBySql("Show VARIABLES LIKE '%CHARACTER_SET%';"); // Then set the result varable $msg to display in views $this->set('msg',$msg); ....
In view: ... debug($msg); ...
Then I got this result: Array ( [0] => Array ( [VARIABLES] => Array ( [Variable_name] => character_set_client [Value] => latin1 )
The character_set_client,character_set_results,character_set_connection is Laten1, and the other is utf8. I think the problem is here. So I continue the test:
When I query the data in the utf8 table by convert the field to laten1 encoding, I can get the right chinese string. The SQL is: SELECT CONVERT(myField USING utf8) from myUtf8Table;
How can I change the database connection setting to solve this problem? Should I modify the core code?
I wish there was more elegant solution, without modifying the core. Perhaps another setting in DATABASE_CONFIG (app/config/database.php), something like:
Andreas: When I add the __construct() function into my model. I got this error:
Fatal error: Call to a member function fetchAll() on a non-object in D:\php-5.0.4-Win32\PHP_Script\PHP_CAKE\cake\libs\model\model_php5.php on line 1143
> Andreas: > When I add the __construct() function into my model. I got this error:
> Fatal error: Call to a member function fetchAll() on a non-object in > D:\php-5.0.4-Win32\PHP_Script\PHP_CAKE\cake\libs\model\model_php5.php > on line 1143
Fatal error: Call to a member function fetchAll() on a non-object in D:\php-5.0.4-Win32\PHP_Script\PHP_CAKE\cake\libs\model\model_php5.php on line 1143
will occur though I add you appmodel in my app_model.php in the cake directory. I think It can't execute any sql statment in model constructor. Because the database connection hasn't be created. So the error occur.
to reduce the number of SET NAMES queries add a static boolean variable to your AppModel an set it to false. Check the value of the variable in your constructor and execute SET NAMES only, if it has not been done before. E.g.
class AppModel extends Model{ static $utf8IsSet = false;
Cake 1.2 now supports this natively. Just add 'encoding' => 'UTF-8' to your database configuration and the querying is taken care of automatically at the database level.
> Cake 1.2 now supports this natively. Just add 'encoding' => 'UTF-8' to > your database configuration and the querying is taken care of > automatically at the database level.