且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento:获取按属性过滤的产品集合的订单项集合

更新时间:2023-11-30 10:05:22

使跨实体选择有效且唯一地工作的唯一方法是通过使用集合选择对象构建SQL.

The only way to make cross entity selects work cleanly and efficiently is by building the SQL with the collections select object.

$attributeCode = 'unique_category_id';
$alias = $attributeCode.'_table';
$attribute = Mage::getSingleton('eav/config')
    ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);

$collection = Mage::getResourceModel('sales/order_item_collection');
$select = $collection->getSelect()->join(
    array($alias => $attribute->getBackendTable()),
    "main_table.product_id = $alias.entity_id AND $alias.attribute_id={$attribute->getId()}",
    array($attributeCode => 'value')
)
->where("$alias.value=?", 75);

这对我来说很好.由于性能原因,我倾向于跳过完整的方式来加入eav_entity_type表,然后是eav_attribute,然后是值表.由于attribute_id是特定于实体的,因此仅需这些.
根据属性的范围,您可能还需要添加商店ID.

This works quite well for me. I tend to skip going the full way of joining the eav_entity_type table, then eav_attribute, then the value table etc for performance reasons. Since the attribute_id is entity specific, that is all that is needed.
Depending on the scope of your attribute you might need to add in the store id, too.