CGridView uses wrong charset

I use a MySQL database with charset ISO-8859-1 (latin1) because it must handle german umlauts and it must be case-insensitive (which is not possible with utf8). Everything worked fine but now I started to use CGridView in a view and there I get a problem:

When I open the view in the browser the german umlauts are first shown correctly but as soon as I use the pagination or the sorting (click on a link) the special characters are not shown correctly on the next result page. The html page is still ISO-8859-1 encoded so I think it is the database access of the widget.

Any ideas how to solve this problem?? Thanks

Try to define the charset explicitly: CDbConnection::$charset.

Other than that case-insensitive is also possible with utf8. For example try collation utf8_general_ci

I defined the charset for the db connection already explicitly in /protected/config/main.php How can I do that especially for the CGridView Widget?

Well CGridView or to say better the underlying CActiveDataProvider uses the connection you defined in config.

I don’t think it’s mysql problem. Maybe your (view) files are encoded differently or something?

I solved it: I added


'ajaxUpdate'=>false,

to my CGridView and now it works.

But what does that mean? Why is it not possible with ajax? Can somebody explain? What’s the difference?

try to add:


AddDefaultCharset utf-8

to your .htaccess

Great that’s it!!! Thanks a lot. (BTW I used


AddDefaultCharset ISO-8859-1

<_< but that solved it) It’s really tricky on how many different places you might need to configure the charset…

Right, that’s why we’ve compiled a cookbook for it ;)

http://www.yiiframework.com/doc/cookbook/16/

You’re absolutely right. The cookbook is very helpful. In fact, I used it to adjust my charset settings, but at that time it was not necessary to adjust the .htaccess. And now that I ran into troubles again I thought: “I’ve already seen it, so I don’t have to read it again” …

Hey all,

I’m still having problems with this. I’m forced to use ISO-8859-1 because I’m building functionality on top of a monstrous legacy system. I’ve set the encoding in all the places mentioned in the cookbook including the .htaccess file. Now the CGridView search results all appear as they should.

However…

When I search for a multibyte character — for example, if I search for the city Köln, it replaces my search field with köln and doesn’t find the matching record.

Any ideas?

Here’s where I’ve defined the character set:

In config/main.php:


'charset' => 'ISO-8859-1',


...


'db'=>array(

     'charset' => 'latin1',

     'initSQLs'=>array('set names latin1'),

     ...



in the html header:


<meta http-equiv="Content-Type" content="text/html; charset="<?php echo Yii::app()->charset ?>" />

in the .htaccess file:


AddDefaultCharset ISO-8859-1

in the CGridView params:


'ajaxUpdate'=>false,

And I’ve verified that the MySQL charset and collation are both latin1.

Is there something special I need to do in CDbCriteria to get the search to use the correct charset?

Thanks!!!

I met the same problem! I simply added a converting line to the beginning of the search function:




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


        $this->plName=mb_convert_encoding($this->plName, "ISO-8859-1", "UTF-8");




It works fine for me.

Hi,

Those fixes below have worked for us:

In the protected/config/main.php




'charset' => 'ISO-8859-1',

...

 'db'=>array(

  ...

  'charset'=>'utf8',

 ),



At the very first line of any layout:




<?php header('Content-Type: text/html; charset=' . Yii::app()->charset); ?>

...

<head>

<meta http-equiv="Content-Type" content="text/html; charset=<?php echo Yii::app()->charset; ?>" />



And, whenever we send a renderPartial only, I do convert to utf8:




echo utf8_encode($this->renderPartial('//CONTROLLER_NAME/VIEW_NAME', $params, true, true) );



Hope it helps.

Cheers,

Otávio

I used the solution mentionned by otaviofcs but because all my views files (and all my project files) are already encoded in utf-8, i do not mb_convert_encoding them.

I use utf8_decode instead (in my global layout or in any render_partial view) :




	if(Yii::app()->charset == 'ISO-8859-1')

		echo utf8_decode($content); // because all views files are encoded in UTF-8

	else

		echo $content;



This way, i always mention ‘utf8’ (and not utf-8 ) as database charset, so that i manipulate utf-8 strings server side.

At the last step, i check the Yii::app()->charset that i have to use and utf8_decode content to render if i need to.

I can finally easily switch from ISO-8859-1 to UTF-8 if i need to change the output encoded HTTP response.

Hope it could help

I changed the characterset in the head of ‘main.php’ in the view/layouts dir to iso-8859-1. It works fine now.

Thanks, it worked very well for me