Magento – What is the Problem with My Filtering

categorycollection-filteringcollection;template

In my category/view.phtml, I have tried this code for some test purpose. However I am facing some problems with my filtering. Filtering code look like this.

//gets current category
$current_category = Mage::registry('current_category');

//get essential datas
$category_id = (int)$current_category->getId();
$store = (int)$current_category->getStoreId();

//loads parent category lies in level 2
$parent_category = Mage::getModel('catalog/category')->getCollection()
                        ->addAttributeToSelect(array('level','id','is_active','children'))
                        ->addAttributeToFilter('level', array('eq' => 2))
                        //->addAttributeToFilter('store_id', array('eq' => $store))
                        ->addAttributeToFilter('is_active', array('eq' => 1))
                        //->addAttributeToFilter('children', array('in' => array($category_id)))
                        ->load();

print_r($parent_category->toArray());

It gives me this result in frontend

Array
(
    [10] => Array
        (
            [entity_id] => 10
            [entity_type_id] => 9
            [attribute_set_id] => 12
            [parent_id] => 3
            [created_at] => 2007-08-23 11:45:22
            [updated_at] => 2008-08-08 00:01:18
            [path] => 1/3/10
            [position] => 10
            [level] => 2
            [children_count] => 2
            [is_active] => 1
            [children] => 22,23
        )

    [13] => Array
        (
            [entity_id] => 13
            [entity_type_id] => 9
            [attribute_set_id] => 12
            [parent_id] => 3
            [created_at] => 2007-08-24 13:31:01
            [updated_at] => 2008-08-08 00:02:23
            [path] => 1/3/13
            [position] => 13
            [level] => 2
            [children_count] => 13
            [is_active] => 1
            [children] => 8,12,15
        )

    [18] => Array
        (
            [entity_id] => 18
            [entity_type_id] => 9
            [attribute_set_id] => 12
            [parent_id] => 3
            [created_at] => 2007-08-24 15:44:31
            [updated_at] => 2008-08-07 23:54:16
            [path] => 1/3/18
            [position] => 18
            [level] => 2
            [children_count] => 6
            [is_active] => 1
            [children] => 4,5,19,24
        )
        ...
)

I am facing two problems with this code

Problem-1. shows error when I add store filtering : See my code. When I uncomment this line of code ->addAttributeToFilter('store_id', array('eq' => $store)), it shows following error.

Fatal error: Call to a member function getBackend() on a non-object in /home/rajeev/public_html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816

What is the reason for this error ? (store_id is 1 here)

2.problem-2 : children filtering is also not working

I am standing in category page which has an id 12. With that said, if I uncomment this line ->addAttributeToFilter('children', array('in' => array($category_id))) (commented the store filtering line), it returns me null. It can't, because as you can see I have a category available with category-12 as its children. (check category 13 above).

So why does it returns empty result ?

Please point out the errors that I have made in my code.

Thanks

Best Answer

First the store id thing.
This is useless.
There is no store_id attribute for categories so you cannot filter by it.

Second: children field filter. Try it like this:

 ->addAttributeToFilter('children', array('finset' => $category_id))

[EDIT].
But your code seams like something wrong.
What you are basically doing is to retrieve the top level category collection (level = 2 means top level) and then just limit that collection to one single item. The one that contains the current category as a child category.
Since one category can have only one parent. this can be done easily through

$currentCategory = ....;
if ($currentCategory->getLevel() == 3) { //if second level in tree
    $lookingFor = $currentCategory-getParentCategory();
}
else { //not looking for anything
    $lookingFor = null;
}

[Second degree edit]
If you need the top level category when any of it's children is are requested you can take a different approach. Each category has a path column that holds all the tree node ids starting with the root of all roots until the current category.
Just do an explode by slash and get the third element.
Something like this:

$path = $currentCategory->getPath();
$parts = explode('/', $path);
$rootOfAllRoots = $parts[0];//you don't need this...just added it as a proof of concept
$rootCatalog = $parts[1]; //you don't need this...just added it as a proof of concept
$topLevelCategory = isset($parts[2]) ? $parts[2] : null; //Bingo, you are looking for this.
//rest is not important

You have the id you are looking for, you can load it.

path =  '1/2/10/15/24/30'; 
Root of  | | |         |-----current category id
roots----| | |
           | |
Catalog    | |---------top level parent category
Root ------|
Related Topic