【问题标题】:Foreign key constraint error when saving product保存产品时外键约束错误
【发布时间】:2012-10-16 14:25:14
【问题描述】:
$product = Mage::getModel('catalog/product');
            // Build the product
            $product->setStoreID($store_id);//Store Id
                   $product ->setTotalrooms($post['room'])//No of roms avaliable
                    ->setSku($sku)//product Sku
                    ->setUserid($CusId)//Customer id
                    ->setAttributeSetId(4)
                    ->setTypeId('property')//product type
                    ->setName($post['name'])//propertyName
                    ->setDescription($post['desc'])//Description
                    ->setShortDescription($post['sdesc'])//shortdescription
                    ->setPrice($post['price']) // Set some price
                    ->setAccomodates($post['accomodate'])//Custom created and assigned attributes
                    ->setHostemail($CusEmail)//host email id
                    ->setpropertyadd($post['address'])// property address
                    ->setAmenity($amenity)//amenity like room service,e.t.c
                    ->setState($post['state'])//property state name
                    ->setCity($post['city'])// property city name
                    ->setCountry($post['propcountry'])//country
                    ->setCancelpolicy($post['cancelpolicy'])//regarding to cancelation policy
                    ->setPets($post['pets'])//regaring to pets allowed or not allowed
                    ->setBedtype($post['bedtype'])//bedtype
                    ->setMaplocation($post['map'])//property map location
                    ->setMetaTitle($post['meta_title'])//Meta title
                    ->setMetaKeyword($post['meta_keyword'])//Meta keywords
                    ->setMetaDescription($post['meta_description'])//Meta description
                    ->setPropertytype(array($post['proptype']))//property type
                    ->setPrivacy(array($post['privacy']))//privacy
                    ->setCategoryIds(Mage::app()->getStore()->getRootCategoryId())//Default Category
                    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)//Visibility in both catalog and search
                    ->setStatus(1)//enable the Status
                    ->setTaxClassId(0) # My default tax class
                    ->setStockData(array(
                        'is_in_stock' => 1,
                        'qty' => 100000
                    ))//Inventory
                    ->setCreatedAt(strtotime('now'))
                    ->setWebsiteIDs(array($websiteId)); //Website id, my is 1 (default frontend)
           try {
                    $product->save();
                }
                catch (Exception $ex) {
                    echo $ex->getMessage();
                    exit();
                }

当我保存产品时,它显示如下错误:

a:5:{i:0;s:313:"SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 (glampeu_mage1.mage_catalog_product_entity, 约束 FK_GALI_CAT_PRD_ENTT_ATTR_SET_ID_GALI_EAV_ATTR_SET_ATTR_SET_ID 外键 (attribute_set_id) 引用 mage_eav_attribute_set )";i:1;s:2463:"#0 /lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

谁能帮我解决这个问题?

【问题讨论】:

    标签: php mysql magento foreign-keys constraints


    【解决方案1】:

    您似乎没有设置 attribute_set_id = 4 的属性,因此无法插入或更新您的新产品。检查您的表 ma​​ge_eav_attribute_set 是否为真。

    【讨论】:

      【解决方案2】:

      检查数据库中的密钥,可能存在问题,有时会在更新 magento 后发生。

      【讨论】:

        【解决方案3】:

        试试这个代码

        try {
           //$product->save(); //this method re-trigger all save events
           $product->getResource()->save($product);
        }
        catch (Exception $ex) {
           echo $ex->getMessage();
           exit();
        }
        

        【讨论】:

          【解决方案4】:

          检查您的 attribute_set_id 是否正确。

          【讨论】:

            【解决方案5】:

            这行应该是大写的P

            ->setpropertyadd($post['address'])// property address
            

            应该是这样的

            ->setPropertyadd($post['address'])// property address
            

            【讨论】:

              【解决方案6】:

              Community and Entrprise 的工作方式似乎有些不同,

              在社区中,$product->save();将工作 在企业版中,$product->getResource()->save($product);有效。

              社区在使用后者时似乎不会引发任何约束错误,但企业会。

              【讨论】:

                猜你喜欢
                • 2023-03-31
                • 2011-06-12
                • 1970-01-01
                • 2018-09-15
                • 2023-03-13
                • 2015-04-03
                • 2018-02-19
                • 2018-02-15
                相关资源
                最近更新 更多