views:

95

answers:

2

Hello !

When I insert some data with Zend_Form to database with non a-z characters like chrząszcz it cuts me this string and in database I have saved only chrz.

Everyting in MySql is set as utf8_general_ci, when connecting with MySql I call SET CHARACTER SET 'utf8', files are also UTF-8.

I have no idea what to do with that.

I wrote also standalone script and it inserts and reads me that string correct. ZendFramework reads it also correct. The problem is only with inserting.

Do anyone know how to fix it ?

UPDATE:

If I insert data with:

$db->query("INSERT INTO unit SET name = 'chrząszcz'");

in ZendFramework it works. Problem is with inserting that way:

$unitTable = new Model_Unit_Table();
$unit = $unitTable->createRow();
$unit->setFromArray($form->getValues());
$unit->save();

UPDATE 2:

Problem is with using Zend_Filter_StringToLower - it modifies string chrząszcz into chrz�szcz.
How to get this filterto work correct ?

A: 

I'm pretty sure this is an encoding problem. Strings dropping off at the point of the first non-standard (i.e. above the ASCII character set) character is most often caused by inserting UTF-8 data in a non-UTF8 context so my first suspicion would be that the encoding of the database connection is not properly set.

  • Can you try $db->query("SET NAMES utf8"); before calling the insertion commands?
  • Is the connection Zend_form uses definitely $db?
  • Are you 100% sure the page your form is in is UTF-8 encoded?
Pekka
1. `SET NAMES utf8` changed nothing. 2. How to check/do it ? 3. Yes, I am 100% sure.
hsz
2. I'm not a Zend man so I don't know, sorry. But I imagine the classes have a `$db` object? --- Can you post a live example of this?
Pekka
+3  A: 

Responding to your comment:

No. var_dump of $form->getValue() gives chrz�szcz. When var_dump a $_POST superglobal it gives correct chrząszcz.

Does this work?

$testArray = array('name' => 'chrząszcz');

$unitTable = new Model_Unit_Table();
$unit = $unitTable->createRow();
$unit->setFromArray($testArray);
$unit->save();

If yes, your problem may be more Zend_Form related.

Edit:

Your filter needs to use mb_strtolower() instead of strtolower().

Edit2:

Try this:

$filter = new Zend_Filter_StringToLower();
$filter->setEncoding('UTF-8');
Karsten
Yes. I discovered problem - look at `UPDATE 2` please.
hsz
Is it possible to set encoding to `UTF-8` permanently for all filters in all forms ?
hsz
I'm sorry but i don't know any builtin method to do this. But you could extend the `Zend_Filter_StringToLower` with your own filter which uses UTF-8 as default encoding.
Karsten
Ok, thank you very much. :)
hsz