Welcome Guest, Not a member yet? Register   Sign In
insert, get ID of insert [FIXED: My fault]
#1

(This post was last modified: 04-13-2020, 09:30 AM by Kaosweaver.)

Tried:
PHP Code:
$order_id $orderModel->insert($data); 

Then tried:

PHP Code:
$orderModel->insert($data);
$order_id $orderModel->insertID(); 

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.
Reply
#2

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?
Reply
#3

(This post was last modified: 04-14-2020, 04:47 AM by Kaosweaver.)

(04-13-2020, 04:06 PM)cyberstunts Wrote: 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?

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(); 
Reply
#4
Thumbs Up 
(This post was last modified: 04-14-2020, 04:53 AM by cyberstunts.)

Thank you, it seems to work both ways.

By $db I meant the database connection e.g.

PHP Code:
$db = \Config\Database::connect();

// insert row

$record_id $db->insertID(); 

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB