Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

how to add expression based grid column in custom module’s grid ? 
 
ilovemagento
Member
 
Total Posts:  40
Joined:  2009-02-04
 

hello experts,

I followed the wiki article to create a new custom module that manages ‘portfolio’. Everything works fine in it but i got request from my client to add a new column that must be sum of few numeric values..

I tried using addExpressionAttributeToSelect() but it complaints that i am doing wrong because i am extending my collection from “Mage_Core_Model_Mysql4_Collection_Abstract” class.

Please guide me on how i can add an expression based grid column in my module ...

Thanks in advance

 
Magento Community Magento Community
Magento Community
Magento Community
 
rosew
Member
 
Total Posts:  43
Joined:  2009-03-07
Leatherhead UK
 

You can try this:

Add this function to your collection class:

public function addExpressionToSelect($alias, $expression)
{
// validate alias
if (isset($this->_joinFields[$alias])) {
throw Mage::exception(’Mage_Eav’, Mage::helper(’eav’)->__(’Joined field or attribute expression with this alias is already declared’));
}
$this->getSelect()->from(null, array($alias=>$expression));

$this->_joinFields[$alias] = array(
‘table’ => false,
‘field’ => $expression
);

return $this;
}

Then in your grid class:

$collection->addExpressionToSelect(’customer’, ‘CONCAT(table1.value,” “,table2.value)’);

$this->addColumn(’customer’, array(
‘header’=> Mage::helper(’sales’)->__(’Customer’),
‘width’ => ‘80px’,
‘type’ => ‘text’,
‘index’ => ‘customer’,
));

Hope that helps.

Rose

 
Magento Community Magento Community
Magento Community
Magento Community
 
rosew
Member
 
Total Posts:  43
Joined:  2009-03-07
Leatherhead UK
 

Here’s how I did it in the end:
collection class:
public function addExpressionToSelect($cols) {

$this->getSelect()->columns($cols);
return $this;
}

grid class:
$collection->addExpressionToSelect(array(’due’=>new Zend_Db_Expr(’original_price-price’)))

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top