Welcome Guest, Not a member yet? Register   Sign In
[Solved] Codeigniter Transactions Does Not Let Me Return Insert ID
#1

(This post was last modified: 02-13-2017, 03:06 AM by wolfgang1983.)

Hello I am using codeigniter database transactions when I inset a new post it does not let me return $this->db->insert_id()

How can I use db transactions and still be able to return the id of new insert?



PHP Code:
 public function insert_post() {
        $this->db->trans_begin();

        $data = array(
            'forum_id' => '1',
            'user_id' => '1',
            'subject' => $this->input->post('subject'),
            'message' => $this->input->post('message'),
            'date_modified' => time(),
            'date_created' => time(),
            'code' => $this->input->post('code')
        );

        $this->db->set($data);
        $this->db->insert($this->db->dbprefix 'post');

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

        return $this->db->insert_id();
 


Controller


PHP Code:
<?php

class Newthread extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('parsedown');
        $this->load->helper('string');
        $this->load->model('catalog/forum/newthread_model');
        $this->load->model('catalog/forum/preview_newthread_model');
    }

    public function index($forum_id NULL) {
        $code random_string('alpha'32);

        $data['forum_id'] = $forum_id;

        $this->form_validation->set_rules('subject''subject''trim|required');
        $this->form_validation->set_rules('message''message''trim|required');

        if ($this->form_validation->run() == TRUE) {

            if ($this->input->post('newthread_preview')) {

                if ($this->preview_newthread_model->check_if_code_set($this->input->post('code'))) { 

                    $this->preview_newthread_model->update_post();

                } else {
                
                    $this
->preview_newthread_model->insert_post(); 
                }
            }

            if ($this->input->post('newthread')) {

                if ($this->newthread_model->check_if_code_set($this->input->post('code'))) { 

                    $this->newthread_model->update_post();

                    redirect(base_url('post') .'/'$this->input->post('post_id'));

                } else {
                
                    $new_id 
$this->newthread_model->insert_post(); 

                    redirect(base_url('post') .'/'$new_id);
                    
                
}
                
            
}

            if ($this->input->post('newthread_delete')) {

                $this->newthread_model->delete_post();

                redirect(base_url('forum'));
                
            
}

        }

        $post_data $this->newthread_model->get_newthread();

        if ($this->input->post('post_id')) {
            $data['post_id'] = $this->input->post('post_id');
        } elseif (!empty($post_data)) {
            $data['post_id'] = $post_data['post_id'];
         } else {
             $data['post_id'] = '';
         }

        if ($this->input->post('code')) {
            $data['code'] = $this->input->post('code');
        } elseif (!empty($post_data)) {
            $data['code'] = $post_data['code'];
         } else {
             $data['code'] = $code;
         }

        if ($this->input->post('subject')) {
            $data['subject'] = $this->input->post('subject');
        } elseif (!empty($post_data)) {
            $data['subject'] = $post_data['subject'];
         } else {
             $data['subject'] = '';
         }

         if ($this->input->post('message')) {
            $data['message'] = $this->input->post('message');
        } elseif (!empty($post_data)) {
            $data['message'] = $post_data['message'];
         } else {
             $data['message'] = '';
         }

         $data['header'] = Modules::run('catalog/common/header/index');
        $data['footer'] = Modules::run('catalog/common/footer/index');
        $data['preview'] = Modules::run('catalog/forum/newthread_preview/index'$post_data);

        $this->load->view('template/forum/newthread_view'$data);
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Hi, I haven't tried it, but would something like this work?

PHP Code:
$this->db->insert($this->db->dbprefix 'post');
$last_insert_id $this->db->insert_id();

...
stuff in-between...

return 
$last_insert_id
Reply
#3

(02-11-2017, 04:16 AM)n2fole00 Wrote: Hi, I haven't tried it, but would something like this work?

PHP Code:
$this->db->insert($this->db->dbprefix 'post');
$last_insert_id $this->db->insert_id();

...
stuff in-between...

return 
$last_insert_id

That worked
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB