0

I've been doing some modifications my magento install to make it fit my needs but I'm come accross quite a few walls. This are some of the things I've done just incase it's needed as a reference for my upcoming question.

Magento: Add New Field to Credit Card Form

Magento: How to display customer's phone number on customer information field

Magento: How to Add Order / Payment information to Admin-BackEnd

Magento: How to save data to order_payment and quote_payment

So I've followed a few tutorials about adding a custom attribute but I'm puzzled as of how to run a script to add the attributes to the correct tables.

Here's why.

I noticed that the "telephone" field that already comese with magento, appears on several tables.

eav_attributes (4 instances) then the attribute_id for the telephone field also appears on other tables, all 4 instances of it. Now I have added a Mobile telephone number field as seen on my previous questions but I don't know how to make it appear on all of those tables with the necessary values.

I'm a bit limited on resources right now to provide more detailed information, but if someone can understand what I'm trying to explain, please assist me.

Thank you.

Community
  • 1
  • 1
BlueSun3k1
  • 757
  • 5
  • 21
  • 39

1 Answers1

0

Answer

  • Use a setup script executed by the entity module's setup resource class.

Why?

While there are classes and tables which are common to all EAV attributes and entities (e.g. eav_attribute table and Mage_Eav_Model_Entity_Setup), each EAV entity may have an additional attribute info table. The generic setup resource class Mage_Eav_Model_Entity_Setup has a _prepareValues() method which sets defaults for all of the generic attribute metadata (i.e. the columns in the eav_attribute table). In the case of Mage_Customer, there is the customer_eav_attribute table among others. The Mage_Customer setup resource class Mage_Customer_Model_Entity_Setup overrides this method, calling the parent method and adding more metadata defaults which are stored in the customer_eav_attribute table.

So, while a setup resource file can execute in any context (setup class instance), if it is modifying some other module's EAV configuration, it needs to do so with the correct class.

<?php

$installer = Mage::getResourceModel('customer/setup','customer_setup');

$installer->startSetup();

$installer->addAttribute(
    'customer',
    'new_attribute_code',
    array('label'=>'Label','type' => 'text')
)

$installer->endSetup();

Depending on the need to add to customer / customer address forms, there is further work to be done. References for this type of work can be found in Mage/Customer/sql/customer_setup/.

benmarks
  • 23,384
  • 1
  • 62
  • 84
  • Thank you very much for your response. I will work on a setup script for the necessary attributes and give it a try and hope it works. Manually adding/removing values from the DB is not exactly something I wanted to do but wasn't quite sure what was the best approach. – BlueSun3k1 Aug 16 '12 at 20:50