问题
I'm trying to add two fields (Shipping Postcode
and Billing Postcod
) to Magento 1.7CE's backend Sales grid.
I'm doing this by overriding Mage_Adminhtml_Block_Sales_Order_Grid::setCollection(...)
to join the table with 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')
);
}
And Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns(...)
to add the columns to Sales Grid.
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();
}
The issue is that both times I need to select postcode
field from sales/order_address
table which results in SQL error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'postcode' in order clause is ambiguous
I have tried to use MySQL's AS
alias to differentiate between two postcodes, by modifying setCollection(...)
to pass array('shipping_postcode'=>'postcode')
and array('billing_postcode'=>'postcode')
as well as changing _prepareColumns(...)
to say 'index' => 'shipping_postcode'
and 'index' => 'billing_postcode'
. This resulted in another SQL error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'shipping_postcode' in 'where clause'
How can I achieve having both columns in the Sales Grid?
回答1:
let try this first code
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('address_shipping.postcode as shippingpostcode')
);
$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('address_billing.postcode as billingpostcode')
);
}
Second _prepareColumns()
is here,
protected function _prepareColumns(){
$this->addColumn('shippingpostcode', array(
'header' => Mage::helper('sales')->__('Shipping Postcode'),
'index' => 'shippingpostcode',
'filter_index' => 'address_shipping.postcode'
));
$this->addColumn('billingpostcode', array(
'header' => Mage::helper('sales')->__('Billing Postcode'),
'index' => 'billingpostcode',
'filter_index' => 'address_billing.postcode'
));
return parent::_prepareColumns();
}
if you want to know more about 'filter_index'
, comment in/out in this, then try sorting in your grid for post code column. You will see different result. If you remove filter_index
, error in sorting.
回答2:
Before return parent::_prepareCollection();
You should create a join:
$collection->getSelect()->joinLeft(array('billing'=>'sales_flat_order_address'),
'main_table.entity_id = billing.parent_id AND billing.address_type="billing"',array('billing.postcode AS bp'));
And in the method _prepareColumns paste:
$this->addColumn('bp', array(
'header' => Mage::helper('sales')->__('Billing Postcode'),
'index' => 'bp',
'width' => '60px',
'filter_index' => 'billing.postcode'
));
来源:https://stackoverflow.com/questions/18622319/using-column-aliases-in-sales-order-grid-field