简体   繁体   English

如何使用CodeIgniter插入多个表

[英]How to insert into multiple tables with CodeIgniter

I have two tables that have (1:1) relationship between each others. 我有两个表,彼此之间有(1:1)的关系。

customers table:
 - customerID (PK)(AI)
 - customerName
 - phone

addresses table:
 - customerID (PK&FK)
 - address
 - city
 - zipcode

I tried to update them in the same CodeIgniter view form. 我试图在相同的CodeIgniter视图表单中更新它们。

update_view.php update_view.php

<th>Customer Name:</th>
<td><input type="text" name="customerName"/></td>
<tr>
<th>Customer Phone:</th>
<td><input type="text" name="phone"/></td>
<tr>
<th>Address:</th>
<td><input type="text" name="address"/></td>
<tr>
<th>City:</th>
<td><input type="text" name="city"/></td>
<tr>
<th>Zip Code:</th>
<td><input type="text" name="zipcode"/></td>

This is part of the my controller code: 这是我的控制器代码的一部分:

    public function insert()
        {      
            $this->load->database();
            $this->load->model('my_model');
            $this->my_model->insert_entry();

            $custInsert=$this->my_model->get_all_customers();
            $this->load->view('main_view',array('customer'=>$custInsert));
..
}

Note: up to this point everything was working for process the one table (customers). 注意:到目前为止,一切都在处理一个表(客户)。

This is the part of my model file: 这是我的模型文件的一部分:

function insert_entry()
    {
        $this->customerName   = $_POST['customerName']; 
        $this->phone = $_POST['phone'];
        $this->db->insert('customers', $this); // up to here it was working

        //$customerID=$db->insert_id;
        $customerID=$this->db->query("SELECT MAX(customerID) FROM `customers`");
        $this->customerID;
        $this->address = $_POST['address'];
        $this->city = $_POST['city'];
        $this->zipcode = $_POST['zipcode'];
        $this->db->insert('addresses', $this);
}

As for me the problem is the 'addresses' table need to customerID, but I don't insert it handly (auto_increment). 至于我,问题是'address'表需要customerID,但我不会手动插入(auto_increment)。 I tried to many way for get it after inserted to customers table but I cant do it. 插入到客户表后我尝试了很多方法来获取它,但我不能这样做。 Is there anybody know different way or what should I do in that way? 是否有人知道不同的方式或我应该以这种方式做什么?

Doing this is a bad idea... how is your app going to handle when new customers are being added at nearly the same exact time? 这样做是个坏主意......当几乎在同一时间添加新客户时,您的应用程序将如何处理?

$customerID=$this->db->query("SELECT MAX(customerID) FROM `customers`");

You should get rid of that line and use the method that is preferred and works. 你应该摆脱那条线,并使用首选和有效的方法。 If you do not, it can and will inevitably result in you fetching the wrong record for a customer at some point in time and associating an address with the wrong customer. 如果不这样做,它可能并且将不可避免地导致您在某个时间点为客户取错记录并将地址与错误的客户相关联。

This is because two clients running the code at (nearly enough) the same time might encounter the MAX() at the same point in time, and thus each could get the same value. 这是因为在同一时间(几乎足够)运行代码的两个客户端可能在同一时间点遇到MAX() ,因此每个客户端都可以获得相同的值。 When they both try to save, only one will succeed, and the other will fail due to the primary key constraint. 当他们都尝试保存时,只有一个会成功,另一个会因主键约束而失败。 This situation is known as a race condition and should be guarded against. 这种情况被称为竞争条件 ,应予以防范。

Use this instead: 请改用:

 $this->db->insert_id()

also this: 这个:

$this->customerID;

should be: 应该:

$this->customerID = $this->db->insert_id();

A cleaner way to achieve what you want is to use arrays instead of $this . 实现您想要的更简洁的方法是使用数组而不是$this You will have all the info you need and nothing more. 您将获得所需的所有信息,仅此而已。 There is a lot of stuff in your $this object than you don't need. 你的$this对象中有很多东西比你不需要的东西。

In your controller 在你的控制器中

public function insert()
{      
    $this->load->database();
    $this->load->model('my_model');

    $data_user = array(
      'customerName' => $this->input->post('customerName'),
      'phone'        => $this->input->post('phone')
    );

    $data_address = array(
      'address'    => $this->input->post('address'),
      'city'       => $this->input->post('city'),
      'zipcode'    => $this->input->post('zipcode')
    );    

    $this->my_model->insert_entry($data_user, $data_address);

    [...]
}

In your model 在你的模型中

function insert_entry($data_user, $data_address) {

    $this->db->insert('customers', $data_user);

    $data_address['customerID'] = $this->db->insert_id();

    $this->db->insert('addresses', $data_address);
}

A good practice is also to use the Input Class of Codeigniter when you get your $_POST variables. 当你得到$_POST变量时,一个好的做法是使用Codeigniter输入类 Just use the following : 只需使用以下内容:

$this->input->post('VAR_NAME') instead of $_POST['VAR_NAME'] $this->input->post('VAR_NAME')而不是$_POST['VAR_NAME']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM