Just another WordPress.com weblog

Posts tagged ‘join table’

Join any table with collection in admin

Hello,

We can easily display different table values as a column in grid in adminend of magento.

For eg:
I have to add one field for displaying ‘rule_id’, ‘status’, ‘send_date’ field as a columns in customer grid.
//Customer grid collection
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id');
$this->setCollection($collection);

/***From here we can left join with ‘giftcard’ table, join criteria is ‘gf.customer_id = e.entity_id’ and in array we can specifie the ‘rule_id’ as index in column and its values are from table giftcard. i.e ‘gf.rule_id’ and i have grouped with customer primary key i.e ‘e.entity_id’ ***/

$collection->getSelect()->joinLeft(array('gf'=>'giftcard'),
'gf.customer_id = e.entity_id',
array(
'rule_id'=> 'gf.rule_id',
'status' => 'IF(gf.status IS NULL,"Unsend","Send")',
'send_date' => 'gf.created_time'
))
->group('e.entity_id');

//To add column
Add below

$this->addColumn('created_time', array(
'header' => Mage::helper('giftcard')->__('Coupon Send Date'),
'align' => 'left',
'index' => 'send_date',
'type' => 'datetime',
'gmtoffset' => true
));

I hope this will help you..
If this post is help you than please do reply,..

Thanks,
Bijal Bhavsar 🙂