首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento:如何将“目录/类别”字段左转到“sales/order_item”集合?

问Magento:如何将“目录/类别”字段左转到“sales/order_item”集合?
EN

Stack Overflow用户
提问于 2016-05-15 12:50:17
回答 2查看 2.5K关注 0票数 1

我试图将一些catalog/category属性左转到一个sales/order_item集合中。

例如:一个令人满意的结果将让我将类别名称添加到订单项数据中。

  • 我知道我可以使用类别平面表,但我需要知道是否有可能加入EAV-集合和如何加入。
  • 它必须通过一个mysql查询来完成(而不是通过循环遍历订单项或类似的.)
  • 我知道,如果产品有超过一个类别,这可能会导致重复行,这正是我所需要的。

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-16 05:58:16

这就是你在Magento世界里所做的事情。注意集合中的"group“。这是因为使用左联接时,当同一主键有多个结果时,Varien_Data_Collection抛出一个错误,表明具有该ID的项已经存在。不管怎样,我给你写了这个并测试过了。

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

代码语言:javascript
复制
<?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();
票数 2
EN

Stack Overflow用户

发布于 2016-05-15 15:26:57

你可以做某事。就像这样:

代码语言:javascript
复制
$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);  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37238512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档