Magento 以编程方式保存属性值会导致 SQL 错误
有人可以帮忙吗?
在 Magento 1.5.0.1 中,我试图保存可配置产品的属性值。我使用 Admin 将我的属性添加到相应的产品中。
我查看了 使用 magento 自动修改属性 和 以编程方式在商店视图级别更改产品属性,因此能够提出看似简单的代码,不幸的是,它仅有时有效 - 即对于没有等级价格的可配置产品...
这是代码片段:
$id = 126; # id of existing product
$product = Mage::getModel('catalog/product')->load($id);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->setx_pos(123);
// tried this too, does not make a difference: $product->setData('x_pos', 123);
$product->save();
如果可配置产品没有具有等级价格的产品,则它可以完美地工作。它可以有没有分级价格的分级产品,在这种情况下可以节省工作。如果分级产品有分级价格 - 比我得到的例外是:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '126-1-0-5.0000-0' for key 'UNQ_CATALOG_PRODUCT_TIER_PRICE'
Trace:
#0 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1513): Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/afterSa...', Array)
#1 C:\magento\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product.php(142): Mage_Eav_Model_Entity_Abstract->_afterSave(Object(Mage_Catalog_Model_Product))
#2 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1013): Mage_Catalog_Model_Resource_Eav_Mysql4_Product->_afterSave(Object (Mage_Catalog_Model_Product))
#3 C:\magento\app\code\core\Mage\Core\Model\Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#4 C:\magento\app\design\frontend\default\OneOffFit\template\page\data.phtml(90): Mage_Core_Model_Abstract->save()
我在谷歌上搜索了这个错误 - 唯一的参考是未回答的问题。
在这种情况下,为什么 Magento 会更新等级价格?我做错了什么吗?我应该首先更新关联产品(它们具有相同的属性但未设置值)吗?或者更确切地说,我应该做什么?有人可以帮忙吗?
Could somebody please help?
In Magento 1.5.0.1 I am trying to save an attribute value for configurable product. I added my attribute to corresponding products using Admin.
I looked at Modify automatically an attribute with magento and Programmatically change product attribute at store view level, so was able to come up with seemingly simple code which works unfortunately only sometimes - namely for configurable products without tier prices...
Here is a snippet of code:
$id = 126; # id of existing product
$product = Mage::getModel('catalog/product')->load($id);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->setx_pos(123);
// tried this too, does not make a difference: $product->setData('x_pos', 123);
$product->save();
It works perfectly IF configurable product does not have products with tier prices. It can have tier products without tier prices, it this case save works. If tier products have tier prices – than I am getting the exception:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '126-1-0-5.0000-0' for key 'UNQ_CATALOG_PRODUCT_TIER_PRICE'
Trace:
#0 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1513): Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/afterSa...', Array)
#1 C:\magento\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product.php(142): Mage_Eav_Model_Entity_Abstract->_afterSave(Object(Mage_Catalog_Model_Product))
#2 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1013): Mage_Catalog_Model_Resource_Eav_Mysql4_Product->_afterSave(Object (Mage_Catalog_Model_Product))
#3 C:\magento\app\code\core\Mage\Core\Model\Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#4 C:\magento\app\design\frontend\default\OneOffFit\template\page\data.phtml(90): Mage_Core_Model_Abstract->save()
I Googled on this error - the only refrence is unanswered question.
Why is Magento updating tier prices in this case? Am I doing anything wrong? Should I update associated products first (they have same attribute but with unset value)? Or rather what should I do right? Could anybody help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此行移至加载命令上方;
或者,如果您不关心商店特定的值,请将其完全删除。
Move this line above the load command;
Or if you're not concerned with store-specific values remove it altogether.
就我而言,当我将以下代码放在产品负载上方时,错误消失了,但浏览器需要很长时间才能停止。最后属性也没有更新。
知道代码发生了什么吗?
in my case when I put following code just above the product load, the error is gone but browser is taking so long time to stop. Finally the attribute is also not updated.
Any idea what is happening to the code?