题
我正在尝试将产品名称,产品SKU和订单运输方法添加到销售订单网格中。
这就是我的功能的外观:
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();
}
但这给出了这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'entity_id' in-group statement is ambiguous
可能是因为其中有两个entitity_ids main_table
.
有没有办法来解决这个问题?
解决方案
$collection->getSelect()->group('entity_id');
Entity_id是许多表的PK名称。您也应该指定表名称:
$collection->getSelect()->group('maint_table.entity_id');
另外,请参阅: $collection->getSelect()
将查询复制到MySQL,因此您可以更多地了解基本问题。