Question

Why cant I do the following:

            $this->getSelect()
        ->columns(
            array(
                '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"

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top