Codeigniter Active Record太快了吗?

时间:2013-06-24 13:24:08

标签: javascript jquery codeigniter activerecord

我创建了“管理菜单”,用于翻译我的网页,包含图片,以便您了解相关信息。 picture

这很简单,我将所有内容保存到数据库中,当我进行更改时,我会重新生成所需的文件,例如。 /languages/english/tank_auth_lang.php。每当我想要翻译/编辑某些东西时,我只需点击字段编辑并点击字段外,AJAX就会向我的控制器发送POST,负责插入/更新/添加翻译。

问题

这是我的功能,但每当我快速重新发送(重新编辑/添加新的翻译到表格)时,有时我会在数据库中获得重复。有什么我应该避免做的吗? (比如在发送之前等待或等等)。

如果我进行性能分析,它会向我显示有2个相同的插入(不是在一个AJAX调用中,而是在两个连续的快速 AJAX调用中)。

函数本身起初很复杂,因为只要有比{52}更长的值(strlen()),我就会以不同的方式保存它(我将其保存在结构TEXT不是VARCHAR(524)的表中)。 / p>

功能

public function insert() {
    //this function is used only with AJAX
    if ($this->input->is_ajax_request()) {
        //$this->output->enable_profiler(FALSE); //to work profiler must be turned off

        $id_kw = $this->input->post('id_kw');
        $id_language = $this->input->post('id_language');
        $translation_text = $this->input->post('new_translation');


        if (strlen($translation_text) < '524') {
            //short
            if ($this->general_model->isInDBWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language))){
                //update short
                //get id of text_short and update text field in it
                $text_id = $this->general_model->_getColumnWhere('layout_short', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language));
                $this->general_model->updateRow('text_short', $text_id, array('text' => $translation_text));
                unset($text_id);
            }elseif ($this->general_model->isInDBWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language))) {

                $text_id = $this->general_model->_getColumnWhere('layout_long', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language)); //old text_id
                $this->general_model->_deleteWhere('text_long', array('id' => $text_id));
                $this->general_model->_deleteWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language));
                //insert new entry to short

                $this->_insert($table = 'short', $id_kw, $id_language, $translation_text);
            }else{
                //insert in short
                $this->_insert($table = 'short', $id_kw, $id_language, $translation_text);
            }
        }else{
            //long
            if ($this->general_model->isInDBWhere('layout_long', array('id_keyword' => $id_kw, 'id_language' => $id_language))){
                //update long
                //get id of text_long and update text field in it
                $text_id = $this->general_model->_getColumnWhere('layout_long', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language));
                $this->general_model->updateRow('text_long', $text_id, array('text' => $translation_text));
                unset($text_id);
            }elseif ($this->general_model->isInDBWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language))) {

                $text_id = $this->general_model->_getColumnWhere('layout_short', 'id_text', array('id_keyword' => $id_kw, 'id_language' => $id_language)); //old text_id
                $this->general_model->_deleteWhere('text_short', array('id' => $text_id));
                $this->general_model->_deleteWhere('layout_short', array('id_keyword' => $id_kw, 'id_language' => $id_language));
                //insert new entry to long

                $this->_insert($table = 'long', $id_kw, $id_language, $translation_text);
            }else{
                //insert in short
                $this->_insert($table = 'long', $id_kw, $id_language, $translation_text);
            }
        }
        echo "1";
    }
}

的JavaScript

$(".ajax-translate-field").on("focusout", function(){
    console.log($(this).prop('name'));
    console.log($(this).attr('data-language'));
    console.log($(this).val());
    var this_object = $(this);
    if ($(this).val() === "") {
        this_object.animate({ backgroundColor: '#FF9494', opacity: '0.6'}, "slow");
        this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
    }else{

        $.ajax({
            url: _baseUrl + 'admin/language/insert',
            type: 'POST',
            data: {id_kw: $(this).prop('name'), id_language : $(this).attr('data-language'), new_translation: $(this).val()},
            success: function (result) {
                if (result == 1) {
                    this_object.animate({ backgroundColor: '#BCED91', opacity: '0.6'}, "slow");
                    this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
                }else{
                    //nothing happens here yet
                }
            }
        });
    }

});

1 个答案:

答案 0 :(得分:1)

将您的java脚本更改为

var flag = 0;


$(".ajax-translate-field").on("focusout", function(){
    console.log($(this).prop('name'));
    console.log($(this).attr('data-language'));
    console.log($(this).val());
    var this_object = $(this);
    if ($(this).val() === "") {
        this_object.animate({ backgroundColor: '#FF9494', opacity: '0.6'}, "slow");
        this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
    }else if(flag != $(this).prop('name')){
        flag = $(this).prop('name');
        $.ajax({
            url: _baseUrl + 'admin/language/insert',
            type: 'POST',
            data: {id_kw: $(this).prop('name'), id_language : $(this).attr('data-language'), new_translation: $(this).val()},
            success: function (result) {
                if (result == 1) {
                    this_object.animate({ backgroundColor: '#BCED91', opacity: '0.6'}, "slow");
                    this_object.animate({ backgroundColor: '#ffffff', opacity: '1'}, "fast");
                }else{
                    //nothing happens here yet
                }
            },
            complete: function (result) { flag = 0;}
        });
    }

});

我认为$(this).prop('name')是代码中的唯一键(id)。