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 (ref), 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
The EAV resource model offers the ability to save single attributes without saving the entity.
$value = Mage::helper('referral')->generateRafCode($oCustomer);
$oCustomer->setData($vAttrCode, $value)->getResource()->saveAttribute($oCustomer, $vAttrCode);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With