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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题