Magento – How to load category using url_key and parent catgory_id

catalogcategory

How to get category using url_key and parent category_id?
If category structure is as follows

-Root
  --Mobile
    ---Android
    ---Windows
    ---iOs
  --Tablet
    ---Android
    ---Windows
    ---iOs

Then using following code if try to load iOs category using url_key it load always load root->Mobile->iOS

$category = Mage::getModel('catalog/category')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByAttribute('url_key', 'cat_urlkey');

How I can modify code so that, sub category load by url_key and respective parent category_id?

Best Answer

You can load the attributes collection and get it's first element:

$categories = Mage::getResourceModel('catalog/category_collection');
$categories->addAttributeToSelect('*');

$categories->addAttributeToFilter('url_key', 'cat_urlkey');
$categories->addAttributeToFilter('path', array('like' => '1/15/%');

The second condition tells to take subcategories of the category with id 15.