Fetch attribute value from database and populate admin drop down list

Hello Friends , today we will discuss here to fetch the attribute value from the database and showing it into the attribute dropdown list.

Code for fetching the value from the database :-

Class Namespace_Modulename_Model_Sourcefilename  extends Mage_Eav_Model_Entity_Attribute_Source_Abstract


public function getAllOptions()


$var = Mage::getModel('modulename/modelname')->getCollection();

foreach ($var as $var1)


$label = $var1->getName();//Name of your option displayed in dropdown of attribute

$value = $var1->getId(); // Id of attribute for product

$this->_options[]= array('label'=> $label, 'value'=> $value );


return $this->_options;



  •  First of all you have to create a new source file for your attribute in that you will have to write the above code.
  •  At the first you have to write a class hierarchy as mentioned above in the first line.
  •  After that you have write a function called getAllOptions() which is magento default function for this process.
  •  After that you would have to call your respective model for your attribute and use getCollection method to collect all the data stored in the database through that model.
  •  Now you will have set a foreach loop to distinguish the data stored in model.You will have to fatch the data required to you.
  •  In the foreach loop the value stored in $var is assigned to $var1.
  •  Than to display the label in the dropdown list you will have to fetch the respective database field.(suppose your database field name is name than use getName() method.)
  •  Similarly you will have to fatch the relative Id for your name from the database.
  •  In last you will have to just return it.
  • Below I have shown snapshot which shows  the Dropdown list of Department attribute.

Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: