Magento - SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'billing_name'

时间:2012-01-27 01:01:39

标签: grid magento-1.5 magento

我已更改 app / code / local / Mage / Adminhtml / Block / Sales / Order / Grid.php 以自定义订单网格上的内容。

_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()以添加必要的字段,所有工作都很棒!除了一件事......

当我通过结算发货搜索订单时,收到错误消息。我已将 filter_index 'filter_index' => 'theindex')添加到所有必要组件中,除了这两个字段结算发送

所以我也把 filter_index 放在那些。

一切都很棒。我可以搜索其他字段,但只要我搜索结算发送字段,我就会收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'billing_name' in 'where clause'

我尝试了各种各样的东西,但似乎没有任何效果。有人可以帮忙!???


1 个答案:

答案 0 :(得分:7)

当我退后一步并开始查看数据库时,我突然意识到我想要的字段已经格式化了。我不需要连接/合并任何东西。我只是需要像其他人一样调用这些字段......

我的新_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',
    ));

这就是她写的所有内容!