Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

According to this article Managing hierarchical data in mysql the most suitable solution for holding regular hierarchical data is a Nested Set Model, which i totaly like, but unfortunately my task is slightly more difficult. I need to manage a hierarchical model where some of the sub-categories may have multiple parents, something like crossing sets. Similiar problems described here.

To be exact i have some structure of categories in which each item can belong to multiple categories (and also on my way i'll need to provide some mean of category inheritance, if item belongs to TVs then it also belongs to Home_electronics, so the regular tag-cloud won't do here).

tl;dr: need a simple way / approach (maybe complex in realization, but simple in managment, like delete, add and find path) to manage model of categories with M:M relations.

Sadly i'm limited to MySQL only, but if this task can't be solved with SQL only, i'll move on to implementing this functionality in PHP (and thus i'll be glad to hear any out-of-the-box solutions of this problem - libraries or just sources, but thats for the worst case scenario).

Looks like the thing i'm looking for is named Directed Acyclic Graph (which was pretty obvious but i was probably too dumb to think about it :)).

It'll be good to see some implementation of it which has good managability.

And by the way, regular ID, ParentID, Data thing is not an option because MySQL doesn't have recursion and thus can't retrieve data by one query (well it can if you make PHP create a query with 1000 JOINs and pass it to MySQL, but thats retarded).

PS: Using only MySQL is not my decision, it's simply given, i know that any NoSQL DBMS would be more suitable.

share|improve this question

1 Answer 1

Shouldn't a M to N type relationship tables in mysql work?

tbl_Category
cat_id, name


tbl_Cat_parent
cat_id, parent_cat_id

where parent_cat_id refers to Category->cat_id

share|improve this answer
    
Offcourse, but the question is about how to build efficently manageable data structure (which i could use to easily extract path of the node, or paths to be exact, add and delete nodes without need to build SQL quries with thousands of joins). –  Ivan0x32 Mar 6 '13 at 22:34
    
Data structure wise it's a tree structure, which of course you have to manage in PHP. You will have to separate, product to category relationship from category to category relationship. Represent your category as a path, i.e. A->B->C as it can also be that A->D->C, so what's included in C might different based on the path. If you use any caching mechanism you can build the category tree with two quires and cache it. –  xelber Mar 6 '13 at 22:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.