Why cant I do the following:

                'ordered_qty' => 'COUNT(DISTINCT main_table.entity_id)', 
                'invoiced_qty' => new Zend_Db_Expr('( select COUNT(OI.item_id) AS Ct from sales_flat_order_item AS OI WHERE OI.order_id=main_table.entity_id ) AS invoiced_qty')

The subquery fails with the error, "Cannot determine the field name"

  • Well the issue is certainly with the invoiced_qty column because it goes away when I comment it out.
    – user2045
    Aug 12 '13 at 19:40
  • Ok, so when I log the $attribute variable using Mage::log() I get this: 2013-08-12T20:00:18+00:00 DEBUG (7): Array ( [0] => invoiced_qty [1] => Zend_Db_Expr Object ( [_expression:protected] => select from sales_flat_order_item AS OI WHERE OI.order_id=main_table.entity_id ) )
    – user2045
    Aug 12 '13 at 20:02
  • Notice that the subquery's select clause is blank
    – user2045
    Aug 12 '13 at 20:03
  • nevermind, it was something else. I am ashamed.
    – user2045
    Aug 12 '13 at 20:16
  • Don't be ashamed — everyone's wrong until they're right.
    – Alan Storm
    Aug 13 '13 at 1:31

Your question doesn't have enough context to provide a concrete answer, so I'm going to explain why Magento is throwing an exception with the message

Cannot determine the field name

Hopefully that will give you enough information to track down the problem.

In a stock Magento system, the only place the error "Cannot determine the field name" appears is

#File: app/code/core/Mage/Sales/Model/Resource/Collection/Abstract.php
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, if the _attributeToField method is passed an attribute value that's

  1. An Empty string

  2. An attribute object where getAttributeCode returns false (or one of the many values PHP considers false-ish)

  3. Something that's not an Mage_Eav_Model_Entity_Attribute or string

then the exception "Cannot determine the field name" is thrown.

In a standard system Magento calls this method when you call a collection object's addAttributeToSelect, addAttributeToFilter, addAttributeToSort, or addAttributeToSearchFilter methods.

So, something about your code causes an invalid value to be passed to _attributeToField. This means the problem isn't your sub-query, or the sub-query may alter something that causes an invalid value to be passed.

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.