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