3

我正在 Magento 1.7 中创建销售订单项目网格。我在管理员的销售菜单下创建了一个新的子菜单。Order Item 网格将在新行中显示订购的每个产品。因此,网格中可能存在多个 OrderId。我在网格类中的查询是:

$collection = Mage::getResourceModel('sales/order_collection')
        ->join(
            'sales/order_item',
            '`sales/order_item`.order_id=`main_table`.entity_id',
            array(
                'skus'  => `sales/order_item`.`sku`,
                'names' => `sales/order_item`.`name`,
                'order_incharge' => `sales/order_item`.`order_incharge` ,
                'proptions' => `sales/order_item`.`product_options` ,

            ));

我没有任何 GroupBy 子句。当我在日志中打印此查询时,它显示为:

SELECT `main_table`.* FROM `sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_item` AS `sales/order_item` ON `sales/order_item`.order_id=`main_table`.entity_id

我的数据库中有 2 个订单。Order Id =24 有两个产品 Order Id 25 有一个产品 所以上面的查询在数据库上运行时正确显示 3 条记录。但是,这不会显示在网格上。尝试在网格中显示时出现以下错误:

Item (Mage_Sales_Model_Order) with the same id "24" already exist";i:1;s:4274:"#0 C:\wamp\www\bakery\lib\Varien\Data\Collection\Db.php(576): Varien_Data_Collection->addItem(Object(Mage_Sales_Model_Order))

如何解决此问题,以便可以将相同的订单 ID 添加到集合中?

谢谢,尼特

4

2 回答 2

2

You should get the sales/order_item_collection and then join the sales/order table instead.

Your problem is that you are displaying order information per item on the order. Instead you should be getting the item information specifically and blending some order info in with a join.

Depending on what you want, you may not even need to join the sales/order table - just the sales/order_item collection may be all you need.

于 2013-08-11T06:54:33.647 回答
0

感谢马歇尔为我指明了正确的方向。这是我的解决方案,以防有人需要

MY_MODULE_Block_Adminhtml_Order_Grid

protected function _getCollectionClass()
{
   // This is the model we are using for the grid
   // We need to work with the item collection and join the sales/order table because we wanna show one item per line
   return 'sales/order_item_collection';
}

protected function _prepareCollection()
{
    // Get the collection for the grid
    $collection = Mage::getResourceModel($this->_getCollectionClass())

    // Join the order table
    // The order item collection object already implements Mage_Core_Model_Mysql4_Collection_Abstract. 
    // That means, the join method doesn't need an array for the table and you don't need to get the table manually. 
    ->join('order', 'order_id=entity_id')

    // Get the address
    ->join(array('a' => 'sales/order_address'), 'order.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
        'city'       => 'city',
        'postcode'   => 'postcode',
        'region'     => 'region',
        'country_id' => 'country_id',
        'street'     => 'street',
        'telephone'  => 'telephone'
    ))

    // Get the customer group
    ->join(array('c' => 'customer/customer_group'), 'order.customer_group_id = c.customer_group_id', array(
            'customer_group_code' => 'customer_group_code'
    ))

    // Concat customer name
    ->addExpressionFieldToSelect(
        'customer_name',
        'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
        array('customer_firstname' => 'order.customer_firstname', 'customer_lastname' => 'order.customer_lastname'))
    ;
于 2018-05-17T11:03:16.707 回答