My required result is customer collection sort by firstname (ascending and descending order)

The Query
$collection = Mage::getModel('customer/customer')->getCollection();
$collection->getSelect()->order("e.firstname ASC")

But its show an error "Unknown column 'e.firstname' in 'order clause'"

Instead of $collection->getSelect()->order("e.firstname ASC") use

$collection->addAttributeToSort('firstname', 'ASC')

And an off topic tip, you can add all the attributes you need to the select from one addAttributeToSelect statement like this:

$collection->addAttributeToSelect(array('firstname', 'lastname', 'profile_image', 'specialist', 'latitude', 'free_consult', 'longitude'))
| improve this answer | |

Use setOrder instead of order

$collection->setOrder('firstname','DESC'); // for  descending order Use ASC for ascending 
| improve this answer | |
  • i follow with your way,but the expected result not get – senthil Jul 14 '15 at 7:45


e alias name indicate the customer_entity" Customer Entity" table and as magento Customer model is follow Eav Attribute structure then it does not save it in single flat table.

Better to use addAttributeToSort()

As customer model is follow Eav Structure that if you have trying sort using getSelect Query(->getSelect()->order("e.firstname ASC"))

or using setOrder(fieldname',DESC) then it will risky becasue of multitple time join of below table s

  1. customer_entity
  2. customer_entity_datetime
  3. customer_entity_decimal
  4. customer_entity_int
  5. customer_entity_text
  6. customer_entity_varchar


Above tables are join several times basic of selet attribute and sort attribute in collection in different table alias name _getAttributeTableAlias().

Only you can do if you have good & Solid knowledge about eav table of that Eav Model.

If use addAttributeToSort() then magento is already do it logic on this function on

Mage_Eav_Model_Entity_Collection_Abstract class.

Take a Look on function:

public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
    if (isset($this->_joinFields[$attribute])) {
        $this->getSelect()->order($this->_getAttributeFieldName($attribute).' '.$dir);
        return $this;
    if (isset($this->_staticFields[$attribute])) {
        $this->getSelect()->order("e.{$attribute} {$dir}");
        return $this;
    if (isset($this->_joinAttributes[$attribute])) {
        $attrInstance = $this->_joinAttributes[$attribute]['attribute'];
        $entityField = $this->_getAttributeTableAlias($attribute) . '.' . $attrInstance->getAttributeCode();
    } else {
        $attrInstance = $this->getEntity()->getAttribute($attribute);
        $entityField = 'e.' . $attribute;

    if ($attrInstance) {
        if ($attrInstance->getBackend()->isStatic()) {
            $orderExpr = $entityField;
        } else {
            $this->_addAttributeJoin($attribute, 'left');
            if (isset($this->_joinAttributes[$attribute])||isset($this->_joinFields[$attribute])) {
                $orderExpr = $attribute;
            } else {
                $orderExpr = $this->_getAttributeTableAlias($attribute).'.value';

        if (in_array($attrInstance->getFrontendClass(), $this->_castToIntMap)) {
            $orderExpr = Mage::getResourceHelper('eav')->getCastToIntExpression(

        $orderExpr .= ' ' . $dir;
    return $this;


I setOrder() really good idea whenever you have using Non-Eav Model i,e Flat Collection(simple table model)

| improve this answer | |
  • do you having any other idea, i did but not getting the desired result – senthil Jul 14 '15 at 7:46
  • you can follow marius answer ...magento.stackexchange.com/questions/74298/… As customer is eav you use addAttributeToSort() – Amit Bera Jul 14 '15 at 7:57
  • here i am unable to fetch the value firstname, lastname and rest of custom field value in sales order column ..any help will be appreciated – irshad Jan 8 at 9:34
  • here i am unable to fetch the value of firstname, last name and other created cutome in sales order grid coloumn stackoverflow.com/questions/59626259/… any help will be appreciated. – irshad Jan 8 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.