【问题标题】:Magento: how to filter the result of Mage::getResourceModel('eav/entity_attribute_collection') by CategoryMagento:如何按类别过滤 Mage::getResourceModel('eav/entity_attribute_collection') 的结果
【发布时间】:2011-04-06 14:51:04
【问题描述】:

我已使用以下代码获取制造商的完整列表。

    $currentCategory = Mage::registry('current_category');
    $product = Mage::getModel('catalog/product');       
    $attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getResource()->getTypeId())->addFieldToFilter('attribute_code', 'brand_name');      
    $attribute = $attributes->getFirstItem()->setEntity($product->getResource());       
    $manufacturers = $attribute->getSource()->getAllOptions(false);      

现在,如何获取属于特定类别的制造商列表?我一直在挖掘一段时间以找到一种方法来按类别限制结果,但没有运气。 ResourceModel 可以按类别过滤吗?

谢谢。

【问题讨论】:

    标签: magento attributes categories


    【解决方案1】:

    我认为您最好从该类别中的产品集合开始,然后获取与这些产品相匹配的属性。类似的东西:

    $currentCategory = Mage::registry('current_category');
    $products = $currentCategory->getProductCollection();
    foreach($products as $product):
      $product = Mage::getModel('catalog/product')->load($product->getId());
      $manufacturers[] = $product->getBrandName();      
    endforeach;
    

    您需要对数组进行重复数据删除,但 array_unique() 应该可以帮到您。

    HTH, 京东

    编辑

    这将防止需要加载每个产品,应该会提高性能:

    $currentCategory = Mage::registry('current_category');
    $products = $currentCategory->getProductCollection();
    $products->addAttributeToSelect('brand_name');
    $products->load();  //execute the query
    $manufacturers = $products->toArray(array('brand_name'));
    

    请记住,使用缓存时,性能损失只会花费您一次。

    京东

    【讨论】:

    • 我这样做了,但性能与以前的方法不匹配。还有什么想法吗?谢谢
    • 谢谢!我会尝试一下,然后告诉你它如何提高性能。
    【解决方案2】:

    感谢您提供获取属性选项 ID 的好方法。它真的帮了我很多! 这是我最终获取选项 id 及其文本值的方法:

    $currentCategory = Mage::registry('current_category');
    $products = $currentCategory->getProductCollection();
    $products->addAttributeToSelect('brand_name');
    $products->load();  //execute the query
    $manufacturers = $products->toArray(array('brand_name'));   
    $temp = array();
    foreach($manufacturers as $v) {
        $temp[] = $v['brand_name'];
    }
    $brands = implode(",",array_unique($temp));
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    $readresult=$write->query("SELECT eav_attribute_option_value.option_id, eav_attribute_option_value.value FROM eav_attribute_option_value INNER JOIN eav_attribute_option ON eav_attribute_option_value.option_id=eav_attribute_option.option_id WHERE eav_attribute_option.attribute_id=505 AND eav_attribute_option_value.option_id IN (".$brands.")");
    
    while ($row = $readresult->fetch() ) {
        $brandlist[] = array('value'=>$row['option_id'], 'label'=>$row['value']);
    }
    

    brandlist 数组将具有选项 id 和 value,以便我可以使用它来链接。

    再次感谢。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2012-07-12
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多