我正在创建一个自定义模块。有两个新表。
表1:t1_id
(PK),order_id
(FK)
表2:t2_id
(PK),t1_id
(FK),item_id
(FK)。
Table2.item_id
相当于sales_flat_order_item.item_id
。我正在创建自定义报告,并且在集合中需要显示SKU。起初我尝试了以下内容:
$collection = Mage::getModel('custom/two')->getCollection();
$tbl_product_collection = Mage::getSingleton('core/resource')->getTableName('catalog/product');
$tbl_one = Mage::getSingleton('core/resource')->getTableName('custom/one');
$tbl_two = Mage::getSingleton('core/resource')->getTableName('custom/two');
$collection->getSelect()
->from(array('tbl_one' => $tbl_one))
->join(array('tbl_two' => $tbl_two),
'tbl_two.t1_id = tbl_one.t1_id')
// Join with Item ID on Simple Product
// Showing wrong SKU
->join(array('product' => $tbl_product_collection),
'tbl_two.item_id = product.entity_id');
但是,product.entity_id实际上是sales_flat_order_item.item_id
的product_id。如何通过与表2中的item_id
相关联来获取集合中的SKU?
感谢您提供任何帮助或建议!
答案 0 :(得分:1)
花了一点时间挖掘。它有助于转到您正在查看的核心模型的config.xml。这样您就可以知道数据库getTableName中的哪个表。
$tbl_product_collection = Mage::getSingleton('core/resource')->getTableName('sales/order_item');
$tbl_one = Mage::getSingleton('core/resource')->getTableName('custom/one');
$tbl_two = Mage::getSingleton('core/resource')->getTableName('custom/two');
$collection->getSelect()
->from(array('tbl_one' => $tbl_one))
->join(array('tbl_two' => $tbl_two),
'tbl_two.t1_id = tbl_one.t1_id')
// Join Item ID
->join(array('order_item' => $tbl_order_item),
'tbl_two.item_id = order_item.item_id');