How to select from magento EAV tables when flat catalog product data is on

戏子无情 提交于 2019-12-03 13:40:22

Create a new model class ('mycatalog/product') that extends the original product class but hard code it to use the EAV resource model and EAV resource collection, and then use that model in your query code.

I'd been running my code from a stand alone php file, as soon as i moved my code into an admin module it stopped using the flat_file and went back to eav.

If you look at: Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection

There's a method:

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()];
}

You could modify this to add an extra condition that returns false based on your own criteria.

BTW, The reports collection mentioned in the first post by Blazo is an extension of this collection.

To expand on Alan's answer:

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

    }

}

The above removes the check to see if flat was enabled and only inits the standard eav verson of the catalog/category resource.

And then when you wish to load your model ensuring that you get the eav model regardless of wither the flat data is enabled:

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

I have use

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

before

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

And it start fetching data from eav based system.

This is an old post but I thought one important point was not stated. 1. Once you set flat catalog to on you need to run indexer via cron or via admin/shell so that flat catalog tables get populated.

  1. If you do have many products in your search bypassing flat catalog table will slow down your site and each search code will consume lots of resources.

I found that the easiest solution was to turn off the flat tables and then get the SQL query that magento executes using the ->load(true) parameter

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

then turn flat tables back on and replace this code with:

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

From this point on you will probably need to change other code like replacing

$category->getId()

with

$category["entity_id"]

I hope this helps a bit...

NOTE: this is a real solution for the IG_Unifeed module magento bug that disregards category filtering when using flat tables.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!