📜  如果不存在,codeigniter 插入 - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:23.460000             🧑  作者: Mango

代码示例1
function safe_update_batch($table_name,$records,$filter_field)
    {
        $filters=array();

        foreach($records as $record)$filters[]=$record[$filter_field];

        $this->db->query("SET SESSION group_concat_max_len=10000000");

        $query=$this->db->select("GROUP_CONCAT($filter_field) AS existing_keys",FALSE)->where_in($filter_field, $filters)->get($table_name);

        $row=$query->row();
        $found_fields=explode(',',$row->existing_keys);         

        $insert_batch=array();
        $update_batch=array();

        foreach($records as $record)
        {
            if(in_array($record[$filter_field],$found_fields))$update_batch[]=$record;
            else $insert_batch[]=$record;
        }

        if(!empty($insert_batch))$this->db->insert_batch($table_name,$insert_batch);
        if(!empty($update_batch))$this->db->update_batch($table_name,$update_batch,$filter_field);      
    }