Data Conversion

Hi all,

i have a problem with cgridview and db coded in iso-8859-2.

So i want to convert data from db to utf-8 before it takes a htmlspecialchars function.

Where is the best place to do it ?

Or is there any better solution ?

I cannot convert db to utf-8 because of another programs that use it …

Thanks

Bego

I suggest you to add afterFind() in your AR model that represents your database table and do all encoding conversion there:





// this should be placed inside you AR class


protected afterFind()

{

    foreach($this->attributes as $k => $attribute)

    {

        $this->$k = iconv("ISO-8859-2", "UTF-8", $text);

    }


    parent::afterFind();

}




More information about afterFind : http://www.yiiframework.com/doc/guide/1.1/en/database.ar#customization .

More infromation about iconv: http://php.net/manual/en/function.iconv.php .

I agree with the above. You could wrap that code in a behavior to reduce code duplication if you need to do it in multiple models.

Thanks all. I’ll try it today.

Hello,

At least for MySQL if you have table in another charset and you have connection in UTF8 then MySQL will do the encoding and decoding for you automatically.

have this in your db section config:


'charset' => 'utf8',

if this does not work for you then the data in your tables is not actually ISO-8859-2 but encoding mess that is most likely caused by the another programs not setting the charset (latin1 is used for default)

If it’s Mysql database then you’ve - log in hosting account details

  • Type this command in command line mysql -u username -p

at the place of username mention your username

  • Enter correct password

  • To display current character set type the command mysql>prompt

SELECT default_character_set_name FROM information_schema.SCHEMATA S WHERE schema_name = "DBNAME";

  • To display current character encoding set for a particular database, type the command mysql>prompt

SELECT CCSA.character_set_name FROM information_schema.TABLES T,information_schema.COLLATION_CHARACTER_SET_APPLICABILITY CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "DBNAME" AND T.table_name = "TABLENAME";