• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get last insert id

#1
pls how to get the last insert id after save model?

Code:
$emp_model = new \App\Models\Employee\Employeemodel();
           
            $data = [
                'emp_firstname'     => $fname,
                'emp_lastname'      => $lname,
                'emp_code'          => str_replace("-","",$empcode),
                'emp_birthday'      =>  $dob
            ];
           
           
            if ($emp_model->save($data) === false)
            {
                $this->resp_json['msg'] = array($emp_model->errors());
                return $this->response->setJSON( $this->resp_json);
            }
            $this->resp_json['status'] = 'success';
            $this->resp_json['id'] = $emp_model->id;
            $this->resp_json['msg'] = 'New Employee successed';
            return $this->response->setJSON( $this->resp_json);

i try to write code $emp_model->id is return null;

my model like below


Code:
class EmployeeModel extends Model{
    protected $db;
    protected $table      = 'sk_hr_employee';
    protected $primaryKey = 'id';

    protected $returnType = 'array';
    protected $allowedFields = ['id', 'emp_firstname', 'emp_lastname', 'emp_code', 'emp_birthday'];
    protected $useTimestamps = false;

    protected $validationRules    = [
        'emp_firstname'     => 'required|min_length[3]',
        'emp_lastname'     => 'required|min_length[3]',
        'emp_code'        => 'required|is_unique[sk_hr_employee.emp_code]'
    ];

    protected $validationMessages = [
            'emp_code'   => [
                'required' => 'Sorry. Employee Code Must be fill.',
                'is_unique' => 'Sorry. Employee Code already Taken. Please choose another.'
            ],
            'emp_firstname'     =>[
                'required' => 'Sorry. First Name Must be fill.',
                'min_length' => 'Minimal 3 character'
            ],
            'emp_lastname'     =>[
                'required' => 'Sorry. Last name Name Must be fill.',
                'min_length' => 'Minimal 3 character'
            ]
    ];

    protected $skipValidation     = false;



}


thanks
Reply

#2
$model->insertID();
Reply

#3
PHP Code:
$lastInsertID $db->insertID(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
Try to ans, hopefully this solution can solve u're problem:

In CI-3, i do like this:
Field 'Id' => Primary Key and Auto Increment

Code:
//MODEL

function get_table(){
        $table = "tbl_name";
        return $table;
    }

function _insert($data){
        $table = $this->get_table();
        $this->db->insert($table, $data);
    }

function get_max(){
        $table = $this->get_table();
        $this->db->select_max('Id');
        $query = $this->db->get($table);
        return $query;
    }

//CONTROLLER

function bla(){
...
//INSERT NEW DATA:
$this->model_name->_insert($data);
$update_id = $this->model_name->get_max();
$this->resp_json['id'] = $update_id
...

}
Reply

#5
(12-26-2019, 09:55 AM)InsiteFX Wrote:
PHP Code:
$lastInsertID $db->insertID(); 

Thank you let me try,  Smile
Reply

#6
(12-27-2019, 12:08 AM)imam Wrote:
(12-26-2019, 09:55 AM)InsiteFX Wrote:
PHP Code:
$lastInsertID $db->insertID(); 

Thank you let me try,  Smile


Solved

Code:
$this->resp_json['id'] = $emp_model->getInsertID();

Thank you All
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.