如何使用安装程序/更新脚本和 Magento 抽象更新 Magento 产品的值?
我使用安装程序脚本向我的 Magento 应用程序产品实体添加了自定义 eav 属性(基本上,遵循此处描述的过程: 使用您的模块安装自定义属性)。现在,我想使用更新脚本根据某些条件(基于产品类别)更改(填充)每个产品的此属性的值。我尝试使用的脚本本质上是这样的:
$attributeValues = array(...) // Map from $productId to the desired $value
$product = Mage::getModel('catalog/product');
foreach($attributeValues as $productId=>$value){
$product->load($productId)->setMyAttribute($value);
$product->save();
}
我的问题是:在更新脚本中使用这种抽象级别(Mage::getModel('catalog/product') 及其方法)是否可以?如果不是,您建议如何使用更新脚本(不需要 sql)更改这些属性值?
我使用的脚本(到目前为止)还没有工作并且失败并出现错误:
Call to a member function getStoreIds() on a non-object
in a magento core file。
我不知道这个错误是 Magento 错误还是我使用更新脚本的方式有问题。
我正在使用 Magento 1.4.0.1
I added a custom eav attribute to my Magento application product entity using an installer script (Basically, following the procedure described here: Installing Custom Attributes with Your Module). Now, I want to use an update script to change (populate) the values of this attribute for each product according to some criteria (based on the product category). The script I attempted to use was essentially like this:
$attributeValues = array(...) // Map from $productId to the desired $value
$product = Mage::getModel('catalog/product');
foreach($attributeValues as $productId=>$value){
$product->load($productId)->setMyAttribute($value);
$product->save();
}
My questions would then be: Is it ok to use this level of abstraction (Mage::getModel('catalog/product') and its methods) in update scripts? If it isn't, how would you recommend to change these attribute values using update scripts (without requiring sql)?
The script I used (until now) has not worked and failed with the error:
Call to a member function getStoreIds() on a non-object
in a magento core file.
I don't know if this error is a Magento bug or is a problem with how I'm using the update scripts.
I'm using Magento 1.4.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据更新脚本是最佳选择
只需使用数据升级脚本即可。这是放置在
data
文件夹而不是sql
文件夹中的脚本。这些脚本稍后运行,然后数据库结构更新,并允许访问更多功能。示例文件名:
这个 已在 Magento 1.4 中提供。
Data update scripts are the way to go
Simply use a data upgrade script for this. This is a script placed in the
data
folder instead of thesql
folder. Those scripts run later then the database structure updates, and allow access to more functionality.Example file names:
This is already available in Magento 1.4.
尝试在 SQL 升级脚本中添加
Mage::app()->setUpdateMode(false)
。例如,如果您查看
Mage::app()->getStore()
,您将看到以下代码片段,该代码片段返回保存产品所需的不正确存储。Try adding
Mage::app()->setUpdateMode(false)
in your sql upgrade script. e.g.If you look in
Mage::app()->getStore()
you will see the following snippet that returns an incorrect store which is required for saving a product.