JSON UTF-8 special chars

Hi,

i have this simple Controller: namespace Controllers; class TypesController extends \Phalcon\Mvc\Controller {

public function indexAction() {
header('Content-Type: application/json; charset=utf-8');
$Types = new \Models\Types;
$response["count"] = $Types::count();
$fetch =  $Types::find("active=1");
foreach ($fetch as $robot) {
    $robot->name = utf8_encode($robot->name);
    $response["fetch"][] = json_decode(json_encode($robot));
    }
return $response;
    }
  }

My database collation is utf-8, in the database connection config I have set the UTF-8 parameter. How can i ensure that every column entry is utf-8 encoded, like $robot->name = utf8_encode($robot->name);. If I had to make that for every column my model would somehow be useless. Also, is there a better way to set the header?

edited Mar '16

What is your exact issue? If your JSON output seems grabbed like:

{"result_code":0,"message":["Le sejour (123) n'est pas associ\u00e9 \u00e0 une fiche s\u00e9jour ou celle-ci n'est pas valide"],"extra_msg":[]}

that's all fine. It's per JSON standards. If you really want to force UTF8 for JSON objects, you need to set:

JSON_UNESCAPED_UNICODE

flag to json_decode() function.

Regarding database settings, in your config / services container you need to set charset like this:

[database]
adapter  = MySQL
host     = 192.168.250.200
socket = /var/run/mysqld/mysqld.sock
username = dbuser
password = dbpass
dbname   = database
charset = UTF8
edited Mar '16

What is your exact issue? If your JSON output seems grabbed like:

``` {"resultcode":0,"message":["Le sejour (123) n'est pas associ\u00e9 \u00e0 une fiche s\u00e9jour ou celle-ci n'est pas valide"],"extramsg":[]} ```

that's all fine. It's per JSON standards. If you really want to force UTF8 for JSON objects, you need to set:

``` JSONUNESCAPEDUNICODE ```

flag to json_decode() function.

Regarding database settings, in your config / services container you need to set charset like this:

``` [database] adapter = MySQL host = 192.168.250.200 socket = /var/run/mysqld/mysqld.sock username = dbuser password = dbpass dbname = database charset = UTF8 ```

Thank you for your answer. Having output like this (\u00e0) would be fine, but all I get is null for every object containing special chars, once I remove "$robot->name = utf8_encode($robot->name);" I want to avoid encoding every column in the Controller. Of course I could store the data encoded right away, but that's something i want to avoid as well.