
Magento 1.x – Use MySQL functions on Magento collections
Sometimes, you may need to use mysql functions in your query, to add any of them on your Magento collections, you could use the addExpressionAttributeToSelect statement.
Below, i will attach some examples:
CONCAT
1 2 |
$collection = Mage::getResourceModel('customer/customer_collection') ->addExpressionAttributeToSelect('fullname', 'CONCAT({{firstname}}, " ", {{lastname}})', array('firstname','lastname')); |
MONTH
1 2 3 |
$collection = Mage::getResourceModel('customer/customer_collection') ->addExpressionAttributeToSelect('birth_month', 'MONTH({{dob}})', 'dob') ->joinAttribute('dob', 'customer/dob', 'entity_id', null, 'left'); |
LOCATE
1 |
$collection->addExpressionAttributeToSelect('alias', 'LOCATE("WordToSeach",field)'); |