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

时间:2013-09-04 19:35:28

标签: php mysql magento magento-1.7

我正在尝试将两个字段(Shipping PostcodeBilling Postcod)添加到Magento 1.7CE的后端Sales网格中。

我这样做是通过覆盖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(...)将列添加到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();
}

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

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'postcode' in order clause is ambiguous

我尝试使用MySQL的AS别名区分两个邮政编码,修改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'

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

2 个答案:

答案 0 :(得分: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')
    );
}

第二个_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,则排序错误。

答案 1 :(得分:2)

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