2

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

The Query
--------
$collection = Mage::getModel('customer/customer')->getCollection();
 $collection->addAttributeToSelect('userid')->addAttributeToSelect('firstname')->addAttributeToSelect('lastname')->addAttributeToSelect('profile_image')->addAttributeToSelect('specialist')->addAttributeToSelect('latitude')->addAttributeToSelect('free_consult')->addAttributeToSelect('longitude')->addAttributeToFilter('group_id',Federallawyer_Customer_Helper_Data::LAWYER);
$collection->getSelect()->order("e.firstname ASC")

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

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'))
1

Use setOrder instead of order

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

Edit:

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

etc

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(
                $this->_prepareOrderExpression($orderExpr)
            );
        }

        $orderExpr .= ' ' . $dir;
        $this->getSelect()->order($orderExpr);
    }
    return $this;
}

Note:

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

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.