Magento Forum

   
Customizing Magento Core Abstract classes
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Hi all,

I am working at customizing magento for my store. I have some custom tables that need to inserted and updated when a product is inserted or updated. Especially when some particular attributes are inserted.

Say when attribute ‘XYZ’ is inserted, I need to ensure that my custom new tables are inserted with data. I looked through the code and observed that ‘Mage_Eav_Model_Entity_Abstractold’ inserts and updates attributes. So was planing to customize this class. Rather inherit this class and override the ‘_processSaveData’ method.

But am stuck here as the normal Model customization allows to override methods of Model classes and Helper classes. Am unsure if Abstract class methods can be overriden?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Very good point! smile

Only instantiated models can be overloaded, we were thinking hard about how to make abstract classes overloadable, but could not find a good solution in PHP for that.

However, your problem has different solution. Each attribute can have custom backend model, which means methods afterLoad(), beforeSave(), afterSave() and beforeDelete() of this model will be called upon each respective event on the entity.

Find your attribute in table `eav_attribute` and see field `backend_model`, which accepts full class name or Magento’s class notation (prefered).

So for example your attribute is ‘xyz’ and you put in backend_model field ‘mymodule/product_backend_xyz’, and the model will look like that:

<?php
class Mage_MyModule_Model_Product_Backend_Xyz
    
extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    
public function afterSave($object)
    
{
        
// this could be just 'xyz' but to be safe 
        // in case you use this backend for other attributes too
        
$attributeName $this->getAttribute()->getName();

        
$savedValue $object->getData($attributeName);
        
// here my custom processing for $savedValue
    
}
}

Same would go for afterLoad(), beforeSave() and beforeDelete()

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ross
Guru
 
Avatar
Total Posts:  336
Joined:  2007-08-31
Scarborough, North Yorkshire, UK
 

Thanks for that info Moshe, very helpful to know!  (I look forward to the day when there is more documentation for programming with Magento).

Are event methods like this used in other places as well?

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Thanks Moshe!! The moment I saw your update on the thread all my worries were over w.r.t this issue!!

Yes Ross. I look forward to that day as well. Guess part of the responsibility is with us when Magento has allowed us a very good wiki where we can start posting our code experiences which will be useful to others.

Again coming back to this thread, I initially went wrong when I tried to inherit ‘Mage_Eav_Model_Entity_Attribute_Backend_Abstract’ as Moshe had suggested. But once this was done, my attribute changes weren’t getting stored in DB blank stare . Then I re-read Moshe’s update and checked my attribute back_end in database table `eav_attribute` and found that my attribute was an array (basically my attribute is a multi-select). So everything worked fine when I inhertied from Mage_Eav_Model_Entity_Attribute_Backend_Array.

Besides this I wanted a few values like the current entity_id, store_id and a set of other attributes on the admin catalog product UI. This I obtained from the $newData ( look at code below). Hope I am doing this the right way. Would be glad if Moshe confirms this.

<?php
class Neo_MyAttrib_Model_Attribute_Backend_Author
    
extends Mage_Eav_Model_Entity_Attribute_Backend_Array
{
    
public function afterSave($object)
    
{
    parent
::afterSave($object);

    
$atrributeCode $this->getAttribute()->getAttributeCode();
        
$attribute $this->getAttribute($attributeCode);
        
$backend $attribute->getBackend();
        
$table $backend->getTable();

        
$newData $object->getData();

    
//Step 1 - Obtain the attribute values ids and myattrib values
        
foreach ($newData as $k=>$v{
          
if($k === 'myattrributecode'){
        $attrValue 
.= $v ', ';
      
}
        }

    
//Step 2 - Obtain the entity id of the product being updated
        
$entity $attribute->getEntity();
        
$entityIdField $entity->getEntityIdField();
        
$entityId $object->getData($entityIdField);

    
//Step 3 - Obtain the store id of the store being updated
        
$storeId $object->getStoreId();  
.
.
... 
// more processing code


    
}
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@srinigenie: i’m not sure what you’re tryig to do, but $object already contains all fields you needed.

$oldValue $object->getOrigData($attributeCode);
$newValue $object->getData($attributeCode);
$entityId $object->getId();
$storeId $object->getStoreId();

This code will give you some internal information about EAV storage, but i’m not sure how you’re going to use it:

$attribute $this->getAttribute($attributeCode);
        
$backend $attribute->getBackend();
        
$table $backend->getTable();

        
$newData $object->getData();

    
//Step 1 - Obtain the attribute values ids and myattrib values
        
foreach ($newData as $k=>$v{
          
if($k === 'myattrributecode'){
        $attrValue 
.= $v ', ';
      
}
        }

    
//Step 2 - Obtain the entity id of the product being updated
        
$entity $attribute->getEntity();
        
$entityIdField $entity->getEntityIdField();
        
$entityId $object->getData($entityIdField);

 
Magento Community Magento Community
Magento Community
Magento Community
 
srinigenie
Guru
 
Avatar
Total Posts:  539
Joined:  2008-02-04
 

Thanks Moshe. It would be helpful if the contents of $object is documented. Is this $object available anywhere on magento code?

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

Is there any way to have custom abstract classes without editing the core files? Similar to overriding models by putting them in the local folder.

I would hate to have to re-update the core files for every upgrade.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

Has anyone done custom abstract classes without editing core files?

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

Still trying to find out about creating custom abstract classes without editing core code. Anyone doing this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sensi
Sr. Member
 
Total Posts:  103
Joined:  2008-07-31
Paris, France
 

@ seudo?

You will have to update the -copied- file whenever it is upgraded, but you can leave the core intact by copying the core file under the local folder while keeping the Mage folders hierarchy:

e.g.: app/code/core/Mage/Catalog/Block/Product/Abstract.php => app/code/local/Mage/Catalog/Block/Product/Abstract.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

Sensi - thank you for your reply.

I have it set up similar to what you have suggested and it does not seem to execute the Abstract files in local but will excecute all the non-Abstract files.

I have it like so: app/code/local/Seudo/SalesPDF/Model/Abstract.php (and all some other files) and in a app/etc/modules i have the xml.

I also have in app/code/local/Seudo/SalesPDF/etc xml to tell magento to override the files.

I go in Magento Admin > System > Config > Advanced and I see the module have created and it shows enabled.

I have tried all this with cache disabled and Magento does not want to use the Abstract.php files.

Thank you again for any help you can provide.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seudo
Guru
 
Avatar
Total Posts:  344
Joined:  2008-04-21
 

Still no luck with custom abstract.php files.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top