简体   繁体   中英

Kohana ORM charset for Spanish accents

I'm having some problem working with ORM in Kohana, specially when I'm trying to insert/update data with special chars (spanish accents) in my Data Base.

Note: My DB is set to UTF-8 Charset, this is not the problem, please continue reading:

This is the function that I'm using to save data in my Model:

class Model_Colaborador extends ORM {

    protected $_table_name = 'colaboradores';

    public function save_data($data, $id = 0)
    {
        if (empty($data))
            exit();

        $date = Date::formatted_time();

        $data['fecha_actualizacion'] = $date;

        if (empty($id))
            $data['fecha_creacion'] = $date;

        $data['estatus'] = 'active';

        if ( ! empty($id))
        {
            $this->where($this->_primary_key, '=', $id)->find();
            if ( ! $this->loaded())
                exit();
        }

        return $this->values($data)->save();

    }

}

To save/update data in my Controller I use this:

public function action_save()
{
     $id = $this->request->param('id');
     $data = $this->request->post();     // From the form

     Model::factory('colaborador')->save_data($data, $id);
}

All works great, but, the big problem is when some field comes with accents, for example: Mamá ( save only Mam?? on my Table, including the two question mark at the end of the word ).

Now, If I'm using the DB class works perfect. For example:

class Model_Colaborador extends ORM {

    protected $_table_name = 'colaboradores';

    public function save_data($data, $id = 0)
    {
        if (empty($data))
            exit();

        $date = Date::formatted_time();

        $data['fecha_actualizacion'] = $date;

        if (empty($id))
            $data['fecha_creacion'] = $date;

        $data['estatus'] = 'active';

        // Only an Insert for the example
        DB::insert($this->_table_name, array_keys($data))->values($data)->execute();

    }

}

Now, the data is saved in the correct format, for example: Mamá ( including the spanish accent ).


Now, I did a "solution" for this, but, I believe there's a way to fix my problem (this is the reason that I'm here). I added to my Model a Filter to fix all the data before save it to the DB, for example:

public function filters()
{
        return array(
            TRUE => array(
                array('trim'),
                array(array($this, 'encoding'))
            ),
        );
}

the callback for the custom filter is:

public function encoding($value)
{
        return mb_convert_encoding($value, 'HTML-ENTITIES', 'UTF-8');  // Using mbstring
}

And the data is saved to my Data Base as: Mam & eacute ; (without the spaces, stackoverflow convert it).

Hope you understand what I'm trying to do. I searched like crazy and this case is pretty strange....

Thanks a lot for read this.

Have a nice day.

Inspect your ORM object inserted values by:

$this->values($data);

var_dump($this);

It's in the end of your save_data method. You should see your assigned (not yet saved data) in private $_object property of an ORM object.

Have you verified the user input is in UTF-8? Sometimes browsers send data encoded in a format that you have not prepared to receive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM