使用 codeigniter 对多行使用相同的 id

Using same id for multiple rows using codeigniter

我有一个包含三个字段的 table

id (primary key / auto incremented)
product_name
group_id

我的问题是,当我通过表单插入多行时,整组行应该获得相同的 groupId & 它应该在提交时递增 1,因为可以有许多用户同时提交表单。我不知道该怎么做。请帮忙

我的模型

function get_last_group_id() {
            $this->db->select('group_id');
            $this->db->from('mytable');
            $this->db->order_by('group_id', 'DESC');
            $this->db->limit('1');
            $query = $this->db->get();
            return $query->result();
          }

          function save_rows($ids,$product_names,$group_ids){
            $this->db->trans_begin();
            $ndx=0;
            foreach($ids as $id){

            $data = array(
            'id' => $id,
            'product_name' => $product_names[$ndx],
            'group_id' =>$group_ids[$ndx],
            $this->db->insert("product_details",$data);

            $this->db->update($this->table);
            $ndx++;
            }

如果是自动增加的话,也不需要单独获取组id和设置id字段的函数

function save_rows($ids,$product_names){
                    $this->db->trans_begin();
                    $ndx=0;
        $group_id = $this->db->select("MAX(group_id) as group_id")
        ->from("mytable")
        ->get()->row_array();

                    foreach($ids as $id){

                    $data = array(
                    'product_name' => $product_names[$ndx],
                    'group_id' =>$group_id['group_id']+1,
    );
                    $this->db->insert("product_details",$data);

                    $ndx++;
                    }

您可以创建一个新的 table 说 'groups' 字段:id(自动递增)和 group_name (varchar) 随机生成组名

然后在插入产品之前创建一个新组,然后您将获得一个唯一的新组 ID。

然后使用该组 ID,您可以继续选择