Magento News

 

save an attribute value without saving its parent entity in Magento

I’d like to be able to save the value of an attribute without saving it’s parent entity. I have created a new customer attribute via my module’s sql/setup file, and now I want to populate that attribute for each of the existing customers. I’ve created a Backend model for the attribute that detects null values during the entity load (afterLoad) and generates the content, but I am hesitant to save the entity at that point in the process.

class Aligent_Referral_Model_Customer_Attribute_Backend_Referralcode extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
public function afterLoad($oCustomer)
{
    $vAttrCode = $this->getAttribute()->getAttributeCode();
    $vValue = $oCustomer->getData($vAttrCode);
    if (empty($vValue)){
        $oCustomer->setData($vAttrCode, Mage::helper('referral')->generateRafCode($oCustomer));
    }
    return $this;
}
}

Ideally, I’d like to call something like $this->getAttribute()->setValue('blah')->save() in that afterLoad method so that it doesn’t rely on the user clicking save.

I could write a script that loads a collection of all customers and walks them to set the value, but there’s over 50,000 customers and I’m concerned about the performance impact of running that on a production server…

Any thoughts appreciated.
JD

save an attribute value without saving its parent entity in Magento

Possibly Related Posts:


 

Leave a Reply