7

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

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

它工作正常,直到我在后端将“使用平面目录产品”设置为是。有没有办法告诉magento不要使用平面表,而使用EAV?
谁能帮我这个。

4

6 回答 6

9

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

于 2011-05-27T20:41:07.607 回答
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。

顺便说一句,Blazo 在第一篇文章中提到的报告集合是该集合的扩展。

于 2011-05-30T10:35:42.957 回答
2

扩展艾伦的答案:

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

    }

}

上面删除了检查是否启用了 flat 并且仅初始化目录/类别资源的标准 eav 版本。

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

$category = Mage::getModel('namespace_module/category')->load($id)
于 2013-12-13T22:51:10.290 回答
2

我有用

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

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

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

于 2016-08-14T10:31:26.690 回答
1

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

  1. 如果您的搜索中确实有许多产品,绕过平面目录表会降低您的网站速度,并且每个搜索代码都会消耗大量资源。
于 2013-03-18T04:08:37.633 回答
-2

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

例如

$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);

从此时起,您可能需要更改其他代码,例如替换

$category->getId()

$category["entity_id"]

我希望这能有所帮助...

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

于 2015-06-22T09:51:01.557 回答