Jump to content

[solved] Get last inserted id of table in prestashop


Recommended Posts

Exactly the same, open class / module .php libraries where you want to get the last inserted id and use this command - don't forget to create own mysql connection - it may be necessary.

 

If you don't want to create new connection, the other way is to override Db class, create new function which will return the last inserted ID

Link to comment
Share on other sites

Hi sabexel2,

Not sure if I understand the question, but in Prestashop all objects will get their 'ID" after being saved into the database. As you manipulate the tables through the objects, Isn't it possible to just check the object ID you are working on? If I totally misunderstood, please elaborate a little.

 

Pascal

Link to comment
Share on other sites

$abd = Db::getInstance()->autoExecute(_DB_PREFIX_.'mobiles', array(

 

'id' => '',

'email' => pSQL($email),

'number'=> pSQL($mobile),

'ip_registration_newsletter'=> pSQL($ip)

 

), 'INSERT');

 

here is my insertion code, now i want the inserted id in the table, which i want to use in the next table, for insertion....

Link to comment
Share on other sites

$abd = Db::getInstance()->autoExecute(_DB_PREFIX_.'mobiles', array(

 

'id' => '',

'email' => pSQL($email),

'number'=> pSQL($mobile),

'ip_registration_newsletter'=> pSQL($ip)

 

), 'INSERT');

 

here is my insertion code, now i want the inserted id in the table, which i want to use in the next table, for insertion....

Link to comment
Share on other sites

  • 1 year later...
  • 2 months later...

Hello, I was wondering the same, how to retrieve the created id after using the add method of objectmodel? for sure after add, it is undefined in the object.

 

Using Db::getInstance()->Insert_ID(); seems un-reliable in case of multi-database usage or even concurrent database operations no?

 

Link to comment
Share on other sites

Thanks for this answer, but i was looking for something that would not require another request :) I don't understand why it doesn't work as it seems to be a big asumption of the objectmodel code. 

 

As a remark, if done with too request, i would rather get the biggest existing ID, increment it manually and force it in the insert, to avoid any issue with concurrent operations happening ( you might get an exception, but at least you see it )

Link to comment
Share on other sites

  • 4 months later...
  • 7 months later...

try to get last entry from selected table

SELECT id FROM table ORDER BY id DESC LIMIT 1 

NEVER, EVER DO THAT!

...to retrieve last insert ID!

If you are not locking the whole table for the whole process (which I hope you don't) and are not in complete control at the same time,

this can and often will lead to bloody collisions, and security issues!

At start, it might seem to work, but imagine shop with many users...

Data of one user could end up in other user's data and the other way around... 

 

So **please, I beg you,** never, ever do something like that... not this way...

 

If the abstract 

Db::getInstance()->Insert_ID()

is not working for you => figure out why, do not do piggy magic...

  • Like 3
Link to comment
Share on other sites

×
×
  • Create New...