类别saveAttribute仅保存在存储范围
-
12-12-2019 - |
题
我正在Magento1.7CE中制作一个setupscript,将一些类别数据从一个属性移动到另一个属性。该脚本正在工作,但不知何故,它只保存在存储视图范围(只是存储视图id1),尽管我明确告诉它保存在管理范围。
我当前的代码:
<?php
$installer = $this;
$installer->startSetup();
$categories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('description')
->addAttributeToSelect('text_onder')
->addAttributeToFilter('description_position', 0)
->setStoreId(0)
->addFieldToFilter('entity_id', 93)
;
$resource = Mage::getModel('catalog/category')->getResource();
foreach ($categories as $category) {
$object = new Varien_Object(array(
'entity_id' => $category->getId(),
'description' => '',
'text_onder' => $category->getDescription(),
'store_id' => 0
));
$resource->saveAttribute($object, 'text_onder');
$resource->saveAttribute($object, 'description');
}
$installer->endSetup();
当然,实体id上的过滤器只是为了测试。
属性text_onder是这样创建的:
$installer->addAttribute('catalog_category', "text_onder", array(
'group' => 'General Information',
'type' => 'text',
'input' => 'textarea',
'label' => 'Tekst onder',
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 1,
'visible_in_advanced_search' => 1,
'is_html_allowed_on_front' => 1,
'is_configurable' => 0,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'wysiwyg_enabled' => true,
));
解决方案
<?php
$installer = $this;
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(0)
->addAttributeToSelect('description')
->addAttributeToSelect('text_onder')
->load();
foreach ($categories as $category) {
$category->setTextOnder($category->getDescription());
$category->setDescription('');
$category->setStoreId(0);
$category->save();
}
.
但是,真正的问题是其他地方。您的代码和矿井都不会在安装/升级脚本内工作。您需要将此代码放在数据脚本中!
要执行此操作,请将文件从SQL / module_name / upgrade-x.x.x-y.y.y.php移动到data / module_name / data-upgrade-x.x.x-y.y.y.php,它将像魅力一样工作。
让我知道它是否做了:)。
更新 我已经挖了一下Magento核心一点点,发现了为什么这发生的答案。
让我们看看Magento如何处理安装/升级/数据脚本。
它全部始于MAGE_CORE_MODEL_APP :: RUN方法。此方法调用两个mage_core_model_resource_setup静态方法:- mage_core_model_resource_setup :: applyallupdates() - 这实际上是调用in _initmodules()方法中的in in run()方法,但在此处列出它以简化[行343]
- mage_core_model_resource_setup :: applyAllDataUpdates()[行351]
- 调用mage_core_model_abstract 上的save()
- mage_core_model_abstract在第318行中资源类上的save()调用save()(这是它获取资源类以及您的代码进入其中的地方)。在这种情况下,它调用mage_eav_model_entity_abstract 上的save()
- 作为保存()方法的一部分,mage_eav_model_entity_abstract调用_processsavedata()方法在线1123
- 方法调用mage_catalog_model_resource_abstract 的_updateattribute()方法
- _updateattribute()调用同一类的_saveattributevalue()
- 并且该函数具有导致所有此其中的行(参见下面的代码)
当您查看MAGE_CORE_MODEL_RESOURCE_SETUP中的那些方法(这是MAGE_EAV_MODEL_ENTITY_SETUP的类以及任何其他资源设置类的类),您可以注意到ApplyAllUpdates()从
开始Mage::app()->setUpdateMode(true);
.
和ApplyAllDataUpdates()根本不调用此功能。
现在,让我们回到mage_core_model_app类,它的getStore($ ID)方法:
public function getStore($id = null) {
if (!Mage::isInstalled() || $this->getUpdateMode()) {
return $this->_getDefaultStore();
}
.
因此,当未安装Magento或...更新模式设置为true时 - 它忽略了作为参数给出的$ id,并且始终返回默认存储实例。
从那里向前移动,当您在mage_catalog_model_category上使用save()时,请求如下:
mage_catalog_model_resource_abstract,行219
protected function _saveAttributeValue($object, $attribute, $value)
{
$write = $this->_getWriteAdapter();
$storeId = (int)Mage::app()->getStore($object->getStoreId())->getId();
.其他提示
代码有一些问题。资源模式没有定义在适当的区域.
请试试这个
$categories = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('description')
->addAttributeToSelect('text_onder')
->addAttributeToFilter('description_position', 0)
->setStoreId(0)
->addFieldToFilter('entity_id', 4)
;
foreach ($categories as $category) {
echo $category->getId();
$object =$category->getDescription().'aaaa';
$category->setStoreId(0);
$category->setData('description',$object );
$category->getResource()->saveAttribute($category, 'description');
}
注: $category->setStoreId(0)
使用方法 0 for global/admin
你可以 change 0 to your store according to your code in which store you want to set data