blog site of branko ajzele, senior developer / project manager

EntityExtender – Extend all Magento entity objects at once

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)

  • entity_type_code: catalog_category
  • entity_type_code: catalog_product
  • entity_type_code: creditmemo
  • entity_type_code: creditmemo_comment
  • entity_type_code: creditmemo_item
  • entity_type_code: customer
  • entity_type_code: customer_address
  • entity_type_code: customer_payment
  • entity_type_code: invoice
  • entity_type_code: invoice_comment
  • entity_type_code: invoice_item
  • entity_type_code: invoice_shipment
  • entity_type_code: order
  • entity_type_code: order_address
  • entity_type_code: order_item
  • entity_type_code: order_payment
  • entity_type_code: order_status
  • entity_type_code: order_status_history
  • entity_type_code: quote
  • entity_type_code: quote_address
  • entity_type_code: quote_address_item
  • entity_type_code: quote_address_rate
  • entity_type_code: quote_item
  • entity_type_code: quote_payment
  • entity_type_code: shipment
  • entity_type_code: shipment_comment
  • entity_type_code: shipment_item
  • entity_type_code: shipment_track

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

  • created attribute: activecodeline_catalog_category
  • created attribute: activecodeline_catalog_product
  • created attribute: activecodeline_creditmemo
  • created attribute: activecodeline_creditmemo_comment
  • created attribute: activecodeline_creditmemo_item
  • created attribute: activecodeline_customer
  • created attribute: activecodeline_customer_address
  • created attribute: activecodeline_customer_payment
  • created attribute: activecodeline_invoice
  • created attribute: activecodeline_invoice_comment
  • created attribute: activecodeline_invoice_item
  • created attribute: activecodeline_invoice_shipment
  • created attribute: activecodeline_order
  • created attribute: activecodeline_order_address
  • created attribute: activecodeline_order_item
  • created attribute: activecodeline_order_payment
  • created attribute: activecodeline_order_status
  • created attribute: activecodeline_order_status_history
  • created attribute: activecodeline_quote
  • created attribute: activecodeline_quote_address
  • created attribute: activecodeline_quote_address_item
  • created attribute: activecodeline_quote_address_rate
  • created attribute: activecodeline_quote_item
  • created attribute: activecodeline_quote_payment
  • created attribute: activecodeline_shipment
  • created attribute: activecodeline_shipment_comment
  • created attribute: activecodeline_shipment_item
  • created attribute: activecodeline_shipment_track

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.

  • Binod Luitel
    Hello Branko,
    Nice posting there but I want to do something different in the Magento. I want to add a different entity attribute sets for administrator users in magento database. i.e. I am creating different user groups for administrators say seller. Seller are required to register through the front end and then are able to add their own products but are restricted to create categories. i.e. they are given access to backend after registration for adding products and other limited access privilege. And they are allowed to setup their own profile page like http://domainName.com/newSeller. So I want to be able to add different entity sets for administrator users. Any idea will be greatly appreciated. Looking forward for your response. Thanks.
  • Matthieu
    Sorry to re-post. I found why the new property didn't appear. It was a syntax problem. Please, forget my previous post.

    So, my problem is, now, that I can add a property to an order but I can't add one to shipment. The install (mysql) file doesn't add the line in the database concerning the shipment, even if shipment is part of eav_entity_type ans has a code.

    I changed it directly in the database but do you know why it doesn't create a new property for shipment ? In order to do it cleaner...

    Thanks
  • Matthieu
    Hi,
    Your explanation on how to add properties to magento entities was very useful, for me. I found my new properties in the database, including the _catalog_product one.
    Unfortunately, I was hoping the new property would appear in the Order object when I use the api, with sales_order.info service. But it doesn't.
    What can I do to get it in my Order object retrieved from the API ?

    Thanks in advance,
  • Leo
    Good Job. I have a quick question. I'm trying to extend Virtual Product type. What would be the best approach for this? I want a lightweight version of catalog_product without all the unnecessary attributes. I basically just want a name,sku, and price to be able to add it to the shopping cart.
  • @Alma

    Appreciate the feedback. Thnx.
  • Alma
    Everything works fine...I hadn't change the rights for the sql file to be executed...
    Thnx for sharing Branko :)
  • Alma
    Good morning.
    I have downloaded the ActiveCode_EntityExtender but i see that there are no new attributes created.
    Maybe there is smthg that i do wrong.
    I put "ActiveCodeLine" folder into the app/code/local & the ActiveCodeLine_EntityExtender.xml to app/etc/modules folder.
    When i open magento there is nothing new in it...
    Can u help me? Thnx in advance...
  • Nightfly
    Awesome! This should really go into core. Thanks for sharing!
blog comments powered by Disqus
Powered by Wordpress | Designed by Elegant Themes