update_batch and insert in codeigniter



我有两个表。第一个是客户,第二个是customer_tel。每个客户都可以拥有一个或多个tel_number,因此我将客户的电话号码存储在customer_tel中。我的问题是要更新customer_tel。我不知道客户可以拥有多少个Tel_numbers。因此,如果客户已经有tel_number,现在想对其进行编辑或添加新的tel_number如何将更新和插入在一起?例如,客户表是:

id = 1 , fullname : john doe

和customer_tel是:

id = 1 , customer_id = 1 , tel_number = 123456789
id = 2 , customer_id = 1 , tel_number = 123456

每个用户都可以编辑或添加他们的tel_number。我熟悉update_batch,但是在这里它无法正常工作,因为我也在同一时间插入。我的解决方案是删除具有Customer_ID = 1并插入新数据的行。有比这更好的方法吗?

        for($i = 0 ; $i < sizeof($_POST['tel_title']) ; $i++){
        $tel[] = array(
         'customer_id'=> $id,
         'tel_title'=> htmlspecialchars($_POST['tel_title'][$i]),
         'tel' => htmlspecialchars($_POST['tel'][$i])
        );
    }
    $this->base_model->delete_data('customer_tel' , array('customer_id'=> $id));
    $res = $this->base_model->insert_batch('customer_tel' , $tel);

尝试

$this->db->delete('customer_tel' , array('customer_id'=> $id));
$res = $this->db->insert_batch('customer_tel' , $tel);

而不是

$this->base_model->delete_data('customer_tel' , array('customer_id'=> $id));
$res = $this->base_model->insert_batch('customer_tel' , $tel);

最新更新