在不加载类别模型的情况下更新类别属性的方法
-
13-12-2019 - |
题
当更新产品属性时,此方法就足够了,这样就可以防止加载整个产品模型:
Mage::getSingleton('catalog/product_action')->updateAttributes(
array(1,2,3),
array('some_attribute'=>'some value',
'some_attribute_2'=>'some value',
),
0
);
是否有与此相关的功能来更新类别属性?
解决方案
没有现有的功能,这会像 Mage::getSingleton('catalog/product_action')->updateAttributes()
所以,让创建一个模块 这也是一样的 catalog/product_action
.
在这种情况下,需要创建一个新的资源模型和资源类。
在 资源类 ,我已经定义了一个更新的函数 category attribute quickly like product
.
所以配置。xml类似:
<global>
<models>
<[modelindefier]> <!-- this also call module model group class prefix -->
<class>[ModuleNameSpace]_[ModuleName]_Model</class>
<resourceModel>[modelindefier]_resource</resourceModel>
</custommodule>
<[modelindefier]_resource>
<class>[ModuleNameSpace]_[ModuleName]_Model_Resource</class>
</[modelindefier]_resource>
</models>
</global>
这样做的资源类。
班级 : [ModuleNameSpace]_[ModuleName]_Model_Resource_Category_Action
功能 : updateAttributes(params)
<?php /** * Catalog category attribute quick update * * @author Amit Bera<dev.amitbera@gmail.com> */ class [ModuleNameSpace]_[ModuleName]_Model_Resource_Category_Action extends Mage_Catalog_Model_Resource_Abstract { /** * Intialize connection * */ protected function _construct() { $resource = Mage::getSingleton('core/resource'); $this->setType(Mage_Catalog_Model_Category::ENTITY) ->setConnection( $resource->getConnection('catalog_read'), $resource->getConnection('catalog_write') ); } /** * Update attribute values for entity list per store * * @param array $entityIds * @param array $attrData * @param int $storeId * @return Mage_Catalog_Model_Resource_Product_Action */ public function updateAttributes($entityIds, $attrData, $storeId) { $this->_attributeValuesToSave = array(); $this->_attributeValuesToDelete = array(); $object = new Varien_Object(); $object->setIdFieldName('entity_id') ->setStoreId($storeId); $this->_getWriteAdapter()->beginTransaction(); try { foreach ($attrData as $attrCode => $value) { $attribute = $this->getAttribute($attrCode); if (!$attribute->getAttributeId()) { continue; } $i = 0; foreach ($entityIds as $entityId) { $i++; $object->setId($entityId); // collect data for save $this->_saveAttributeValue($object, $attribute, $value); // save collected data every 1000 rows if ($i % 1000 == 0) { $this->_processAttributeValues(); } } $this->_processAttributeValues(); } $this->_getWriteAdapter()->commit(); } catch (Exception $e) { $this->_getWriteAdapter()->rollBack(); throw $e; } return $this; } }
现在您可以使用下面的代码调用类函数:
Mage::getResourceModel('modelindefier/category_action')->updateAttributes(
array(5,10,11),
array('some_attribute'=>'some value',
'some_attribute'=>'some value2',
),
0
);
其他提示
没有任何功能可用于无需更新类别属性 加载类别模型