【问题标题】:How to divide incoming rows of data to be executed by CodeIgniter's update_batch() and insert_batch()?CodeIgniter的update_batch()和insert_batch()如何划分传入的数据行?
【发布时间】:2020-09-17 21:10:34
【问题描述】:

我的目标是结合使用 CodeIgniter 的 insert_batch()update_batch() 将传入数据添加到我的 macro_plan 表中。

在下面的脚本中,我尝试根据 sr_no 值查询数据库中的现有行,然后适当地调用批处理查询方法。

function insert_batch($dataSet)
{
    $query = $this->db->query("select sr_no from macro_plan");
    $data = $query->result_array();
    $sr_nos=array();

    foreach($data as $key => $value):
        $sr_nos[$key]=$value['sr_no'];
    endforeach;

    $query1= $this->db->query("select * from macro_plan WHERE sr_no IN ('".$sr_nos."')");
    $update_query = $query1->result();
    if ($update_query->num_rows() > 0) {

        $this->db->update_batch($dataSet,$this->macro_plan);//update if ids exist
    } else {
        $this->db->insert_batch($dataSet,$this->macro_plan);//insert if does not exist
    }
}

但是,我收到“数组到字符串转换”错误。

$dataset 将类似于:

Array (
    [0] => Array (
        [quantity_update] => 88
        [sr_no] => 2020-11-1
        [batch] => Batch 2
        [quantity_date_update] => 05-May-20
        [inq_id] => 49
    )
    [1] => Array (
        [quantity_update] => 99
        [sr_no] => 2020-11-2
        [batch] => Batch 1
        [quantity_date_update] => 11-May-20
        [inq_id] => 49
    )
)

我的表结构如下:

【问题讨论】:

  • 第 52 行是哪一行?
  • $query1= $this->db->query("select * from macro_plan WHERE sr_no IN ('".$sr_nos."')");
  • 如何识别表中的唯一性? sr_no 本身就足够了吗,还是需要使用复合键(sr_no AND batch)?您预计$dataSet 中有多少行数据?
  • 是的,sr_no 足以找到唯一性。行数可能不同,大约在 40 到 50 之间。
  • 有时小于 10 - 最大约为 40 -50。

标签: php sql codeigniter activerecord batch-processing


【解决方案1】:
  1. 在您的表中查询包含sr_no 值的预先存在的行,这些值存在于您的$dataSet 中。
  2. 然后将键应用到来自sr_no 值的结果集行——这允许根据旧数据快速查找新数据(以查看是否应该插入相应的新行、作为更新执行还是完全因为数据相同而被忽略。

未经测试的建议:

function insertUpdateMacroPlan($dataSet)
{
    $keyedExistingRows = array_column(
        $this->db
            ->where_in('sr_no', array_column($dataSet, 'sr_no'))
            ->get('macro_plan')
            ->result_array(),
        null,
        'sr_no'
    );

    foreach ($dataSet as $data) {
        if (isset($keyedExistingRows[$data['sr_no']])) {
            // sr_no exists in the db, add known id to new data array
            $identified = ['id' => $keyedExistingRows[$data['sr_no']]['id']] + $data;

            if ($identified != $keyedExistingRows[$data['sr_no']]) {
                $updateBatch[] = $identified;
            }
            // if the arrays contain the same data, the new data will be discarded
        } else {
            $insertBatch[] = $data;
        }
    }

    if (!empty($insertBatch)) {
        $this->db->insert_batch('macro_plan', $insertBatch);
    }
    if (!empty($updateBatch)) {
        $this->db->update_batch('macro_plan', $updateBatch, 'id');
    }
}

附言如果您的业务逻辑要求 sr_no 值是唯一的,我建议您通过将 sr_no 列设置为唯一键来反映在您的表配置中。

【讨论】:

  • 我从来不知道$this->macro_plan 是什么或者你为什么使用它。我应该只写我认为正确的东西。请看我的更新。 (请删除旧的 cmets)
  • 非常感谢您的帮助。它正在工作。我会测试更多场景。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
相关资源
最近更新 更多