19

I am in the process of optimizing magento store, and I've run across a couple of posts that recommend using the Flat Product Catalog for stores with a large amount of SKUs.

As I have over 10K products I thought I'd give it a try. However, when using the Flat Product Catalog only a select few attributes are loaded in product object (e.g. SKU, name, short description). And my template displays a few other attributes in the search/browse view, such as Manufacturer and Color.

Is there a way to add these attributes to the flat product catalog table so they too can be accessed?

4 Answers 4

45

1.4.x.x, just go into the attributes you want to be used in the "Flat Product Catalog" and make sure the property "Used in Product Listing" is set to Yes. Upon making changes, reindex "Flat Product Data"

The following properties cause the attribute to be included in the "Flat Product Catalog":

"Use in Layered Navigation" = Yes
"Used in Product Listing" = Yes
"Used for Sorting in Product Listing" = Yes
1
  • 4
    "Used in Product Listing" seems to be the key. I had several attributes that were already "Use in Layered Navigation" but they weren't showing up in the catalog_product_flat_1 table until I set "Used in Product Listing" too.
    – ben
    Jun 8, 2011 at 16:21
11

I have been working through this issue too, which I would describe as "cannot access product collection attribute in flat mode" or "addAttributeToSelect not working in flat mode".

I found a "clean" solution which:

  • does not require the attribute to have specific settings in admin (it might be added by a user, or hidden on the front end)
  • works for both flat and non-flat mode

Please note - in the code below I have used the associated product collection, but this applies to any product collection (specifically, anything inheriting from Mage_Eav_Model_Entity_Collection_Abstract)

Failing code:

$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
    ->addAttributeToSelect( 'my_custom_attribute' )
;

In flat mode, the above code silently fails to add the attribute if it happens not to be in the flat table.

Working code:

$_product = Mage::getModel('catalog/product')->loadByAttribute( 'sku', 'ABC123' );
$coll = $_product->getTypeInstance()->getAssociatedProductCollection()
    ->joinAttribute( 'my_custom_attribute', 'catalog_product/my_custom_attribute', 'entity_id', null, 'left' )
    ->addAttributeToSelect( 'my_custom_attribute' )
;

The joinAttribute method adds a join to the query. It works even if this duplicates an attribute that's already in the flat table.

Note that I've used a left join there, to ensure that it fetches products if my_custom_attribute is not set on those products. You can change that for inner if you're only interested in rows where my_custom_attribute is set.

(tested in CE 1.6.2.0)

2
  • 1
    The only logical solution to the problem! Thank you Jun 9, 2015 at 7:25
  • nice. This is still relevant for magento 2!
    – Dmitry
    Nov 14, 2019 at 9:15
5

Others have provided the usually sufficient answer (setting Used In Product Listing to Yes), but I found an edge case that would have been useful to know when I was searching for this same thing.

If you are using a custom source model for the custom attribute you would like to include in the flat product table, you will have to override getFlatColums() in the source model class. Yes, I know it's misspelled, but that's native Magento for you.

Sample:

public function getFlatColums() {
    return array($this->getAttribute()->getAttributeCode() => array(
        'type'      => 'tinyint',
        'unsigned'  => true,
        'is_null'   => true,
        'default'   => null,
        'extra'     => null
    ));
}

Source: http://www.dconstructing.com/2012/03/14/custom-product-attributes-and-flat-database-tables-in-magento

2

If you want to make use of an attribute in a module while using the flat product table (filter a collection of products for example) you could add it to a module's Module_File.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Your_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Your_Module>
    </modules>
    <frontend>
        <product>
            <collection>
                <attributes>
                    <your_custom_attribute /> <!-- This is the attribute name -->
                </attributes>
            </collection>
        </product>
    </frontend>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.