One of the time consuming things in Magento development is object extending. Here is a real world scenario: Client wants new address fields for customers. What do you do?
You need to extend Mage_Customer_Model_Address object. Wouldn’t it be great if you can do something like
... $customer = new Mage_Customer_Model_Address(); $customer->load(164); /* Loads customer with id 164 */ $customer->some_new_address_field = "My second Address value here"; $customer->save(); ...
Awesome right! Unfortunately it does not work that way. Database is not aware of the existence of some_new_address_field attribute, since it does not exist. Few weeks ago I wrote an article called Extending Order object and hooking on event in Magento on Inchoo.net (company I work for). In that article I gave a bit “messy” example on how to extend order object. Cool as it may be, cooler that that is how we can extend all Magento objects (to be more precise Entities, since entities “communicate” with database). By default, Magento 1.3.2.1 has 28 entities in the system. Below is their list.
Magento default entities (reflects Magento 1.3.2.1 state)
Beyond “How to extend all entities at once” question, we need to ask ourself “Why would we want to extend all entities?”. Why not? Sooner or later you will probably need to extend Address, Product or Order object to store new info in it that your client asked of you.
Below is a module I wrote that extends of all of the above 28 entities at once.
Download ActiveCodeline_EntityExtender extension (module) for Magento.
By default module is made to create the “object properties” for all of the existing entities. Object properties (attributes) are assigned code name in for of strtolower($_attribute_namespace).’_’.$entity_code. Where $_attribute_namespace is set to “ActiveCodeline”. I strongly advise you open the app/code/local/ActiveCodeline/EntityExtender/sql/entityextender_setup/mysql4-install-0.1.0.php file and change $_attribute_namespace value to suite your needs.
Easiest way to test the success of module is to go to Magento Admin > Catalog > Attributes > Manage attributes and look for attribute with attribute code “activecodeline_catalog_product”.
Attributes created by the ActiveCodeline_EntityExtender
Stuff like below code example should now work.
... $customer = new Mage_Customer_Model_Address(); $customer->load(164); /* Loads customer with id 164 */ $customer->activecodeline_customer_address = "My second Address value here"; $customer->save(); ...
Do not forget that all newly added attributes are text type in database so you can store a bit more than just simple strings. You can save serialized data in them.
Hope you find the module useful.
DO NOT USE THIS ON LIVE SITE PRIOR YOU TEST IT YOURSELF! USE ON YOUR OWN RESPONSIBILITY! DO A FULL BACKUP OF DATABASE BEFORE ADDING THIS EXTENSION.