insert, get ID of insert [FIXED: My fault] |
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.
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?
(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();
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. |
Welcome Guest, Not a member yet? Register Sign In |