问题
I rack my brain with two hard nuts. Thus the problems are linked together I post them in one thread. Unfortunately I am not able to provide a Test Example. Here we go …
I use following Query in my Joomla 2.5 template:
->select(array('a.description','a.display_name','a.parent_id','a.filename','a.url', 'b.title','b.alias', 'b.id','b.catid','b.state','c.title as catTitle','c.parent_id as catparent'))
->from('#__attachments AS a')
->join('LEFT', '#__content AS b ON (a.parent_id = b.id)')
->join('INNER', '#__categories AS c ON b.catid = c.id')
->where("b.state = 1 AND c.parent_id in (23)");
this joins me the module table "#_attachments" with "#_content" and "#_articles". with the goal to get the attached images of an article, the title of the article, the alias, the article id, the category title, the category id,...
My Questions
The query gives me the id of the parent category ('c.parent_id as catparent') of the category from the article. Is it possible to have a subselect which also gives me the title of this parent-category (the id is in catparent)?
Thus I only want articles from special subcategories I have an "in"n in my where. know all subcategories are childs of parent category with id (5). How do I get all Id´s of the subcategories from cat 5. So I can use this Id´s in my "b.state = 1 AND c.parent_id in ($allSubCategoryIDs)" where clause?
Kind regards,
Tony
回答1:
ad 1.
To fetch the title of the parent category, add
->select('parent.title AS parentTitle')
->join('left', '#__categories AS parent ON parent.id=c.parent_id')
to your query.
ad 2.
Since Joomla stores categories as Nested Sets, this is accomplished by changing
->where("b.state = 1 AND c.parent_id in (23)");
into
->where('b.state=1')
->where('c.lft BETWEEN parent.lft AND parent.rgt')
来源:https://stackoverflow.com/questions/15708179/joomla-2-5-get-all-ids-of-subcategories-and-do-subselect-in-query