10

I am trying to get the user orders history, used the following query and its working fine. But its returning all the order related fields from table

    $collection     = Mage::getModel("sales/order")->getCollection()
                               ->addAttributeToSelect('*')
                               ->addFieldToFilter('customer_id', 400)
                               ->setOrder('created_at', 'desc');

I want to fetch only particular fields, so in case of ->addAttributeToSelect('*')

     used the following code 

 ->addAttributeToSelect(array('created_at','customer_id','increment_id','updated_at','status','entity_id','state'))

But getting error as "Cannot determine the field name."

20

Use addFieldToSelect incase of addAttributeToSelect

addFieldToSelect used for flat model

addAttributeToSelect user for EAV model

3

The reason that you are getting the error is that the method Mage_Sales_Model_Resource_Collection_Abstract::addAttributeToSelect only works for individual attributes and not an array of attributes.

It only works for individual attributes because it will validate call to make sure the attribute is real.

public function addAttributeToSelect($attribute)
{
    $this->addFieldToSelect($this->_attributeToField($attribute));
    return $this;
}

When you look into Mage_Sales_Model_Resource_Collection_Abstract::_attributeToField you see that it works as follows.

  1. If you pass a string then it will simply return the string,
  2. If you pass an object then it will validate that this is an object of type Mage_Eav_Model_Entity_Attribute and return the attribute code,

The code looks as follows:

protected function _attributeToField($attribute)
{
    $field = false;
    if (is_string($attribute)) {
        $field = $attribute;
    } elseif ($attribute instanceof Mage_Eav_Model_Entity_Attribute) {
        $field = $attribute->getAttributeCode();
    }
    if (!$field) {
        Mage::throwException(Mage::helper('sales')->__('Cannot determine the field name.'));
    }
    return $field;
}

So you options are the following as I see it:

  1. Simply call addFieldToSelect with an array of attribute codes. Since you are only passing codes and not objects you will not get the validation, but maybe you do not need this in your case.
  2. Call addAttributeToSelect once for each attribute.

I would suggest in your case option one would be the best.

1

AFAIK a work around is just to do them all singularly (for whatever reason you are determined to use addAttributeToSelect:

->addAttributeToSelect('created_at') 
->addAttributeToSelect('customer_id') 
->addAttributeToSelect('increment_id') 
->addAttributeToSelect('updated_at') 
->addAttributeToSelect('status') 
->addAttributeToSelect('entity_id')
->addAttributeToSelect('state')
0

As the title of the post is not collection specific... Mage_Catalog_Model_Resource_Product_Collection has an additional parameter joinType which is to be used like so:

$attributesToSelect = array('name','description');
$collection->addAttributeToSelect($attributesToSelect, 'left');

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.