在CodeIgniter Active Record中如何获取插入查询后的最后一个插入ID

180

我有一个插入查询(使用Active Record风格),用来将表单字段插入到MySQL表中。我想要获取插入操作的最后一个自增id作为查询的返回值,但我遇到了一些问题。

在控制器内部:

function add_post(){
    $post_data = array(
        'id'            => '',
        'user_id'   =>  '11330',
        'content'   =>  $this->input->post('poster_textarea'),
        'date_time' => date("Y-m-d H:i:s"),
        'status'        =>  '1'
    );
    return $this->blog_model->add_post($post_data);
}

并且在模型内部:

function add_post($post_data){
    $this->db->trans_start();
    $this->db->insert('posts',$post_data);
    $this->db->trans_complete();
    return $this->db->insert_id();
}

在模型中,我得不到 add_post 的返回结果。


6
对于那些想知道的人,db->insert_id()db->trans_complete()之后会返回false。确保在完成事务之前获取你的insert_id() - pbarney
1
可能是CodeIgniter activerecord, retrieve last insert id?的重复问题。 - Shaiful Islam
1
请有人将其标记为重复。 - kishor10d
10个回答

318

试试这个

function add_post($post_data){
   $this->db->insert('posts', $post_data);
   $insert_id = $this->db->insert_id();

   return  $insert_id;
}

如果有多个插入的情况,您可以使用

$this->db->trans_start();
$this->db->trans_complete();

1
不必要地使用事务。@Crowlix的回答更为简洁。 - Abraham Philip
1
@Abraham 并发插入怎么处理? - Shekhar Joshi
3
据我所知,insert_id() 函数返回由您正在使用的数据库对象执行的最后一个插入的 ID。这应该可以处理并发插入,是吗?如果我说错了,请纠正我。 - Abraham Philip
为什么我们需要将id值传递给$insert_id变量?如果我返回一个函数Ext:return $this->db->insert_id(),CI3能否返回任何值? - DMS-KH
3
@ShekharJoshi 这不是关于对象的问题,CI的insert_id()根据MySQL的last_insert_id()返回最后插入的ID,该函数在每个连接中保留最后插入的ID。因此,对于最后插入的ID,不需要使用事务。 - Sebastianb
显示剩余2条评论

70

这里不需要进行交易,这样就足够了:

function add_post($post_data) {
    $this->db->insert('posts',$post_data);
    return $this->db->insert_id();
}

1
并发插入操作怎么处理? - Pimin Konstantin Kefaloukos
12
我认为insert_id()函数会返回它被调用的数据库对象中最后一次插入操作的id。即使存在并发插入,这是否意味着它总是返回与此特定数据库对象进行的插入相对应的id? - Abraham Philip

29
$id = $this->db->insert_id();

11

根据文档

$this->db->insert_id()

执行数据库插入操作时的插入 ID 编号。

因此,您可以使用类似以下方式的代码:

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

3
请不要只给一个链接,而是在这里尝试总结解决方案。 - abarisone

5
使用mysqli PHP驱动程序,在提交后无法获取insert_id。
真正的解决方案是这样的:
function add_post($post_data){
  $this->db->trans_begin();
  $this->db->insert('posts',$post_data);

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

  if( $this->db->trans_status() === FALSE )
  {
    $this->db->trans_rollback();
    return( 0 );
  }
  else
  {
    $this->db->trans_commit();
    return( $item_id );
  }
}

代码结构来源: https://codeigniter.com/user_guide/database/transactions.html#running-transactions-manually


1

0

因为您已经启动了数据插入事务,所以首先要检查事务是否已完成。一旦开始事务,根据事务的状态应该提交或回滚;

function add_post($post_data){
  $this->db->trans_begin() 
  $this->db->insert('posts',$post_data);
  $this->db->trans_complete();
  if ($this->db->trans_status() === FALSE){
    $this->db->trans_rollback();
    return 0;
  }else{
    $this->db->trans_commit();
    return $this->db->insert_id();
  }
}``

在上述代码中,即使您获得了时间戳,我们也已经提交了成功交易的数据。

0

只是为了完成这个话题: 如果您设置了带有主键和自动递增的表,您可以省略手动递增ID的过程。

请查看此示例

if (!$CI->db->table_exists(db_prefix() . 'my_table_name')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "my_table_name` (
  `serviceid` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `hash` varchar(32) NOT NULL,
  `url` varchar(120) NOT NULL,
  `datecreated` datetime NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

现在你可以插入行了

$this->db->insert(db_prefix(). 'my_table_name', [
            'name'         => $data['name'],
            'hash'            => app_generate_hash(),
            'url'     => $data['url'],
            'datecreated'     => date('Y-m-d H:i:s'),
            'active'          => $data['active']
        ]);

0
**Inside Model**
function add_info($data){
   $this->db->insert('tbl_user_info',$data);
   $last_id = $this->db->insert_id();
   return  $last_id;
}

**Inside Controller**
public function save_user_record() {
  $insertId =  $this->welcome_model->save_user_info($data);
  echo $insertId->id;
}

-1

在编程中,您必须使用 $lastId = $this->db->insert_id();


伙计,你可以使用上面的语句来获取最后插入的ID。 - Pawan Kr
3
重复的答案 - Rohit Dhiman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接