insert, get ID of insert [FIXED: My fault] - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: insert, get ID of insert [FIXED: My fault] (/showthread.php?tid=76082) |
insert, get ID of insert [FIXED: My fault] - Kaosweaver - 04-13-2020 Tried: PHP Code: $order_id = $orderModel->insert($data); Then tried: PHP Code: $orderModel->insert($data); Neither returned the inserted ID I don't understand why the first one wasn't built to return this information automatically - but whatever, why didn't the second one work? I did check the database, the record is inserted. I should get the ID of the inserted record, right? **************************** Fixed: First, make sure you're including all of the columns in the model declaration (left off order_id) and, also, don't make the wrong ID the key (also didn't have the right key for the table for the model). So, once those were fixed, it all worked fine. RE: insert, get ID of insert [FIXED: My fault] - cyberstunts - 04-13-2020 Documentation "Query Helper Methods" specifies that the correct usage is: PHP Code: $db->insertID(); and not PHP Code: $MyModel->insertID(); Can you share the code which worked for you? RE: insert, get ID of insert [FIXED: My fault] - Kaosweaver - 04-14-2020 (04-13-2020, 04:06 PM)cyberstunts Wrote: Documentation "Query Helper Methods" specifies that the correct usage is: The code that wasn't working was my insert of the order_id into the order details table (and it was mislabeled columns) - the second code block in my OP was the correct code. I did try exactly what the docs say ($db->insertID(); ) as well and that didn't work ($db isn't a valid variable, but ... I figured why not try) The docs should be updated to reflect it is: PHP Code: $MyModel->insertID(); RE: insert, get ID of insert [FIXED: My fault] - cyberstunts - 04-14-2020 Thank you, it seems to work both ways. By $db I meant the database connection e.g. PHP Code: $db = \Config\Database::connect(); So you should be able to get it within model, as well as using the model. Just my trail of thought. Thanks for the feedback. |