Magento:如何将 LEFT-JOIN 'catalog/category' 字段转换为 'sales/order_item' collection?

Magento: How to LEFT-JOIN 'catalog/category' fields to 'sales/order_item' collection?

我正在尝试 left-join 一些 catalog/category 属性到 sales/order_item collection。

例如: 满意的结果会让我将 category-name 添加到 order-item 数据中。

提前致谢!

你可以做某事。像这样:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');

$sql = 'SELECT `main_table`.*, `products`.*, `cname`.* FROM `sales_flat_order_item` AS `main_table`
 LEFT JOIN `catalog_category_product` AS `products` ON main_table.product_id=products.product_id
 LEFT JOIN `catalog_category_entity_varchar` AS `cname` ON `products`.`category_id`=`cname`.`entity_id`
';

$result = $readConnection->fetchAll($sql);  

这就是您在 Magento 世界中的做法。请注意集合中的 "group by"。这是因为在左连接中,当同一个主键有多个结果时,Varien_Data_Collection 会抛出一个错误,表明具有该 ID 的项目已经存在。无论如何,我为你写了这个并测试了它。

我的答案还获取类别名称的存储值,如果存储值不存在,则回退到默认值。

<?php

include 'app/Mage.php';
Mage::app();

$itemModel            = Mage::getModel('sales/order_item');
$itemResource         = $itemModel->getResource();
$categoryProductTable = $itemResource->getTable('catalog/category_product');

$categoryModel    = Mage::getModel('catalog/category');
$categoryResource = $categoryModel->getResource();
$name             = $categoryResource->getAttribute('name');
$nameTable        = $name->getBackendTable();
$defaultStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;

$collection = $itemModel->getCollection();
$collection->getSelect()->joinLeft(array('category_product_table' => $categoryProductTable), 'main_table.product_id = category_product_table.product_id', array());
$collection->getSelect()->joinLeft(array('store_category_name_table' => $nameTable),
                                   "store_category_name_table.entity_id = category_product_table.category_id AND
                                    store_category_name_table.attribute_id = {$name->getAttributeId()} AND
                                    store_category_name_table.entity_type_id = {$name->getEntityTypeId()} AND
                                    store_category_name_table.store_id = main_table.store_id",
                                   array()
);
$collection->getSelect()->joinLeft(array('default_category_name_table' => $nameTable),
                                   "default_category_name_table.entity_id = category_product_table.category_id AND
                                    default_category_name_table.attribute_id = {$name->getAttributeId()} AND
                                    default_category_name_table.entity_type_id = {$name->getEntityTypeId()} AND
                                    default_category_name_table.store_id = {$defaultStoreId}",
                                   array()
);
$collection->getSelect()->columns(array('category_name' => new Zend_Db_Expr('COALESCE(store_category_name_table.value,default_category_name_table.value)')));
$collection->getSelect()->group('main_table.item_id');

echo $collection->getFirstItem()->getCategoryName();