2012-04-17 58 views
0
protected function _prepareCollection() 
{ 
    $collection = Mage::getResourceModel($this->_getCollectionClass()); 
    $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('company')); 
    $this->setCollection($collection); 
} 

我使用上面的代码在订单列表网格上添加公司字段。 但它显示“项目(Mage_Sales_Model_Order)具有相同的ID‘1038’已经存在”Magento Grid添加新列

回答

0

也许尝试一个内部联接,而不是:

$collection->getSelect()->joinInner(
    array(
     'order_address' => 'sales_flat_order_address' 
    ), 
    'order_address.parent_id = main_table.entity_id' 
); 

此外,呼应你的SQL,看看收藏的回报,然后尝试在数据库上运行该sql。这应该可以帮助你弄清楚你做错了什么。

echo $collection->getSelect()->__toString(); 

请记住,单独不会将列添加到网格。你需要添加列在_prepareColumns()

编辑: 其实,考虑它,内部连接可能不会帮助你在这里。您遇到的问题是,sales_flat_order_address包含每个parent_id的多个条目,因此您需要通过使用GROUP BY或SELECT DISTINCT来解决重复问题。尝试是这样的:

$collection->getSelect()->joinInner(
    array(
     'order_address' => 'sales_flat_order_address' 
    ), 
    'order_address.parent_id = main_table.entity_id' 
)->group(array('entity_id', 'parent_id')); 

它并不完美,但你想做什么本质上是不完善的,因为一个订单有许多地址。另一种方法是只显示帐单地址,或只显示送货地址。

+0

其工作正常,但现在我有一个与发票网格小问题,我已经在下面的答案中提到的细节。 – p4pravin 2012-04-20 04:10:50