我已经看到了很多关于如何做到这一点的例子。但是所有示例都是针对不同版本的 Magento。他们都没有在我的情况下工作。我的版本是 1.4.0.0rc-1。是的,我知道这不应该在生产环境中,但不幸的是这里就是这种情况。
问题是 Mage/Adminhtml/Block/Sales/Order/Grid.php 类中的 _prepareCollection() 函数与我见过的所有其他示例不同。所以不知道该怎么做。
这是该功能的代码:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_collection')
->addAttributeToSelect('*')
->joinAttribute('sku','sales_flat_order_item', null, null, 'left')
->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
->addExpressionAttributeToSelect('billing_name',
'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
array('billing_firstname', 'billing_lastname'))
->addExpressionAttributeToSelect('shipping_name',
'CONCAT({{shipping_firstname}}, IFNULL(CONCAT(\' \', {{shipping_lastname}}), \'\'))',
array('shipping_firstname', 'shipping_lastname'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
我尝试添加左连接,但没有用。这是我在 setCollection 语句之前尝试过的:
$collection->getSelect()->join('sales_flat_order_item', 'order_id=entity_id', array('name'=>'name', 'sku' =>'sku'), null,'left');
但它打印出以下错误:
SELECT `e`.*, `_table_billing_address_id`.`value` AS `billing_address_id`, `_table_billing_firstname`.`value` AS `billing_firstname`, `_table_billing_lastname`.`value` AS `billing_lastname`, `_table_shipping_address_id`.`value` AS `shipping_address_id`, `_table_shipping_firstname`.`value` AS `shipping_firstname`, `_table_shipping_lastname`.`value` AS `shipping_lastname`, CONCAT(_table_billing_firstname.value, " ", _table_billing_lastname.value) AS `billing_name`, CONCAT(_table_shipping_firstname.value, IFNULL(CONCAT(' ', _table_shipping_lastname.value), '')) AS `shipping_name`, `sales_flat_order_item`.`name`, `sales_flat_order_item`.`sku` FROM `sales_order` AS `e`
LEFT JOIN `sales_order_int` AS `_table_billing_address_id` ON (_table_billing_address_id.entity_id = e.entity_id) AND (_table_billing_address_id.attribute_id='112')
LEFT JOIN `sales_order_entity_varchar` AS `_table_billing_firstname` ON (_table_billing_firstname.entity_id = _table_billing_address_id.value) AND (_table_billing_firstname.attribute_id='202')
LEFT JOIN `sales_order_entity_varchar` AS `_table_billing_lastname` ON (_table_billing_lastname.entity_id = _table_billing_address_id.value) AND (_table_billing_lastname.attribute_id='204')
LEFT JOIN `sales_order_int` AS `_table_shipping_address_id` ON (_table_shipping_address_id.entity_id = e.entity_id) AND (_table_shipping_address_id.attribute_id='113')
LEFT JOIN `sales_order_entity_varchar` AS `_table_shipping_firstname` ON (_table_shipping_firstname.entity_id = _table_shipping_address_id.value) AND (_table_shipping_firstname.attribute_id='202')
LEFT JOIN `sales_order_entity_varchar` AS `_table_shipping_lastname` ON (_table_shipping_lastname.entity_id = _table_shipping_address_id.value) AND (_table_shipping_lastname.attribute_id='204')
INNER JOIN `sales_flat_order_item` ON order_id=entity_id WHERE (e.entity_type_id = '11') ORDER BY `e`.`created_at` desc, `e`.`created_at` desc LIMIT 20
有人可以帮我解决这个问题吗?为了您的信息,SKU 字段位于“Sales_flat_order_item”表中。