【问题标题】:Is there a way to avoid updating all data when using update_batch()?使用 update_batch() 时有没有办法避免更新所有数据?
【发布时间】:2022-01-24 14:37:50
【问题描述】:

我目前的问题是,当我使用update_batch()时,所有数据都在一次更新,有没有办法只更新最后一个数据?

我每学期都会更新成绩,这就是我需要在本学期更新最新成绩的原因。但是,每当我更新成绩时,最新的更新也会更新我学期中以前的成绩,这就是它被覆盖的原因。因此,我只想更新本学期的最新成绩。我还用update_batch更新了多条记录,我这样做错了吗?

这是我的控制器:

        $schoolYear = $csvData[0]['schoolYear'];
        $semester = $csvData[0]['semester'];

        $studentIDFromDB = $this->importSISAcademicInformationGetStudentIDFromDB($schoolYear, $semester);   

            foreach ($csvData as $key => $value) {                      
                if(isset($studentIDFromDB[$value['studentID']])){
                    $updateFields[$key] = array(                                
                        'studentID'             => $value['studentID'],
                        'programType'           => $value['programType'],
                        'level'                 => $value['level'],
                        'sectionDescription'    => $value['sectionDescription'],
                        'schoolYear'            => $value['schoolYear'],
                        'semester'              => $value['semester']                               
                    );
                }
            }

            if(isset($updateFields))
            $this->DBLogic->updateBatchRecordMultipleCondition('tbl_tt_academicinfo', $updateFields, 'studentID', array('schoolYear' => $schoolYear, 'semester' => $semester));

这是我的模型:

public function updateBatchRecordMultipleCondition($table, $fields, $criteria1, $criteriaN){        
    $this->db->where($criteriaN);
    $this->db->update_batch($table, $fields, $criteria1);       
}

【问题讨论】:

  • @RaoDYC 我之前尝试过inser_id(),但它仍然会产生相同的输出
  • 我需要更新的最后一个数据:最后一个数据是什么?数组中的最后一组?插入数据库的最后一个值?数据库中最高的id?请使用此“详细信息”编辑您的问题。 AND:如果您只想更新 1 个记录集,为什么要使用 update_batch?
  • @Vickel - 你好,美好的一天,我已经更新了我的问题,请看一看,希望它能回答你的问题。如果您对我的回答仍有疑问,请告诉我,感谢您抽出宝贵时间。
  • 您不能使用 update_batch,因为它不允许多个 where 条件。它旨在通过单个属性进行更新,这通常是一个键。当您应该插入新成绩时,为什么要从电子表格“更新”,从而覆盖现有成绩?电子表格包含什么?

标签: php codeigniter codeigniter-3


【解决方案1】:

在 Codeigniter 3.x 中,函数 batch_update()as by documentation 的 where 子句设置在函数的第三个参数中,而不是前面的 where() 子句。

在不了解您的变量和数据的其他任何信息的情况下,很可能会这样做:

$this->db->update_batch($table, $fields, $criteriaN); 

无论如何,请确保您遵循这些指示from doc

update_batch($table[, $set = NULL[, $value = NULL[, $batch_size = 100]]])

参数:

  • $table (string) – 表名
  • $set (array) – 字段名称,或字段/值对的关联数组
  • $value (string) – 字段值,如果 $set 是单个字段
  • $batch_size (int) – 在单个查询中分组的条件计数

返回:

  • 更新的行数或失败时为 FALSE

提示:如果您无法在 update_batch() 函数中设置 where 子句,请使用 update() function - 它允许您在 foreach 循环中设置前面的 where()

【讨论】:

  • 您好,当我删除 where 子句时,它显示 One or more rows submitted for batch updating is missing the specified index.,请您进一步帮助我吗?
  • 检查我更新答案的 hint,我多次无法创建简单的 batch_update 并恢复为“正常”更新,您可以更好地单独控制
  • 到目前为止我已经这样做了,$this->db->set($fields); $this->db->where($criteriaN); $this->db->update($table); 但它确实给了我一个错误Message: Array to string conversion
  • 在不了解变量和数据的情况下很难进一步提供帮助,错误表明您使用的是数组而不是字符串,您可以 print_r($ your_variable) 来检查数据结构,然后从那里获取它
  • 我认为您唯一的解决方案是逐个更新它。您的 where clausule 需要多个键,而 CI3 不允许。不确定 CI4
猜你喜欢
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
相关资源
最近更新 更多