Magento加入错误

时间:2014-01-30 17:42:00

标签: magento join grid admin

我的网格中有这个代码。我正在尝试从order和order_address中检索一些字段。我正在为我的连接生成此sQL查询:

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');

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

我终于使用普通表名解决了它,如下所示:

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

对此不满意,但确实有效。