尝试在Magento加入中分组时出错

时间:2014-02-01 21:58:27

标签: magento join grid grouping

我正面临Magento的标题错误。我正在尝试按县分组网格中的记录,这是在连接中获得的字段之一,而不是主表。当我运行针对DB生成的SQL查询时,它按预期运行,但在尝试访问Magento中的网格时,我收到了该错误。这是我的加入:

$collection = Mage::getModel('sales/order_address')->getCollection();
        $collection
            ->addAttributeToSelect('region')
            ->addAttributeToSelect('city')
            ->addAttributeToFilter('address_type', 'shipping')
            ->addAttributeToFilter('region', 'California')
            ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
            ->addFieldToFilter(array('status', 'status'),
                array(
                    array('eq' => 'complete'),
                    array('eq' => 'processing')
                ));

        $collection->getSelect()->joinLeft(
            array('order' => 'sales_flat_order'),
            'order.entity_id = main_table.parent_id',
            array('status' => 'order.status',
                'created_at' => 'order.created_at',
                'grand_total' => 'SUM(order.grand_total)'))
            ->joinLeft(
                array('order_tax' => 'sales_order_tax'),
                'order_tax.order_id = main_table.parent_id',
                array('county' => 'order_tax.title',
                    'tax_amount' => 'SUM(order_tax.amount)',
                    'tax_rate' => 'order_tax.percent'))
            ->group('county')
            ->group('city')
            ->distinct(true);

这是生成的SQL:

SELECT DISTINCT `main_table`.`region`, `main_table`.`city`, `order`.`status`, `order`.`created_at`, SUM(order.grand_total) AS `grand_total`, `order_tax`.`title` AS `county`, SUM(order_tax.amount) AS `tax_amount`, `order_tax`.`percent` AS `tax_rate` FROM `sales_flat_order_address` AS `main_table` LEFT JOIN `mvmt_sales_flat_order` AS `order` ON order.entity_id = main_table.parent_id LEFT JOIN `mvmt_sales_order_tax` AS `order_tax` ON order_tax.order_id = main_table.parent_id WHERE (address_type = 'shipping') AND (region = 'California') AND (created_at >= '2013-08-01 00:00:00' AND created_at <= '2013-12-31 00:00:00') AND ((status = 'complete') OR (status = 'processing')) GROUP BY `county`, `city`

有人知道这里发生了什么以及为什么SQL接受查询但不接受Magento?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

$collection = Mage::getModel('sales/order_address')->getCollection();
        $collection
            ->addAttributeToSelect('region')
    ->addAttributeToSelect('country_id')
            ->addAttributeToSelect('city')
           ->addAttributeToFilter('address_type', 'shipping')
           ->addAttributeToFilter('region', 'California')
           ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
          ->addFieldToFilter(array('status', 'status'),
                array(
                    array('eq' => 'Pending'),
                    array('eq' => 'processing')
                ));
 $collection->getSelect()->joinLeft(
            array('order' => 'sales_flat_order'),
            'order.entity_id = main_table.parent_id',
            array('order.status','order.created_at','SUM(order.grand_total)'));
                $collection->getSelect()
            ->joinLeft(
                array('order_tax' => 'sales_order_tax'),
                'order_tax.order_id = main_table.parent_id',
                array('order_tax.title','SUM(order_tax.amount)','order_tax.percent'))

      ->group('country_id')
            ->group('city')
             ->distinct(true);

希望这对你有用。 :)
祝你好运。