我已更改 app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php 以自定义 Orders 网格上的内容。
在 _getCollectionClass()
我有这个:
protected function _getCollectionClass()
{
//return 'sales/order_grid_collection';
return 'sales/order_collection';
}
在
_prepareCollection()
我有这个:protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('s1' => 'sales_flat_order_address'),'main_table.shipping_address_id = s1.entity_id',array('region','firstname','lastname'));
$collection->getSelect()->joinLeft(array('s2'=>'sales_flat_order_address'),'main_table.billing_address_id = s2.entity_id',array('firstname','lastname'));
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s2.firstname, ' ',s2.lastname) AS billing_name"));
$collection->getSelect()->columns(new Zend_Db_Expr("CONCAT(s1.firstname, ' ',s1.lastname) AS shipping_name"));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total')); // New
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone')); // New
$this->setCollection($collection);
return parent::_prepareCollection();
}
现在我已经更改了
_prepareColumns()
以添加我需要的字段,并且一切正常!除了一件事...当我通过 Billing 或 Shipping 搜索订单时,出现错误。我已经将 filter_index 的 (
'filter_index' => 'theindex'
) 添加到所有必要的组件中,除了这两个字段 Billing 或 Shipping 之外,它们都很好用。所以我也把 filter_index 放在了上面。
一切都很好。我可以搜索其他字段,但是一旦我搜索 Billing 或 Shipping 字段,我就会收到此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'billing_name' in 'where clause'
我尝试了各种方法,但似乎没有任何效果。有人可以帮忙吗!???
最佳答案
当我退后一分钟开始查看数据库时,我突然意识到我想要的字段已经格式化。我不需要连接/合并任何东西。我只需要像其他已经被调用的字段一样调用这些字段......
我的新 _prepareCollection()
函数是:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft(array('sfog' => 'sales_flat_order_grid'),'main_table.entity_id = sfog.entity_id',array('sfog.shipping_name','sfog.billing_name'));
$collection->getSelect()->joinLeft(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight','sfo.discount_description','sfo.increment_id','sfo.store_id','sfo.created_at','sfo.status','sfo.base_grand_total','sfo.grand_total')); // New
$collection->getSelect()->joinLeft(array('sfoa'=>'sales_flat_order_address'),'main_table.entity_id = sfoa.parent_id AND sfoa.address_type="shipping"',array('sfoa.street','sfoa.city','sfoa.region','sfoa.postcode','sfoa.telephone')); // New
$this->setCollection($collection);
return parent::_prepareCollection();
}
注意
sfog
数组。这将进入数据库的
sales_flat_order_grid
表并获取预先格式化的名称,就像它们在原始网格中一样。我想知道这是否与从该表中调用原始网格的事实有关(讽刺)-:P然后,就像确保在两个名称字段中执行
filter_index
(像所有其他字段一样)例子:
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
'filter_index' => 'sfog.billing_name',
));
这就是她写的全部内容!
关于grid - Magento - SQLSTATE[42S22] : Column not found: 1054 Unknown column 'billing_name' in 'where clause' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9027741/