Get Last Id before insert in dbtable

Hello,

Can anyone help me out, Actually I want the last inserted id in my application before insert / save.
Please help me out. Your reply will be noticable and appreciable.

Well, this is the way according to the book:
https://book.cakephp.org/3.0/en/orm/saving-data.html#inserting-data

If you want the last inserted ID between requests, then you could use something like this, but beware that is might cause issues (getting the wrong id) when handling multiple concurrent requests:

$id = $this->MyModel->find()->select(['id'])->last();

You want to know the highest ID that exists in the database before doing your own insert? Why? If it’s to set the new to-be-saved ID manually because you’re not using an autoincrement column, that’s a terrible idea because of race conditions.

1 Like

Thanks for your answer.

I know, but sometimes according to project we need to do . Sorry but it is required here to do so. Thanks for your concern.

Are you integrating with some third-party database that unfortunately doesn’t have autoincrement? If so, might it be an option to create an auxiliary table that has nothing but an autoincrement id? You would initialize the autoincrement to whatever the third-party table is at. Then you can create an empty entity of that type, save it, get the id back from the entity, and use that as the id for the other table. Race condition resolved, without changing anything about the third-party schema.

Hello Finlay,

$id = $this->MyModel->find()->select([‘id’])->last();
Here I am giving my ModelName but it is giving me error like

Error: Call to a member function save() on boolean

[ Notice (1024)](javascript:void(0);): Undefined property: BeneficiariesController::$PaymentLedger.

Actually I am inserting data using connectionManager like below

if($insPaymentLedger){
$insPaymentEntry = $connection->insert(‘payment_entries’,[
‘id’ => Text::uuid(),
‘entry_level’ => $entry_level,
‘status’ => ‘Done’
]);
}
$result= $this->MyModelName->save();
$id = $result->id;

But I am getting error. pLease help me out on this as soon as possible.

Thanks in advance.

is your model loaded?

public function initialize() {
  parent::initialize();
  
  // Load model
  $this->loadModel('MyModel');
}

please note that last is collection https://book.cakephp.org/3.0/en/core-libraries/collections.html#Cake\Collection\Collection::last method not query, so when you

$this->Users->find()->select(['id'])->last();

it generates

Debug: duration=0 rows=4 SELECT Users.id AS `Users__id` FROM users Users

so you get ALL rows and collection just gives you last one

EDIT for clarification:
first is https://api.cakephp.org/3.8/class-Cake.Datasource.QueryTrait.html#_first

Hello Finlay,

I am still having error like below I am also giving you my code snippet :-1:
[ Notice (1024)](javascript:void(0);): Undefined property: BeneficiariesController::$PaymentLedgers

CODE SNIPPET

At the top in class

public function initialize() {
parent::initialize();
$this->loadModel(‘PaymentLedger’);
}

In my function where I need just after insert
echo $lId = $this->PaymentLedgers->find()->select([‘id’])->last();die;
$result= $this->PaymentLedger->save();
echo $id = $result->id;die;

In both I am having error . I have given you my isnert query earlier.

Please help me out on this.
I will be thankful for this.

Your code is mixing PaymentLedger and PaymentLedgers. Which is it? You need to be consistent.