如何在平面目录产品数据打开时从magento EAV表中进行选择

时间:2011-05-12 10:15:52

标签: magento flat

我有从Magento选择最畅销产品的代码:

$productCollection = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty($startTime, $currentTime)
            ->addAttributeToSelect('*')
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc')
            ->setPageSize($this->limit());
    }

它工作正常,直到我在后端设置“使用平面目录产品”为是。 有没有办法告诉magento不使用平台,而是使用EAV? 任何人都可以帮助我。

6 个答案:

答案 0 :(得分:9)

创建一个新的模型类('mycatalog/product'),扩展原始产品类,但对其进行硬编码以使用EAV资源模型和EAV资源集合,然后在查询代码中使用该模型。

答案 1 :(得分:4)

我一直在运行我的代码来自一个独立的php文件,只要我将代码移动到管理模块,它就停止使用flat_file并返回到eav。

如果你看一下:Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection

有一种方法:

public function isEnabledFlat()
{
    if (Mage::app()->getStore()->isAdmin()) {
        return false;
    }
    if (!isset($this->_flatEnabled[$this->getStoreId()])) {
        $this->_flatEnabled[$this->getStoreId()] = $this->getFlatHelper()
            ->isEnabled($this->getStoreId());
    }
    return $this->_flatEnabled[$this->getStoreId()];
}

您可以修改此项以添加根据您自己的条件返回false的额外条件。

BTW,Blazo在第一篇文章中提到的报道集是这个系列的延伸。

答案 2 :(得分:2)

扩展艾伦的回答:

class Namespace_Module_Model_Category extends Mage_Catalog_Model_Category
{
    protected function _construct()
    {
        $this->_init('catalog/category');

    }

}

上面删除了检查以查看是否启用了flat并且只是在目录/类别资源的标准eav verson中。

然后当您希望加载模型时确保获得eav模型,无论是否启用了平面数据:

$category = Mage::getModel('namespace_module/category')->load($id)

答案 3 :(得分:2)

我用过

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

之前

Mage::getModel('catalog/product')->getCollection()

它开始从基于eav的系统中获取数据。

答案 4 :(得分:1)

这是一个老帖子,但我认为没有说明一个重点。 1.将平面目录设置为on后,需要通过cron或admin / shell运行索引器,以便填充平面目录表。

  1. 如果您的搜索中有很多产品绕过平面目录表会降低您的网站速度,每个搜索代码都会占用大量资源。

答案 5 :(得分:-2)

我发现最简单的解决方案是关闭平面表,然后使用 - > load(true)参数获取magento执行的SQL查询

e.g。

$collection = Mage::getModel('catalog/category')->getCollection();
$collection
    ->setStoreId($store->getId())
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute'=>'ig_unifeed_ids', 'like'=>"%:".$this->getId().":%")))
    ->load(true);

然后重新打开平面表并将此代码替换为:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT `e`.*, `at_ig_unifeed_ids`.`value` AS `ig_unifeed_ids` FROM `catalog_category_entity` AS `e` INNER JOIN `catalog_category_entity_varchar` AS `at_ig_unifeed_ids` ON (`at_ig_unifeed_ids`.`entity_id` = `e`.`entity_id`) AND (`at_ig_unifeed_ids`.`attribute_id` = '139') AND (`at_ig_unifeed_ids`.`store_id` = 0) WHERE (`e`.`entity_type_id` = '3') AND ((at_ig_unifeed_ids.value LIKE '%:".$this->getId().":%'))";
$collection = $readConnection->fetchAll($query);

从这一点开始,您可能需要更改其他代码,例如替换

  

$类别 - >的getId()

  

$类别[ “ENTITY_ID”]

我希望这有点帮助...

注意:这是IG_Unifeed模块magento bug的真正解决方案,它在使用平面表时忽略了类别过滤。

相关问题