0

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

1

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 ------|
8
  • categories do not have a store_id !!!! I think when we output a category object, it holds a store_id data. Actually before making this filtering, I will make a look on a category object, by simply print it in screen. Then how it do not have a store id. – Rajeev K Tomy Aug 11 '14 at 13:47
  • 1
    because it is previously set using setStoreId(). Having a property does not mean it has an attribute. Just as well you can do $category->setAsdfghjkl('wertyuio') and it will still show up. this does not mean the attribute exists. – Marius Aug 11 '14 at 13:50
  • oh ok. Thanks for that valuable point. Marius. and about your edit, I know this will work and I don't want to any category loading then. But what would I do if I am in 4, 5 levels of category ? So what I need to do is, I want to load first level parent category when any of its child categories requested. I know it's wiered idea. But marius this is a part of my study. I just want to play with magento. Its my hobby :) – Rajeev K Tomy Aug 11 '14 at 13:54
  • you cleared my first problem. But solution for my second problem didn't go away with your answer. Still i got an empty array – Rajeev K Tomy Aug 11 '14 at 13:57
  • 2
    @programmer_rkt I added a possible solution to your problem. Next time, post the problem you have along with the approach you took. maybe there is an easier way :D. See if this fits your needs. – Marius Aug 11 '14 at 14:02

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.