在magento错误中设置产品属性值

时间:2015-08-09 19:41:33

标签: magento magento-1.9

我正在编写一个创建新属性(可行)的导入器,现在我想为不同的产品设置不同的属性值,而不使用属性集。一种似乎适用于很多人的方法似乎如下:

Mage::getSingleton('catalog/product_action')
            ->updateAttributes([$id], ['color'=>'red'], self::STORE_ID);

可悲的是,这对我来说是错误的:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attribute_id' in 'where clause'' in /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /vagrant/htdocs/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /vagrant/htdocs/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /vagrant/htdocs/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /vagrant/htdocs/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('DELETE FROM `ca...', Array)
#5 /vagrant/htdocs/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('DELETE FROM `ca...', Array)
#6 /vagrant/htdocs/lib/Zend/Db/Adapter/Abstract.php(664): Varien_Db_Adapter_Pdo_Mysql->query('DELETE FROM `ca...')
#7 /vagrant/htdocs/app/code/core/Mage/Catalog/Model/Resource/Abstract.p in /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php on line 235

有没有更好的方法,或者我只是做错了什么?

2 个答案:

答案 0 :(得分:1)

为了保存产品的属性值,您必须先加载产品对象,然后设置属性值。请参阅下面的示例:

$id = 102; // Your product id    
$product = Mage::getModel("catalog/product")->load($id);
        $product->setStatus(1);  
        $product->setColor("red");
        $product->save(); 

答案 1 :(得分:1)

正如我所知,一种非常好的和高效的方法是使用资源模型:

$product = Mage::getModel('catalog/product');
$product->setId($productId);
$product->setStoreId($storeId);
$product->setColor('red');

// or
// $product->setDataUsingMethod('color', 'red')

$resource = Mage::getResourceModel('catalog/product');
$resource->saveAttribute($product, 'color');