I have a question that might seem simple, but yet I was unable to find the answer. Unlike articles, which are stored in table jos_content, categories in table jos_categories lack any column named ordering or any other that would have the desired information stored. I also tried to find anything similar in the jos_assets table, but it did not help either.
I am hacking the content component a little and I need to get my child categories ordered by the ordering when calling $parent->getChildren() or just find the ordering column so I can create my own query even though it's not clean, I just need to get it working ASAP.
So where can I find category ordering or how do I force getChildren method to return ordered results?
Thanks in advance, Elwhis
In Joomla categorises' order is stored in table "jos_categories" as hierarchical tree structure with a set of linked nodes. Columns used to set order are: "parent_id", "lft", "rgt" and "level".
Assets and menu items are stored in the same way.
You can read more about "Tree traversal" on wiki
Edit:
From Joomla 1.6 to load a specific category and all its children in a JCategoryNode object use:
jimport( 'joomla.application.categories' );
$extension = 'Content'; // com_content
$options['countItems'] = true;
$categoryId = 0;
$categories = JCategories::getInstance($extension, $options);
$categories->get($categoryId);
Related
In a category list, I'd like to be able to display the list of articles dynamically. The website I'm developing allows a user to "submit" an article once they have read it. When they hit the submit button, the ID of that article is stored in the database in a table called "completed_quests." What I need to do is have Joomla check to see if the article ID exists in both the "content" and the "completed_quests" tables. If the article ID exists in both tables, then the article should not be displayed in the category list as that article or "quest" has already been submitted. If the article ID exists in the "content" table, but NOT in the "completed_quests" table, then the article SHOULD be displayed in the category list as it has note yet been submitted.
I'm wondering if there is a specific core Joomla file I should override to alter the category list output, or if I should develop a custom module to create this dynamic list. Any guidance would be much appreciated. If you have any other thoughts about how to display this dynamic list, I'm all ears.
edit: I've started by developing a custom module within Joomla, at least for testing. The below code is not working as expected. When I take out the following line "WHERE arp2i_completed_quests.id IS NULL" it displays a list of articles that exist in both tables. But what I need it to do is display the rows that exist in the content table but NOT in the completed_quests table. When I add the WHERE , it displays the text "ID:" and "TABLE:" for each row that exists, but the actual id and title from that row is not echo'ed to the screen. Please help.
Working code (see comments).
<?php
$query = "SELECT * FROM arp2i_completed_quests RIGHT JOIN arp2i_content ON arp2i_content.id=arp2i_completed_quests.id WHERE arp2i_completed_quests.id IS NULL LIMIT 0, 30 "; // prepare query
$db = &JFactory::getDBO(); // get database object
$db->setQuery($query); // apply query
$articles = $db->loadObjectList(); // execute query, return result list
foreach($articles as $article){ // loop through articles
echo 'ID:' . $article->id . ' Title: ' . $article->title . '<br />';
}
?>
You could override the com_content's listings view in your template.
You do that by copying default.php from /components/com_content/views/categories/tmpl/ to /templates/yourtemplate/html/com_content/categories/
However, I believe that such template overrides only allow you to update the display files of modules and components (the default.php files), and not the models, which would mean you have to add database queries into this layout file (or into a library), which feels a very hacky approach. It should work, though.
A better solution would be to create a bespoke component with a single view, and a model that queries your database to create the appropriate listings. It may even make sense to just make the whole thing a bespoke component (saving the articles in that component) and not use Joomla's articles manager at all.
If your listings page is not paginated, or if you do not care about an SEO friendly URL on secondary pages, then you also could create a module to display the listings and insert that into an article area. Modules are often simpler to make than components. You enter the database query into the helper file of a module.
You should just develop a content view that does exactly what you want. Should be very simple if you have some programming experience. You needn't change any core files.
I am getting a collection of categories using
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
For each of these categories, I need to retrieve the sort order. How do I do this?
The only function I can find that looks relevant in the category class is getDefaultSortBy(), which always returns news_from_date, which is neither the default sort order (position) or the selected sort order (price), so I don't know where that's getting its value from. I also noticed that if, in the magento backend, I change the list of available sort orders, the function getAvailableSortByOptions still always returns the same array. From these two facts, I conclude that the category functions must be accessing some sort of cross-category global settings, which is useless to me.
I want the specific sort order chosen for each specific category. Is there any way to retrieve this? Or do I need to write my own SQL? In that case, which table do I need to query?
I am using magento enterprise ver. 1.11.1.0
Found the answer. Naturally, moments after I posted the question!
The problem is that Mage::getModel('catalog/category')->getCollection() does NOT automatically load all of the category attributes. You have to indicate which ones to retrieve manually. So, I need to replace this:
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
With this:
foreach(
Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('default_sort_by')
->addAttributeToSelect('available_sort_by')
as $category
)
I am retrieving all category IDs that a product belongs to like so in a helper:
$productCategories = $this->getProduct()->getCategoryIds();
This gives me an array of category IDs. In my case, each product will only be in one sub-category. From the IDs I need to figure out the logical order based on which categories are children of which.
The aim is to create a method which I can call and specify the level of category I want for a product and it will return the path to that category.
I know I can load a category like so:
Mage::getModel('catalog/category')->load($categoryId)
and I know what I'm doing in PHP, but just a little stuck on the logic (and methods) to use here to achieve what I want.
The easiest way is to test if it has a Parent Id associated to it. A parent_id that is > 0 means that it is a lower-level category. If the parent_id equals one it means that it is one level lower than the root category.
$category = Mage::getModel('catalog/category')->load($categoryId)
echo $category->getParentId();
I advise against using raw SQL as it is bad practice, the database schema may at some point change.
I am using the adjacency list model to find sub categories within my website. I have working PHP code to find all the categories and sub categories, but now I cannot figure out how use that to create a navigation system. Here is how the site will work, very basic:
URL string
There will be a main category, followed by levels
index.php?category=category-name&level1=sub-category&level2=another-sub-category&level3=content-item
Later I will make SEO friendly links.
URL with no sub categories
Where Level 1 is the content item
www.website.com/category/content-item/
URL with sub categories
Where Level 1, 2, 3, etc are the sub categories and the final level is the content item
www.website.com/category/sub-category/sub-category-2/content-item/
Here is the code I am using to find categories and sub categories. Currently it just outputs a list of all categories and sub categories and number's the level of each child. Not sure if this helps, it just creates a list.
function display_children($ParentCategoryID, $Level) {
// retrieve all children of parent
if ($ParentCategoryID == ''){
$Result = mysql_query('SELECT * FROM categories WHERE parent_category_id IS null');
}
else{
$Result = mysql_query('SELECT * FROM categories WHERE parent_category_id="'.$ParentCategoryID.'";');
}
// display each child
while ($Row = mysql_fetch_array($Result)) {
echo str_repeat('-',$Level)."[".$Level."]".$Row['category_name']."<br />";
display_children($Row['category_id'], $Level + 1);
}
}
See this question first for options on how to represent hierarchical data in a database.
Adjacency list is great for its simplicity, and makes changes easy, but can be awful because it leads to recursive code, such as your function above, in practice, which is a performance killer under load. The best approach, absent changing your data model is using MySQL session variables to retrieve the entire hierarchy in one query, which brings back all the data you need in one database call. Even this though leads to poor performance under load - less so than the recursive function - but still not good; and, I write from experience :).
If it was me I'd use either Nested Sets, Adjacency List in combination with some denormalizations, such as the Bridge Table and Flat Table, or just a Lineage Table. Really depends on how often the data changes and if you need those changes to be done easily. All of these options should be much, much faster, to work with rather than relying upon just the parent-child ID columns.
If I had 2 tables, say blog_category and blog, each "blog" can belong in a particular category only so a 1-1 relationship based on a key called "blog_category_id".
Now in my code I would do something like:
//Loop through categories such as
foreach($categories as $cat):
//then for each category create an array of all its posts
$posts = $cat->getPosts(); // This would be another DB call to get all posts for the cat
//do stuff with posts
endforeach;
Now to me this seems like it could end up quite expensive in terms of DB calls depending on the size of $categories. Would this still be the best solution to do this? Or would I be able to do something in the code and first retrieve all the categories, then retrieve all the blogs and map them to their corresponding category via the id somehow? This would in theory be only 2 calls to the DB, now size wise the result set for call 2 (the blogs) would definitely be larger, but would the actual DB call be as expensive?
I would normally go for the first option, but I'm just wondering if there would be a better way of approaching this or is it more likely that the extra processing in PHP would be more costly in terms of performance? Also specifically from an MVC perspective, if the model returns the categories, but it should also return the corresponding blogs for that category, I'm not sure how best to structure this, from my understanding, shouldn't the model return all the data required for the view?
Or would I be better off selecting all categories and blogs using inner joins in the first query and create the output I need of this? Perhaps by using a multi-dimensional array?
Thanks
You can use a simple SQL query to get all categories and posts like the following:
SELECT *
FROM posts p
JOIN categories c ON c.id = p.blog_category_id
ORDER BY c.category_name ASC,
p.posted_date DESC
Then when you loop over the returned records assign the current category id to a variable, which you can use to compare against the next records category. If the category is different then print the category title before printing the record. It is important to note that for this to work you need to get the posts ordered by category first and then post so that all posts in the same category are together.
So for example:
$category_id = null;
foreach($posts as $post) {
if($post['blog_category_id'] != $category_id) {
$category_id = $post['blog_category_id'];
echo '<h2>' . $post['category_name'] . '</h2>';
}
echo '<h3>' . $post['post_title'] . '</h3>';
echo $post['blog_content'];
}
Note: as you have not posted up the schema of these two tables I have had to make up column names that are similar to what I would expect to see in code like this. So the code above will not work with your code without some adjustments to account for this.
The best solution depends on what you are going to do with data.
Lazy loading
Load data when you need it. It's a good solution when you have, for instance, 20 categories and you load posts for only 2 of them. However, if you need to load posts for all of them it won't be efficient at all... It's called a n+1 queries (and it's really bad).
Eager loading
On the other hand, if you have to access to almost all of your posts, you should do an eager loading.
-- Load all your data in a query
SELECT *
FROM categories c
INNER JOIN posts p ON c.id = p.category_id;
// Basic example in JSON of how to format your result
{
'cat1': ['post1', 'post2'],
'cat2': ['post5', 'post4', 'post5'],
...
}
What to do?
In your case I would say an eager loading because you load everything in a loop. But if you don't access to the most of your data, you should re-design your model to perform a lazy loading in such a way that the SQL query to load posts for a specific category is actually performed when a view try to access them.
What do you think?