使用自定义表中的 item_id 获取 Sku
我正在创建一个自定义模块。有两张新桌子。
表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。如何通过关联 table2 中的 item_id
来获取集合中的 SKU?
感谢您的任何帮助或建议!
I am creating a custom module. There are two new tables.
Table1: t1_id
(PK), order_id
(FK)
Table2: t2_id
(PK), t1_id
(FK), item_id
(FK).
Table2.item_id
is equivalent to sales_flat_order_item.item_id
. I am creating a custom report and in the collection need to show the SKU. At first I tried the following:
$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');
However, the product.entity_id is actually the product_id of sales_flat_order_item.item_id
. How can I get the SKU in the collection, by relating to the item_id
in table2?
Thanks for any help or suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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')
// 加入项目 ID
->join(array('order_item' => $tbl_order_item),
'tbl_two.item_id = order_item.item_id');
It took a little bit of digging. It helps to go to the config.xml for the core model you are looking at. This way you know which table in the database getTableName goes to.
$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');