jak uzyskać ostatni insert id po Wstaw zapytanie w codeigniter active record

Mam zapytanie insert (active record style) używane do wstawiania pól formularza do tabeli MySQL. Chcę uzyskać ostatni automatycznie zwiększony identyfikator dla operacji insert jako wartość zwracaną mojego zapytania, ale mam z tym pewne problemy.

Wewnątrz kontrolera:

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);
}

I model wewnętrzny:

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

Nic nie dostaję jako zwrot add_post w modelu

Author: Mel, 2013-05-08

5 answers

Spróbuj tego

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

   return  $insert_id;
}

W przypadku wielokrotnych wstawek można użyć

$this->db->trans_start();
$this->db->trans_complete();
 204
Author: Sudz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-25 08:35:26

Transakcja nie jest tu potrzebna, to powinno wystarczyć:

function add_post($post_data) {
    $this->db->insert('posts',$post_data);
    return $this->db->insert_id();
}
 58
Author: Crowlix,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-17 15:32:24
$id = $this->db->insert_id();
 26
Author: Simon Carlson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-08 12:22:12

Z dokumentacji:

$this - > db - >insert_id ()

Numer ID insert podczas wykonywania wstawiania bazy danych.

Dlatego przydałoby się coś takiego:

$lastid = $this->db->insert_id();
 5
Author: Md.Jewel Mia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-21 16:33:54

Musisz użyć $lastId = $this->db->insert_id();

 0
Author: Pawan Kr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-14 05:55:35