我试图将一些catalog/category属性左转到一个sales/order_item集合中。
例如:一个令人满意的结果将让我将类别名称添加到订单项数据中。
提前感谢!
发布于 2016-05-16 05:58:16
这就是你在Magento世界里所做的事情。注意集合中的"group“。这是因为使用左联接时,当同一主键有多个结果时,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();发布于 2016-05-15 15:26:57
你可以做某事。就像这样:
$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); https://stackoverflow.com/questions/37238512
复制相似问题