Question

I'm trying to add the Product Name, Product SKU, and Order shipment method, to the Sales Order Grid.

this is how my function looks:

protected function _prepareCollection()
{
    echo $collection = Mage::getResourceModel($this->_getCollectionClass())
    ->join(
            'sales/order_item',
            '`sales/order_item`.order_id=`main_table`.entity_id',
            array(
                    'skus'  => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
                    'names' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
            )
    )
    ->join(
            'sales/order',
            '`sales/order`.entity_id=`main_table`.entity_id',
            array(
                    'shipping_description' => 'shipping_description'
            )
    );
    echo $collection->getSelect();
    $collection->getSelect()->group('entity_id');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

but this gives this error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in-group statement is ambiguous

Probably because there are two entitity_ids in the main_table.

Is there a way to fix this?

Was it helpful?

Solution

$collection->getSelect()->group('entity_id');

Entity_id is the PK name for many tables. You should specify the table name as well:

$collection->getSelect()->group('maint_table.entity_id');

also, see: $collection->getSelect() copy-paste the query to mysql so you understand more the underlying issue.

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