2016-05-23 23 views
0

我有一个使用CI3在数据库中的配置表。Codigniter更新表中的每一行不同的ID

每一行都有一个唯一的ID以及一个type_name和type_desc。

因此,每行都有一个唯一的ID,我如何使用一个update_batch查询更新整个表?

我显然不希望运行在每一行查询,所以我猜我需要建立一个能与值的数组,但如何能在阵列内的where子句中不知何故做到这一点?困惑。

基本上,我需要做到这一点:

UPDATE check_types 
SET type_name = POSTED_TYPE_NAME, type_desc = POSTED_TYPE_DESC 
WHERE check_type_id = ???? POSTED_ID????? 
+1

要更新的每一行不需要where子句中。只需使用'UPDATE check_types SET type_name = POSTED_TYPE_NAME,type_desc = POSTED_TYPE_DESC'它会更新表 – Saty

+0

的每一行但是这不会更新依赖于ID的行吗? – frobak

回答

0

我知道要做到这一点的唯一方法是延长DB驱动程序。

在名为MY_DB_mysqli_driver.php的核心文件夹中创建文件。文件名假定在config.php中定义的subclass_prefix是MY_

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 

class MY_DB_mysqli_driver extends CI_DB_mysqli_driver 
{ 
    final public function __construct($params) 
    { 
     parent::__construct($params); 
    } 

    /** 
    * Insert_On_Duplicate_Key_Update_Batch 
    * 
    * Compiles batch insert strings and runs the queries 
    * 
    * @param string $table Table to insert into 
    * @param array $set An associative array of insert values 
    * @param bool $escape Whether to escape values and identifiers 
    * @return int Number of rows inserted or FALSE on failure 
    */ 
    public function insert_on_duplicate_update_batch($table = '', $set = NULL, $escape = NULL) 
    { 
     if ($set !== NULL) 
     { 
      $this->set_insert_batch($set, '', $escape); 
     } 

     if (count($this->qb_set) === 0) 
     { 
      // No valid data array. Folds in cases where keys and values did not match up 
      return ($this->db_debug) ? $this->display_error('db_must_use_set') : FALSE; 
     } 

     if ($table === '') 
     { 
      if (!isset($this->qb_from[0])) 
      { 
       return ($this->db_debug) ? $this->display_error('db_must_set_table') : FALSE; 
      } 

      $table = $this->qb_from[0]; 
     } 

     // Batch this baby 
     $affected_rows = 0; 
     for ($i = 0, $total = count($this->qb_set); $i < $total; $i += 100) 
     { 
      $this->query($this->_insert_on_duplicate_key_update_batch($this->protect_identifiers($table, TRUE, $escape, FALSE), $this->qb_keys, array_slice($this->qb_set, $i, 100))); 
      $affected_rows += $this->affected_rows(); 
     } 

     $this->_reset_write(); 
     return $affected_rows; 
    } 

    /** 
    * Insert on duplicate key update batch statement 
    * 
    * Generates a platform-specific insert string from the supplied data 
    * 
    * @param string $table Table name 
    * @param array $keys INSERT keys 
    * @param array $values INSERT values 
    * @return string 
    */ 
    private function _insert_on_duplicate_key_update_batch($table, $keys, $values) 
    { 
     foreach ($keys as $num => $key) 
     { 
      $update_fields[] = $key . '= VALUES(' . $key . ')'; 
     } 
     return "INSERT INTO " . $table . " (" . implode(', ', $keys) . ") VALUES " . implode(', ', $values) . " ON DUPLICATE KEY UPDATE " . implode(', ', $update_fields); 
    } 
} 

你会使用这样的:

$sql_array = array(); 
foreach ($data as $row) 
{ 
    $arr = array(
      'check_type_id' => $row['check_type_id'], 
      'type_name' => $row['type_name'], 
      'type_desc' => $row['type_desc'] 
    ); 
    $sql_array[] = $arr; 
} 
$this->db->insert_on_duplicate_update_batch('check_types', $sql_array);