sabexel2 Posted May 5, 2013 Share Posted May 5, 2013 I am using prestashop v1.4, in some custom development i need the last inserted id of the table, how could i get it as simple in php we use mysql_insert_id(); how to use it in prestashop? Link to comment Share on other sites More sharing options...
vekia Posted May 5, 2013 Share Posted May 5, 2013 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 More sharing options...
PascalVG Posted May 5, 2013 Share Posted May 5, 2013 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 More sharing options...
sabexel2 Posted May 5, 2013 Author Share Posted May 5, 2013 Helllo prestashop fanatic, could you please write the complete code, as my insetertion table is 'ts_mobiles' Link to comment Share on other sites More sharing options...
vekia Posted May 5, 2013 Share Posted May 5, 2013 but i must know where you exactly want to retrieve the last inserted ID, and how you insert there own rows? Link to comment Share on other sites More sharing options...
sabexel2 Posted May 5, 2013 Author Share Posted May 5, 2013 $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 More sharing options...
sabexel2 Posted May 5, 2013 Author Share Posted May 5, 2013 $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 More sharing options...
sabexel2 Posted May 6, 2013 Author Share Posted May 6, 2013 Answer is Db::getInstance()->Insert_ID(); 1 2 Link to comment Share on other sites More sharing options...
vekia Posted May 6, 2013 Share Posted May 6, 2013 thanks for the information im convinced that it will be really helpfull for other developers, for me it is! :-) i can go ahead and mark this thread as solved regards Link to comment Share on other sites More sharing options...
hasniou Posted March 9, 2015 Share Posted March 9, 2015 Hello, How to return id after use Save function of object model in PS 1.6 ? thanks Link to comment Share on other sites More sharing options...
hasniou Posted March 9, 2015 Share Posted March 9, 2015 the answer to my question if this can help someone is : Db::getInstance()->Insert_ID(); 3 Link to comment Share on other sites More sharing options...
LouAdrien Posted May 31, 2015 Share Posted May 31, 2015 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 More sharing options...
LouAdrien Posted May 31, 2015 Share Posted May 31, 2015 (edited) Actualy, after checking, the add method rely on Db::getInstance()->Insert_ID(); to fill the new object id, which in my case, doesn't work. Any suggestion? Edited May 31, 2015 by LouAdrien (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted May 31, 2015 Share Posted May 31, 2015 try to get last entry from selected table SELECT id FROM table ORDER BY id DESC LIMIT 1 Link to comment Share on other sites More sharing options...
LouAdrien Posted June 3, 2015 Share Posted June 3, 2015 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 More sharing options...
soduno Posted November 1, 2015 Share Posted November 1, 2015 (edited) try to get last entry from selected table SELECT id FROM table ORDER BY id DESC LIMIT 1 Note: Be carefull with usage of last entry in a table (it can mess up). Always use insert ID (this will return the correct result all time) Edited November 1, 2015 by soduno (see edit history) Link to comment Share on other sites More sharing options...
jave.web Posted June 2, 2016 Share Posted June 2, 2016 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... 3 Link to comment Share on other sites More sharing options...
Recommended Posts