我正在尝试将两个字段 (Shipping Postcode
和Billing Postcod
) 添加到 Magento 1.7CE 的后端销售网格。
我通过覆盖Mage_Adminhtml_Block_Sales_Order_Grid::setCollection(...)
加入表格来做到这一点sales/order_address
。
public function setCollection($collection){
parent::setCollection($collection);
$collection->getSelect()->join(
array('address_shipping' => $collection->getTable("sales/order_address")),
'main_table.entity_id = address_shipping.parent_id AND address_shipping.address_type = "shipping"',
array('postcode')
);
$collection->getSelect()->join(
array('address_billing' => $collection->getTable("sales/order_address")),
'main_table.entity_id = address_billing.parent_id AND address_billing.address_type = "billing"',
array('postcode')
);
}
并将Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns(...)
列添加到销售网格。
protected function _prepareColumns(){
$this->addColumn('shipping_postcode', array(
'header' => Mage::helper('sales')->__('Shipping Postcode'),
'index' => 'postcode',
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('sales')->__('Billing Postcode'),
'index' => 'postcode',
));
return parent::_prepareColumns();
}
问题是两次我都需要postcode
从表中选择字段,sales/order_address
这会导致 SQL 错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'postcode' in order clause is ambiguous
我尝试使用 MySQL 的AS
别名来区分两个邮政编码,方法是修改setCollection(...)
为 passarray('shipping_postcode'=>'postcode')
和array('billing_postcode'=>'postcode')
以及更改_prepareColumns(...)
为 say'index' => 'shipping_postcode'
和'index' => 'billing_postcode'
. 这导致另一个 SQL 错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'shipping_postcode' in 'where clause'
如何在销售网格中实现两列?