Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrestaShop, get ID when insert in DB

I am creating a script for PrestaShop 1.6 that inserts data into a table. My table is made in this way:

  • id: int AUTO INCREMENT
  • desc: string

As I enter a description I would get back the ID value.

On can not use the standard because it blocked by PrestaShop.

I find this a situation:

$sql = "INSERT INTO `"._DB_PREFIX_."table`(`desc`) VALUES ('".$this->desc."')";
$restpo = Db::getInstance()->execute($sql);
var_dump($restpo);

But I have an answer only a boolean. Can you suggest something?

like image 296
Matteo Enna Avatar asked Jan 30 '26 02:01

Matteo Enna


1 Answers

You can use the:

$id = (int)Db::getInstance()->Insert_ID();

For example, in the Cart class:

$last_id = (int)Db::getInstance()->Insert_ID();

I also recommend the use of the function insert, for example in the Carrier class:

$values = array();
foreach ($shops as $id_shop) {
      $values[] = array(
           'id_carrier' => (int)$this->id,
           'id_tax_rules_group' => (int)$id_tax_rules_group,
           'id_shop' => (int)$id_shop,
       );
}
$res = Db::getInstance()->insert('carrier_tax_rules_group_shop', $values);

Then use the Insert_ID to get the last one.

like image 88
sadlyblue Avatar answered Jan 31 '26 14:01

sadlyblue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!