Jump to content

How to insert product with a specific ID?


selin

Recommended Posts

Hello,

I want to insert products to my new P.S store with their specific IDs. (XML import). Ex: X product's ID is 346, I want to save X product with 346, not autoincrement ID. (ofcourse the IDs are unique.)

How can I do this?

If you wonder, why? My original topic is this.

 

My codes for insert:

$product 				= New Product();
$name 					= (string)$xml->name;
..
bla bla
..
$product->save();

 

Thanks a lot!

Link to comment
Share on other sites

You have to turn off identity insert, insert the records then turn on identity insert back. 

Example:

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT PRIMARY KEY NOT NULL;

--insert commmand here....

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT AUTO_INCREMENT PRIMARY KEY NOT NULL;

 

Edited by phinq1910 (see edit history)
  • Like 1
Link to comment
Share on other sites

23 minutes ago, phinq1910 said:

You have to turn off identity insert, insert the records then turn on identity insert back. 

Example:


ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT PRIMARY KEY NOT NULL;

--insert commmand here....

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT AUTO_INCREMENT PRIMARY KEY NOT NULL;

 

Okay,

Well, can I update a product's ID? Such as: name?

 

Link to comment
Share on other sites

1 hour ago, phinq1910 said:

You have to turn off identity insert, insert the records then turn on identity insert back. 

Example:


ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT PRIMARY KEY NOT NULL;

--insert commmand here....

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT AUTO_INCREMENT PRIMARY KEY NOT NULL;

 

Hmm.. at: phpMyadmin

SQL error:

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT PRIMARY KEY NOT NULL

MySQL error: Belgeler

#1292 - Incorrect date value: '0000-00-00' for column 'available_date' at row 1

UPDATED:

This error is solved, source.

About MySQL 5.7

Link to comment
Share on other sites

3 hours ago, phinq1910 said:

You have to turn off identity insert, insert the records then turn on identity insert back. 

Example:


ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT PRIMARY KEY NOT NULL;

--insert commmand here....

ALTER TABLE ps_product DROP PRIMARY KEY,
MODIFY id_product INT AUTO_INCREMENT PRIMARY KEY NOT NULL;

 

Opps..

There is an error :

INSERT QUERY does not have id_product 

INSERT INTO `ps_product` 

(`id_shop_default`, `id_manufacturer`, `id_supplier`, `reference`, `supplier_reference`, `location`, `width`, `height`, `depth`, `weight`, `quantity_discount`, `ean13`, `isbn`, `upc`, `cache_is_pack`, `cache_has_attachments`, `is_virtual`, `state`, `additional_delivery_times`, `id_category_default`, `id_tax_rules_group`, `on_sale`, `online_only`, `ecotax`, `minimal_quantity`, `low_stock_threshold`, `low_stock_alert`, `price`, `wholesale_price`, `unity`, `unit_price_ratio`, `additional_shipping_cost`, `customizable`, `text_fields`, `uploadable_files`, `active`, `redirect_type`, `id_type_redirected`, `available_for_order`, `available_date`, `show_condition`, `condition`, `show_price`, `indexed`, `visibility`, `cache_default_attribute`, `advanced_stock_management`, `date_add`, `date_upd`, `pack_stock_type`) 

VALUES 

('1', '0', '2', '', '2', '', '0', '0', '0', '0', '0', '', '', '', '0', '0', '0', '1', '1', '0', '1', '0', '0', '0', '1', NULL, '0', '4', '0', '', '0', '0', '0', '0', '0', '1', '', '0', '1', '0000-00-00', '0', 'new', '1', '0', 'both', '0', '0', '2018-12-10 15:50:17', '2018-12-10 15:50:17', '3')

THEN

I updated classes/Product.php
 

/** @var int default Shop id */
    public $id_product;
...

bla

..

 'fields' => array(
            /* Classic fields */
            
            'id_product' =>                    array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_shop_default' =>            array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_manufacturer' =>            array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),

Then,

I saved. but not listed in B.O Products page. Because, product's id is 1 at ps_product but,  0 at ps_product_lang table.

Any ideas? 

Edited by selin (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...