Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

magento

flat

I have this code for selecting best selling products from Magento:

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

and it works fine, until I set "use flat catalog product" in backend to yes. Is there any way to tell magento to not use flat tables, and use EAV instead?
Can any one help me with this.

like image 630
Blazo Avatar asked May 12 '11 10:05

Blazo


People also ask

What is Flat table in Magento 2?

Magento 2 Flat tables: what are they? A Flat table is an additional table storing all information about a product or a category. Working principle here is fairly simple: the shorter the query to the database, the better the store performance.

What is flat catalog category in Magento 2?

Using Flat Catalog will help you speed up your product collection instead of managing the catalog data in many tables by the Entity Attribute Value (EAV). When your store applies the Flat Catalog, the new tables on the fly are generated and they will store all necessary data related to your products or categories.

What is catalog search in Magento?

Advanced Catalog Search Magento Advanced Search allows customers to be more specific about the products they are looking for. They can search for a product not only by Product Name but also SKU, Price, Long and Short Descriptions.


5 Answers

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.

like image 109
Alan Storm Avatar answered Oct 06 '22 22:10

Alan Storm


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.

like image 29
Richpc Avatar answered Oct 06 '22 23:10

Richpc


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)
like image 25
ajameswolf Avatar answered Oct 06 '22 22:10

ajameswolf


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.

like image 34
Akbar Adeeb Avatar answered Oct 07 '22 00:10

Akbar Adeeb


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.
like image 43
Oscprofessionals Avatar answered Oct 06 '22 23:10

Oscprofessionals