6

I have some code to perform a basic search of categories. It works by searching the category name and description - attributes that are both core.

The code I have is as follows:

$_categories = Mage::getModel('catalog/category')->getCollection()  
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('description')
    ->addAttributeToSelect('associated_brand')
    ->addAttributeToSelect('image')
    ->setPageSize(12)
    ->setCurPage(1)
    ->addAttributeToFilter('level',['gt' => 2])
    ->addAttributeToFilter('is_active',['eq'=>true])
    ->addAttributeToFilter(
        [
            ['attribute' => 'name', 'like' => '%'.$searchterm.'%'],
            ['attribute' => 'description', 'like' => '%'.$searchterm.'%']
        ]
    )->load();

Prior to enabling the flat catalog for categories, it returned the expected results - any categories that had a match for the search term in their name or description.

With the flat catalog enabled, the name or description search does not work. If I output the underlying collection query, it is as follows:

SELECT `main_table`.`entity_id`, `main_table`.`level`, `main_table`.`path`, `main_table`.`position`, `main_table`.`is_active`, `main_table`.`is_anchor`, `main_table`.`name`, `main_table`.`description`, `main_table`.`associated_brand`, `main_table`.`image` 
FROM `catalog_category_flat_store_1` AS `main_table` 
WHERE (level > 2) AND (is_active = '1')

As can be seen from the above, the name and description clause are missing completely.

I've checked the generated catalog_category_flat_store_1 table and the fields are present there.

Any ideas why Magento is dropping these filters from the query, only when using the flat catalog?

2

In \Mage_Catalog_Model_Resource_Category_Flat_Collection::addAttributeToFilter

public function addAttributeToFilter($attribute, $condition = null)
{
    if (!is_string($attribute) || $condition === null) {
        return $this;
    }

    return $this->addFieldToFilter($attribute, $condition);
}

It seems not possible to have an array in first parameter. You need to get the Select object and do your query manually when flat is enabled.

Hope this help,

  • So this is just another example where flat catalog implementation is not interface compatible with standard EAV implementation? – BrynJ Nov 30 '15 at 17:20
  • unfortunately yes. This is a big problem in Category. When I start a project I usually start using flat or EAV but never change in project life cycle. – bchatard Nov 30 '15 at 18:19
  • What if you use addFieldToFilter() directly instead? I don't see a reason why it should not work – Fabian Schmengler Dec 13 '15 at 10:37
  • Yes it will work, only if you always use flat (doesn't works on EAV) – bchatard Dec 14 '15 at 9:47

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.