[英]codeigniter 2.0 inserting records in two tables at once
I'm not sure how to handle this with active records or using manual sql. 我不知道如何使用活动记录或使用手动sql来处理这个问题。 What I want to do is to add a record into the 'contact' table which has an auto incremented id, and then add a record into the "order" table with the id of the corresponding 'contact' record id.
我想要做的是在“联系人”表中添加一个记录,该记录具有自动增加的id,然后在“order”表中添加一条记录,其中包含相应“联系人”记录ID的id。
I know I can get the id of the inserted contact record using $this->db->insert_id(), but does that only work when using active records? 我知道我可以使用$ this-> db-> insert_id()来获取插入的联系人记录的id,但这只能在使用活动记录时起作用吗?
Also, can I achieve this type of insert in one query, or must I split it up in two? 另外,我可以在一个查询中实现这种类型的插入,还是必须将其拆分为两个?
$this->db
does not refer to Active Record. $this->db
不引用Active Record。 You must choose to use Active Record, $this->db
simply refers to the database abstraction layer. 您必须选择使用Active Record,
$this->db
只是引用数据库抽象层。
That said, you can use $this->db->insert_id()
immediately after a query (Active Record or not) to get its insert ID. 也就是说,您可以在查询(Active Record或不是Active
$this->db->insert_id()
之后立即使用$this->db->insert_id()
)来获取其插入ID。
But to handle inserting multiple records at once that are dependent on eachother's IDs, I would switch to InnoDB as your table type (if you haven't already) and use CodeIgniter's transactions . 但是为了处理依赖于彼此ID的一次插入多个记录,我将切换到InnoDB作为您的表类型(如果您还没有)并使用CodeIgniter的事务 。
Here's an example, adapted from CodeIgniter's documentation: 这是一个根据CodeIgniter文档改编的示例:
$this->db->trans_start();
$this->db->query('INSERT INTO table1 VALUES(id,field1,field2)');
$table1_id = $this->db->insert_id();
$this->db->query('INSERT INTO table2 VALUES(id,' . $table1_id . ',field2,field3)');
$this->db->trans_complete();
According to Codeigniter, the $this->db
is named "Active Record" [sic!] so you already answered your own question in these terms. 根据Codeigniter的说法,
$this->db
被命名为“Active Record”[原文如此],所以你已经用这些术语回答了你自己的问题。 Otherwise you need to actually specify which kind of thing you mean by writing "Active Record" . 否则你需要通过编写“Active Record”来实际指定你的意思。
In PHP the Mysqli extension offers to run multiple queries at once: mysqli::multi_query
Docs . 在PHP中,Mysqli扩展提供了一次运行多个查询:
mysqli::multi_query
Docs 。
The last insert ID function exists in Mysql itself as well and can be used inside any SQL query then, it is called LAST_INSERT_ID()
Mysql Docs . 最后一个插入ID函数也存在于Mysql本身中,可以在任何SQL查询中使用,然后称为
LAST_INSERT_ID()
Mysql Docs 。
INSERT INTO contact;
INSERT INTO order SET contact_ID = LAST_INSERT_ID();
Maybe this is the information you're looking for. 也许这是您正在寻找的信息。
Additional Info 附加信息
Don't forget to Check For success of Transaction: 不要忘记检查交易是否成功:
$this->db->trans_complete();
if($this->db->trans_status() === FALSE){// Check Transaction Result successful
echo "<br>------- INFO WAS >>>>>>> NOT INSERTED <<<<<<< -------<br>";
}else{
echo "<br>------- >>>>>>> TRANSACTION SUCCESSFUL <<<<<<< -------<br>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.