2017-04-13 53 views
1

我試圖將解析的數據添加到每個產品的描述。Magento SQL錯誤,當試圖添加描述programmaticaly

這就是我:

require 'app/Mage.php'; 
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
$productMage = Mage::getModel('catalog/product'); 

foreach ($productDescXpath as $description) { // Find each item's description 
    if (!$productMage->load($dataId)->getData('description')) { 
     $productMage->load($dataId)->getData('description'); 
     $productMage->setDescription($description->nodeValue); 
     $productMage->save(); 
    } 
} 

在$數據ID我有每個產品的ID和$說明 - >的nodeValue那些描述。

如果我只想用一種產品(例如用真正的產品ID替換$ dataID,例如570),並添加一些字符串而不是$ description-> nodeValue,則它可以工作。

但如果我使用第一建築 - 這就是我得到:

PHP Fatal error: Uncaught exception 'PDOException' with message 
'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update 
a child row: a foreign key constraint fails (`semenin_magento`.` 
catalog_product_entity`, CONSTRAINT 
`FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY 
(`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id)' in 
/home/s/semenin/public_html/lib/Zend/Db/Statement/Pdo.php:228                                               

Stack trace:                                                   
#0 /home/s/semenin/public_html/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)                              
#1 /home/s/semenin/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110):Zend_Db_Statement_Pdo->_execute(Array)   
#2/home/s/semenin/public_html/lib/Zend/Db/Statement.php(300):Varien_Db_Statement_Pdo_Mysql->_execute(Array)   
#3 /home/s/semenin/public_html/lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)                            
#4 /home/s/semenin/public_html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)                    
#5 /home/s/semenin/publi in /home/s/semenin/public_html/lib/Zend/Db/Statement/Pdo.php on line 234                              
+0

你可以給我們$ dataId和$ description變量的日誌嗎? – Jouby

+0

@Jouby,你好。當然,這是$ dataId = 19523 19524 19522 19511 19509 19508 19496,$ in描述了每個$ dataId的一些文本,這對評論很重要,但它是存在的。 – Frunky

+0

Hm ...帶有此Id的項目在'catalog_product_entity'表中不存在 – Frunky

回答

1

我不知道,如果你的$數據ID是數組與否。如果不是,只需使用$dataId = explode(' ', $dataId)

只需更改說明即可,無需加載產品。 加載產品真的很貴,你必須小心使用它。嘗試使用目錄/產品資源模型的saveAttribute

foreach ($dataId as $productId) { 
    $attributeCode = 'description'; 
    $data = Mage::getModel('catalog/product'); 
    $data->setData($attributeCode, $description->nodeValue); 
    $data->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID); 
    $data->setId($productId); 
    $data->isObjectNew(false); 

    Mage::getResourceModel('catalog/product')->saveAttribute($data, $attributeCode); 
} 

在Magento的,這是完全禁止做負載在foreach。如果你不想加載很多實體,使用Magento系列

+0

感謝您的回答,請問我能否幫助我,我如何根據產品的名稱(不是ID)添加說明? – Frunky

+0

您必須按名稱使用集合和過濾器。 – Jouby