在"销售订单网格"字段中使用列别名

Bar*_*tak 1 php mysql magento magento-1.7

我正在尝试将两个字段(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')
    );
}
Run Code Online (Sandbox Code Playgroud)

而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();
}
Run Code Online (Sandbox Code Playgroud)

问题是,我需要postcode从sales/order_address表中选择字段,这会导致SQL错误

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'postcode' in order clause is ambiguous
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用MySQL的AS别名2个邮政编码来区分,通过修改setCollection(...)传递array('shipping_postcode'=>'postcode')和array('billing_postcode'=>'postcode')以及改变_prepareColumns(...)说'index' => 'shipping_postcode'和'index' => 'billing_postcode'.这导致了另一个SQL错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'shipping_postcode' in 'where clause'
Run Code Online (Sandbox Code Playgroud)

如何在Sales Grid中实现两个列?

502*_*eek 9

让我们尝试第一个代码

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')
    );
}
Run Code Online (Sandbox Code Playgroud)

第二个_prepareColumns()在这里,

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();

}
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息'filter_index',请在此处注释/注销,然后尝试在网格中为邮政编码列排序.你会看到不同的结果.如果删除filter_index,则排序错误.