2014-01-30 63 views
1

我在我的網格中有此代碼。我試圖從訂單和order_address中檢索一些字段。我得到了我的加入生成此SQL查詢:Magento加入錯誤

SELECT `main_table`.`region`, `main_table`.`city`, `order`.* FROM `sales_flat_order_address` AS `main_table` LEFT JOIN `` AS `order` ON order.entity_id = main_table.parent_id WHERE (address_type = 'shipping') AND (region = 'California') GROUP BY `city` 

我可以在查詢中看到:LEFT JOIN '' AS 'order'。這是不正確的。以下是查詢生成的代碼。歡迎任何幫助。

$collection = Mage::getModel('sales/order_address')->getCollection(); 
    $collection 
     ->addAttributeToSelect('region') 
     ->addAttributeToSelect('city') 
     ->addAttributeToFilter('address_type', 'shipping') 
     ->addAttributeToFilter('region', 'California'); 

    $collection->getSelect()->joinLeft(
     array('order' => $this->getTable('sales/order')),//The problem is here! 
     'order.entity_id = main_table.parent_id', 
     array('order.*')) 
     ->group('city'); 

回答

0

我終於解決了它使用普通的表名,如下所示:

$collection->getSelect()->joinLeft(
     array('order' => 'sales_flat_order'), 
     'order.entity_id = main_table.parent_id', 
     array('order.*')) 
     ->group('city'); 

不滿意這個,但它的工作原理。

0
延長 Mage_Adminhtml_Block_Report_Grid

,Magento的核心用下劃線用於獲取表名:

$coreResource = Mage::getSingleton('core/resource'); 
$collection = Mage::getModel('sales/order_address')->getCollection(); 
$collection 
    ->addAttributeToSelect('region') 
    ->addAttributeToSelect('city') 
    ->addAttributeToFilter('address_type', 'shipping') 
    ->addAttributeToFilter('region', 'California'); 

$collection->getSelect()->joinLeft(
    array('order' => $this->getTable('sales_order')), 
    'order.entity_id = main_table.parent_id', 
    array('order.*')) 
    ->group('city'); 
+0

對不起,但它沒有奏效。 – reydelleon