我正在为自己学习CI,这就出现了。如果我运行多个INSERT,无论是否作为事务,是否可以获得每个INSERT的插入ID(按照正确的顺序),而不是每次为每个INSERT运行一个$this->db->insert_id()
?
例如
-- Get ID of each
INSERT INTO table VALUES (insert1), (insert2), (insert3)
-- What if they're called separately
INSERT INTO table VALUES (insert1)
INSERT INTO table VALUES (insert2)
INSERT INTO table VALUES (insert3)
是否仍然可以将ID作为数组从这一切中获取?
答案 0 :(得分:0)
据我所知,我认为你必须在每个插页之间插入它,只需将它存储在一个数组中。
var $insert_id = array();
public function insert()
{
INSERT INTO table VALUES (insert1)
$this->insert_id =$this->db->insert_id();
INSERT INTO table VALUES (insert2)
$this->insert_id =$this->db->insert_id();
INSERT INTO table VALUES (insert3)
$this->insert_id =$this->db->insert_id();
}
每次插入数据库记录时,您仍然可以使用insert_id();在每次插入之后,然后将其存储到数组中。那么你就可以做一个函数来返回它。
public function get_inserted_keys()
{
return $insert_id;
}
答案 1 :(得分:0)
很简单
控制器
$ids = array();
$data[0] = array(blah,blah);
$data[1] = array(blah,blah);
$data[2] = array(blah,blah);
for($i=0;$i<3;$i++)
{
$ids[] = $this->my_model->insert($data[$i]);
}
echo '<pre>';
print_r($ids);
模型
public function insert($data)
{
$this->db->insert('table_name',$data);
return $this->db->insert_id()
}