2

When updating product attributes, this method would suffice so that it would prevent loading the whole product model:

Mage::getSingleton('catalog/product_action')->updateAttributes(
    array(1,2,3),
    array('some_attribute'=>'some value',
          'some_attribute_2'=>'some value',
         ),
    0
);

Is there a function related to this to update the category attribute?

1

There are no function is existing,which will do like Mage::getSingleton('catalog/product_action')->updateAttributes()

So,let create a module which will do same like catalog/product_action.

In this case need to create a new resource model & resource class.

On the resource class , i have define a function which update category attribute quickly like product.

So config.xml like:

<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>

The resource class which do like this.

class : [ModuleNameSpace]_[ModuleName]_Model_Resource_Category_Action

function : 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;
    }
}

Now you have call class function by using below code:

 Mage::getResourceModel('modelindefier/category_action')->updateAttributes(
    array(5,10,11),
    array('some_attribute'=>'some value',
          'some_attribute'=>'some value2',
         ),
    0
);
1

No any function is available to update category attribute without loading category model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.