douqiang4501 2012-08-31 21:01
浏览 49

Kohana ORM charset为西班牙口音

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.

  • 写回答

2条回答 默认 最新

  • doujian7132 2012-09-03 06:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端