php - 保存产品时外键约束错误

标签 php mysql magento foreign-keys constraints

$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]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (glampeu_mage1.mage_catalog_product_entity, CONSTRAINT FK_GALI_CAT_PRD_ENTT_ATTR_SET_ID_GALI_EAV_ATTR_SET_ATTR_SET_ID FOREIGN KEY (attribute_set_id) REFERENCES mage_eav_attribute_set )";i:1;s:2463:"#0 /lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

谁能帮我解决这个问题?

最佳答案

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

关于php - 保存产品时外键约束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916899/

相关文章:

php - 带有选择的下拉列表

php - Echo PHP Swift NSDictionary 对象数组

php - Laravel Eloquent : how to filter multiple and/or criteria single table

mysql - 使用另一个表的主键更新外键列

mysql - Laravel 复选框过滤器 ajax

MySQL if语句条件连接

forms - 没有提交按钮验证 Magento 中的表单元素

php - 如何在 Magento 中管理导航

javascript - PHP 循环中来自 JQuery 代码的 AJAX 请求

Magento 可销售过滤器