Infinite-level categories are a common way to manage hierarchical data, suitable for multi-level and multi-subcategory scenarios such as product or article classifications. This article explains how to design and implement infinite-level categories in the ThinkPHP5 framework, including full example code.
Infinite-level categories allow each category to have multiple subcategories, which in turn can have their own subcategories, creating a nested hierarchical structure. This flexible structure makes managing and displaying complex category relationships easier.
To implement infinite-level categories, a category table needs to be created, typically containing the following fields:
In ThinkPHP5, models are used to interact with the database. The example below defines a Category model with methods to fetch direct child categories and recursively fetch all descendant categories:
namespace app\index\model;
use think\Model;
class Category extends Model
{
// Specify table name if it doesn't follow default convention
protected $table = 'category';
// Get direct child categories of the current category
public function childrenCategories()
{
return $this->hasMany('Category', 'parent_id', 'id')
->order('id', 'asc');
}
// Recursively get all child categories including descendants
public function allChildrenCategories()
{
return $this->childrenCategories()->with('allChildrenCategories');
}
}
These methods leverage ThinkPHP's relation model feature to conveniently handle hierarchical data retrieval.
In the controller, you can query and manipulate category data using the model. The following example fetches top-level categories and iterates over them to get their children:
$categories = Category::where('parent_id', 0)->select();
foreach ($categories as $category) {
$childrenCategories = $category->childrenCategories;
// Process the child categories here
}
To retrieve all descendants of a category (including nested children), use:
$allChildrenCategories = $category->allChildrenCategories;
This returns a collection of related models, allowing further operations as needed.
To display multi-level categories in the view, you can use a recursive function that indents category names by their depth level:
function renderCategories($categories, $indent = 0)
{
foreach ($categories as $category) {
echo str_repeat(' ', $indent);
echo $category->name . '<br/>';
renderCategories($category->childrenCategories, $indent + 1);
}
}
renderCategories($categories);
Alternatively, ThinkPHP5's template engine supports recursive rendering using the volist tag, for example:
<ul>
{volist name="categories" id="category"}
<li>{$category.name}</li>
{volist name="category.childrenCategories" id="child"}
<ul>
<li>{$child.name}</li>
{include file="category_tree" /}
</ul>
{/volist}
{/volist}
</ul>
This approach is concise and maintainable for recursive category display.
With proper database structure, model relationships, and recursive rendering, ThinkPHP5 provides a powerful and flexible way to implement infinite-level categories that satisfy complex classification needs. We hope this article and sample code serve as a helpful reference for your projects.