Save attribute value without saving its parent in Magento

I would like to save the attribute value without saving its parent. I created a new client attribute through my sql module / setup file ( ref ), and now I want to populate this attribute for each of the existing clients. I created a Backend model for an attribute that detects null values ​​during the loading of an object (afterLoad) and generates content, but I hesitate to save the object at this 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 would like to name something like $this->getAttribute()->setValue('blah')->save() in this afterLoad method afterLoad that it does not rely on the user click save.

I could write a script that downloads a collection of all clients and walks through them to set the value, but there are more than 50,000 clients, and I am concerned about the impact of performance on execution on the production server ...

Any thoughts appreciated.
Jd

+8
php attributes magento entity-attribute-value
source share
2 answers

So, one of the parameters that works for writing directly to the database. This is not particularly elegant, but effective enough to achieve a “lazy load” approach to populating a new attribute for existing customers (or other objects). Here is the additional code in Attribute_Backend_Referralcode to what I wrote in the question:

 $vAttrValue = 'some string'; $oCustomer->setData($vAttrCode, $vAttrValue); $iAttrId = $this->getAttribute()->getAttributeId(); $data = array( 'entity_type_id' => $oCustomer->getEntityTypeId(), 'attribute_id' => $iAttrId, 'entity_id' => $oCustomer->getId(), 'value' => $vAttrValue, ); $vTableName = $this->getAttribute()->getBackend()->getTable(); Mage::getResourceModel('table/alias')->getWriteAdapter()->insert($vTableName, $data); 

This requires the publication of a publicly available method for obtaining the Record Adapter in the Mysql4 model, but there are not many.

It is still interesting to find out if there is an approach that uses the EAV model layer better ...

+2
source share

The EAV resource model offers the ability to store individual attributes without saving an object.

 $value = Mage::helper('referral')->generateRafCode($oCustomer); $oCustomer->setData($vAttrCode, $value)->getResource()->saveAttribute($oCustomer, $vAttrCode); 
+19
source share

All Articles