2

我使用自定义模块在 Magento 中向 Sales_Order_Grid 添加新列,但如果我搜索 Order_Id,页面将​​重定向到仪表板。

如果我再次尝试选择销售/订单,我会收到错误消息:

a:5:{i:0;s:104:"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'increment_id' in where clause is ambiguous";i:1;s:6229:"#0

我不知道如何解决这个问题,真的可以请一些指导吗?

这是 Grid.php 代码:

<?php
class Excellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{


    protected function _addColumnFilterToCollection($column)
    {
        if ($this->getCollection()) {
            if ($column->getId() == 'shipping_telephone') {
                $cond = $column->getFilter()->getCondition();
                $field = 't4.telephone';
                $this->getCollection()->addFieldToFilter($field , $cond);
                return $this;
            }else if ($column->getId() == 'shipping_city') {
                $cond = $column->getFilter()->getCondition();
                $field = 't4.city';
                $this->getCollection()->addFieldToFilter($field , $cond);
                return $this;
            }else if ($column->getId() == 'shipping_region') {
                $cond = $column->getFilter()->getCondition();
                $field = 't4.region';
                $this->getCollection()->addFieldToFilter($field , $cond);
                return $this;
            }else if ($column->getId() == 'shipping_postcode') {
                $cond = $column->getFilter()->getCondition();
                $field = 't4.postcode';
                $this->getCollection()->addFieldToFilter($field , $cond);
                return $this;
            }else if($column->getId() == 'product_count'){
                $cond = $column->getFilter()->getCondition();
                $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
                $this->getCollection()->getSelect()->having($this->getCollection()->getResource()->getReadConnection()->prepareSqlCondition($field, $cond));
                return $this;
            }else if($column->getId() == 'skus'){
                $cond = $column->getFilter()->getCondition();
                $field = 't6.sku';
                $this->getCollection()->joinSkus();
                $this->getCollection()->addFieldToFilter($field , $cond);
                return $this;
            }else{
                return parent::_addColumnFilterToCollection($column);
            }
        }
    }

    protected function _prepareColumns()
    {

        $this->addColumnAfter('shipping_description', array(
                'header' => Mage::helper('sales')->__('Shipping Method'),
                'index' => 'shipping_description',
        ),'shipping_name');
        $this->addColumnAfter('method', array(
                'header' => Mage::helper('sales')->__('Payment Method'),
                'index' => 'method',
                'type'  => 'options',
                'options' => Mage::helper('payment')->getPaymentMethodList()
        ),'shipping_description');

        $this->addColumnAfter('shipping_city', array(
                'header' => Mage::helper('sales')->__('Shipping City'),
                'index' => 'shipping_city',
        ),'method');

        $this->addColumnAfter('shipping_telephone', array(
                'header' => Mage::helper('sales')->__('Shipping Telephone'),
                'index' => 'shipping_telephone',
        ),'method');

        $this->addColumnAfter('shipping_region', array(
                'header' => Mage::helper('sales')->__('Shipping Region'),
                'index' => 'shipping_region',
        ),'method');

        $this->addColumnAfter('shipping_postcode', array(
                'header' => Mage::helper('sales')->__('Shipping Postcode'),
                'index' => 'shipping_postcode',
        ),'method');

        /*$this->addColumnAfter('product_count', array(
                'header' => Mage::helper('sales')->__('Product Count'),
                'index' => 'product_count',
                'type' => 'number'
        ),'increment_id');

        /*$this->addColumnAfter('skus', array(
                'header' => Mage::helper('sales')->__('Product Purchased'),
                'index' => 'skus',
        ),'increment_id');*/

        return parent::_prepareColumns();

    }


}
4

2 回答 2

4

这就是我的情况:

$collection->addFilterToMap('increment_id', 'main_table.increment_id');

于 2014-02-07T07:23:17.437 回答
2

看起来您正在向管理销售网格添加更多列?听起来您正在使用包含针对第二个表的连接的集合,您需要将表别名添加到 where 语句中,并使用 increment_id 列定义,所以table_alias.increment_id.

要检查这一点,并假设调用$this->getCollection()fromExcellence_Salesgrid_Block_Adminhtml_Sales_Order_Grid返回集合,请从集合中获取选择对象:

$select = $this->getCollection()->getSelect();

希望您在 IDE 中使用 xdebug,并且可以在代码中设置断点。如果是这样设置一个断点并检查$select上面的行所拉的对象。在此对象中,您将看到一个_parts数组,该数组描述了您的 select 语句的构造方式。在其中,您将看到一个from数组,其中包含有关作为语句一部分的表的信息。如果您有JOIN,这将包含多个条目。

在此处,您还可以看到where哪个将描述作为语句一部分的 where 子句 - 这可能是问题所在。您需要识别from数组中正确表的别名,然后在其中where添加子句,而不是执行以下操作:

$this->getCollection()->getSelect()->where('increment_id = ?', $id);

而是这样做:

$this->getCollection()->getSelect()->where('table_alias.increment_id = ?', $id);

于 2013-03-11T17:33:59.213 回答