Magento 1 – Programmatically Create Configurable Products

exceptionimportmagento-1

I am trying to create a product importer in Magento. Probably exception is thrown by the line: $product->setConfigurableProductsData($configurableProductsData);. When I comment it, no exceptions are thrown. What is wrong with the array?

$configurableProductsData = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [attribute_id] => 135
                    [label] => 00
                    [value_index] => 48
                    [is_percent] => 0
                    [pricing_value] => 0
                )

            [1] => Array
                (
                    [attribute_id] => 92
                    [label] => Print
                    [value_index] => 511
                    [is_percent] => 0
                    [pricing_value] => 0
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [attribute_id] => 135
                    [label] => 0
                    [value_index] => 48
                    [is_percent] => 0
                    [pricing_value] => 0
                )

            [1] => Array
                (
                    [attribute_id] => 92
                    [label] => Print
                    [value_index] => 511
                    [is_percent] => 0
                    [pricing_value] => 0
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [attribute_id] => 135
                    [label] => 2
                    [value_index] => 49
                    [is_percent] => 0
                    [pricing_value] => 0
                )

            [1] => Array
                (
                    [attribute_id] => 92
                    [label] => Print
                    [value_index] => 511
                    [is_percent] => 0
                    [pricing_value] => 0
                )

        )

)

Exception:

exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`c1_estelles`.`catalog_product_super_link`, CONSTRAINT `FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE C)' in /var/www/clients/client1/web97/web/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /var/www/clients/client1/web97/web/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /var/www/clients/client1/web97/web/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /var/www/clients/client1/web97/web/lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /var/www/clients/client1/web97/web/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)

Best Answer

The error's right there for you to see

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

PHP is telling you it can't perform the SQL you asked it to perform, because a SQL foreign key constraint failed.

If you look at the details of the error, you know this is on the table catalog_product_super_link.

(`c1_estelles`.`catalog_product_super_link`, 

and it's the key named FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID.

CONSTRAINT `FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID` 
FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` 
(`entity_id`) ON DELETE C)' in 

You can look at the table definition if you run the following SQL query.

mysql> SHOW create table catalog_product_super_link;
CREATE TABLE `catalog_product_super_link` (
  `link_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Link ID',
  `product_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Product ID',
  `parent_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Parent ID',
  PRIMARY KEY (`link_id`),
  UNIQUE KEY `UNQ_CATALOG_PRODUCT_SUPER_LINK_PRODUCT_ID_PARENT_ID` (`product_id`,`parent_id`),
  KEY `IDX_CATALOG_PRODUCT_SUPER_LINK_PARENT_ID` (`parent_id`),
  KEY `IDX_CATALOG_PRODUCT_SUPER_LINK_PRODUCT_ID` (`product_id`),
  CONSTRAINT `FK_CAT_PRD_SPR_LNK_PARENT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`parent_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=367 DEFAULT CHARSET=utf8 COMMENT='Catalog Product Super Link Table'    

The specific definition for our key is

CONSTRAINT `FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID` 
FOREIGN KEY (`product_id`) 
REFERENCES `catalog_product_entity` (`entity_id`) 
    ON DELETE CASCADE ON UPDATE CASCADE

In plain english, that's saying "Every product_id in catalog_product_super_link needs to have a corresponding entity_id in catalog_product_entity".

So, there's nothing specifically wrong with your array of information — the problem seems to be in your program logic. My guess would be you're inserting those attributes before the actual product is saved to the catalog_product_entity table.

Related Topic