![]() |
Hot to get ID of record inserted using model "save()" method - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Hot to get ID of record inserted using model "save()" method (/showthread.php?tid=74887) |
Hot to get ID of record inserted using model "save()" method - RobT - 11-20-2019 Hi guys, I need to get (automatically generated) ID of record inserted in db using model "save()" method. PHP Code: $product = new Product(); I know the model contains a shared instance of Query Builder but I can't access "db->insertID()" because "db" is a protected property. Any help? Thanks. RE: Hot to get ID of record inserted using model "save()" method - MGatner - 11-20-2019 save() doesn't return the ID but insert() does (comment in Model.php actually notes "call insert directly if you want the ID or the record object"). If you must use save you can use the query helper method "$db->insertID()" https://codeigniter4.github.io/userguide/database/helpers.html Oops, just read your final comment! You should be able to access $model->db because the model class has a magic __get() method to pass through. RE: Hot to get ID of record inserted using model "save()" method - RobT - 11-20-2019 (11-20-2019, 09:53 AM)MGatner Wrote: Oops, just read your final comment! You should be able to access $model->db because the model class has a magic __get() method to pass through. Unfortunately it doesn't seem accessible. RE: Hot to get ID of record inserted using model "save()" method - MGatner - 11-20-2019 What version of the framework are you on? This was change 9/30 to allow accessing protected properties: https://github.com/codeigniter4/CodeIgniter4/commit/de39f729801fbc7d1fbef8d9e58383cbcb364da0#diff-384916434b0b7a276d4abf274120e2c3 |