我正在Magento 1.7中创建销售订单项网格。我在管理员的销售菜单下创建了一个新的子菜单。 订单项目网格将显示在新行中订购的每个产品。因此,网格中可能存在多个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个订单。 订单ID = 24有两个产品 订单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添加到集合中?
谢谢, 尼特
答案 0 :(得分:2)
您应该获得sales/order_item_collection
,然后加入sales/order
表格。
您的问题是您在订单上显示每件商品的订单信息。相反,您应该专门获取商品信息,并将一些订单信息与加入相混合。
根据您的需要,您甚至可能无需加入sales/order
表格 - 只需sales/order_item
集合即可。
答案 1 :(得分:0)
感谢Marshall指出我正确的方向。这是我的解决方案,以防有人需要它
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'))
;