2
$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();
                }

When I save the product it shows the error like the below:

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)

Can any one help me to rectify the problem?

4

6 回答 6

2

看起来您没有设置attribute_set_id = 4 的属性,因此您的新产品无法插入或更新。检查您的表mage_eav_attribute_set以查看它是否为真。

于 2012-10-16T14:34:58.127 回答
0

检查您的 attribute_set_id 是否正确。

于 2014-06-04T14:10:50.593 回答
0

try this code

try {
   //$product->save(); //this method re-trigger all save events
   $product->getResource()->save($product);
}
catch (Exception $ex) {
   echo $ex->getMessage();
   exit();
}
于 2013-01-25T05:35:37.853 回答
0

这条线应该有一个大写P

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

所以应该是

->setPropertyadd($post['address'])// property address
于 2015-04-30T08:19:58.790 回答
0

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

于 2012-10-16T15:53:01.310 回答
-1

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

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

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

于 2013-04-12T16:02:28.367 回答