2

我正在尝试将两个字段 (Shipping PostcodeBilling 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'

如何在销售网格中实现两列?

4

2 回答 2

10

让我们试试这个第一个代码

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')
    );
}

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

}

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

于 2013-09-05T04:15:21.100 回答
3

return parent::_prepareCollection(); 创建联接之前:

$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'));

并在 _prepareColumns 方法中粘贴:

$this->addColumn('bp', array(
            'header' => Mage::helper('sales')->__('Billing Postcode'),
            'index' => 'bp',
            'width' => '60px',
            'filter_index'  => 'billing.postcode'
        ));
于 2014-12-26T11:25:48.763 回答