How to use magento grid to display master database value

I recently ran into this problem while I was building new customer module that use admin side grid with master child releationship

then i found solution like this

At one point I need to access value customer name that in save in master table

I create a database like this


CREATE TABLE 'gallary' (

`gallary_id` int(11) unsigned NOT NULL auto_increment,

`customer_id` int(11) unsigned NOT NULL,

PRIMARY KEY (`gallary_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Then create collection without join table it display id not master value

/app/code/local/<your interface>/<your modulename>/block/adminhtml/<modulename>/Grid.php


protected function _prepareCollection()

{

$collection = Mage::getModel('modulename/classname')->getCollection();

$this->setCollection($collection);

return parent::_prepareCollection();

}

To display customer name of customer id that we get in our collection I create one array collection that have in customer[id] and customer[name], and this is assign to  grid column  with column type= option

Like this

/app/code/local/<your interface>/<your modulename>/block/adminhtml/<modulename>/Grid.php


protected function _prepareColumns()

{
$customers= array();
$collection = Mage::getResourceModel('customer/customer_collection')
 ->addNameToSelect();
 $customers = array();
 foreach( $collection as $customer){
 $customers[] = array('value' => $customer['entity_id'], 'label'=> $customer['name']);
 }

foreach ($customers as $customer){

$customers=array($customer['value']=>$customer['label']);

}

$this->addColumn('customer', array(

'header'    =>  Mage::helper('classname')->__('Customer'),

'width'     =>  '200',

'index'     =>  'customer_id',

'type'      =>  'options',

'options'   => $customers,

));

return parent::_prepareColumns();

}

And magic is display all row  of customer column display their respective customer name.

Enjoy  magento ;

One Response to “How to use magento grid to display master database value”

  1. ansolinrobort (@ansolinrobort) Says:

    Easiest technique to introduce actual use of magneto grid to display master database value.Addition of step wise representation ensures user to acquire huge knowledge on this.Magento project manager


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: