2013-06-24 69 views
0

我为我的网页翻译创建了“管理菜单”,包含图片,因此您在图片中。 ​​Codeigniter Active Record记录速度太快?

这很简单,我保存一切到数据库,当我做出改变时,我重新生成所需的文件,例如。 /languages/english/tank_auth_lang.php。每当我想翻译/编辑一些东西,我只需点击字段编辑,然后单击字段外AJAX发送POST到我的控制器,负责插入/更新/添加翻译。

问题

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

如果我做了剖析,它显示有2个相同的插入(不是在一个AJAX调用中,而是在两个连续的快速 AJAX调用中)。

功能本身是很复杂的,因为每当有更长的(strlen())值比524我保存它不同(我把它保存在结构TEXT而不是VARCHAR(524))。

功能

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

请将您的JavaScript发布。 –

+1

如果是两个连续的ajax调用,而不是您的javascript而不是您的控制器 –

+0

,那么您需要为表单分配一种'transaction_id',如果您已经在处理该事务,则忽略它。要计算出transaction_id,您可以散列表单值以获取每个更改的唯一值或页面上的固定值。 – mic

回答

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)。 。

+0

是'$ this.prop('name')'是唯一的id,我会试试这个。 – Kyslik

+0

不,使用您的JavaScript后重复。 – Kyslik

+0

@Kyslik:在'Complete'方法中设置'flag = 0;' – mrsrinivas