Home » Magento21 October 2013

Magento: Use MySQL functions with addExpressionAttributeToSelect

Function addExpressionAttributeToSelect as defined in Mage_Eav_Model_Entity_Collection_Abstract (/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php) allows us to use MySQL functions in Magento collection.

Here is an example of using MySQL function CONCAT in Magento customer collection to concat customer’s firstname and lastname:

Here is another example which uses MySQL function MONTH which fetches only month from customer’s birthdate.

Hope it helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • I love you! =P
    It helps me a lot! It’s beautiful how magento/zf uses it.

  • Pranay Gondane

    How we can use this method with our custom table collection? If i use like above it gives error… Fatal error: Call to undefined method Xyz_Test_Model_Mysql4_Admincommission_Collection::addExpressionAttributeToSelect()

    .. I ‘ll Appreciate your help.

  • Pranay Gondane

    I used this and it’s working now…
    $collection->getSelect()->columns(new Zend_Db_Expr(“IF(status = 1,’Activated’,’Inactivated’) as status”));

  • sandy

    I LOVE YOU MAN…… <3