2016-05-15 36 views
1

我試圖將某些catalog/category屬性左加入sales/order_item集合。Magento:如何將'catalog/category'字段左連接到'sales/order_item'集合?

例如:滿意的結果將讓我添加類別名稱到訂單項目數據。

  • 我知道我可以使用類別平板表,但我需要知道是否有可能加入EAV集合以及如何加入。
  • 它必須完成一個mysql查詢(不通過循環訂單項或類似的東西...)
  • 我知道,如果產品有多個類別,它可能會導致重複的行,這正是我需要的。

在此先感謝!

回答

2

這就是你將如何在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(); 
+0

謝謝!我會試一試。 – Pini

+0

很酷。只是FYI,最後一行'echo $ collection-> getFirstItem() - > getCategoryName();'只是一個示例,如何回顯集合中的第一個項目,以便您可以在其數據中看到類別名稱。 –

+0

經測試,按預期工作。 再次感謝@ShawnAbramson,你爲我節省了很多時間。 Notes/Additions: 我實際上需要按其他列進行分組,因此我刪除了「$ collection-> getSelect() - > group('main_table.item_id');」。 這導致已知的「Item與已存在的ID相同...」的集合錯誤,我通過重置選擇列並添加所需列來解決這個問題,同時用別名替換'item_id': $ collection-> getSelect () - >復位(Zend_Db_Select對象:: COLUMNS); $ collection-> getSelect() - > columns(array('order_item_id'=>'item_id')); – Pini

1

你可以做某事。像這樣:

$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); 
+0

codedge,請參閱我的答案。運行magento方法獲取這些數據要可靠得多。 –

+0

@ShawnAbramson我完全同意你 - 更好的方法是使用Magentos ORM。 – codedge