我正在开一家只有可下载产品的商店。我需要获得每个客户的可下载产品,并能够按产品名称和sku对其进行排序。我当前的代码可用于检索所有下载链接。
$purchasedItems = Mage::getResourceModel('downloadable/link_purchased_item_collection')
->addFieldToFilter('purchased_id', array('in' => $purchasedIds))
->addFieldToFilter('status',
array(
'nin' => array(
Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PENDING_PAYMENT,
Mage_Downloadable_Model_Link_Purchased_Item::LINK_STATUS_PAYMENT_REVIEW
)
)
);
但是,这不会返回产品名称或sku。所以我正在尝试将集合连接到catalog_product_flat
表。
$productTableName = Mage::getSingleton('core/resource')->getTableName('catalog/product_flat');
$purchasedItems->getSelect()
->join(array('products' => $productTableName), 'main_table.product_id = products.entity_id', array('table_alias.*'));
执行var_dump($purchasedItems)
会返回Mage_Downloadable_Model_Resource_Link_Purchased_Item_Collection Object
。但我无法迭代$purchasedItems
。
非常感谢任何帮助。
答案 0 :(得分:4)
请查看加入magento定制,我在我的项目中使用它并且它工作正常。
语法
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field',array('field_name_you_want_to_fetch_from_db'));
工作查询示例
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id',array('value'));
希望这对你也有用!
干杯!