Shaan
Shaan

Reputation: 873

Setting up a nested set hierarchy

Let's say I have a bunch of businesses. And each business can have multiple categories, subcategories and sub-subcategories (three levels). Let's say I set up a table according to the nested set model for my categories.

How do I now use this table and assign categories to each business? I understand I will need another table but what node gets assigned? Is it the lowest level node?

business_id category_id

And then what's the right way to retrieve all the categories for each business?

Upvotes: 0

Views: 729

Answers (1)

Ben English
Ben English

Reputation: 3918

The way this generally works is that you assign the leaf or lowest-level-node. Then when you are querying to get the full hierarchy you traverse up the tree to the root. It is generally much easier (especially in MySQL) to traverse from leaf to root then vice versa.

Here is the best link I've found that describes how to accomplish this query for a tree of dynamic size (the link you've included assumes that the tree is always 3 levels deep)

Upvotes: 0

Related Questions