2012-04-17 57 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