0

I'm trying to extract product categories from a csv file. The categories are hierarchical, but in string form like this: Parent,Child,Grandchild.

Each row in the csv file is a product, and the categories are repeated for every product.

I'm working with PHP 7.2, MySQL 5.7 and Laravel 5.8.

I don't even know where to start. I'm usually good at hacking something together that works, and then continuing working on it until it's looking good. But this time I'm kind of stumped.


So, to summarize:

I would like to loop through strings like these:

Parent,Child
Parent,Child,Grandchild
Parent,Other Child,Other Grandchild
Other Parent,Different Child,Different Grandchild

And get this organized into my MySQL database:

| id | parent_id | title                |

| 1  | null      | Parent               |
| 2  | 1         | Child                |
| 3  | 2         | Grandchild           |
| 4  | 1         | Other Child          |
| 5  | 4         | Other Grandchild     |
| 6  | null      | Other Parent         |
| 7  | 6         | Different Child      |
| 8  | 7         | Different Grandchild |

Duplicates (by title) should just be merged, but not if they have a different parent.

Maybe there's some better way to organize it in the database to make this easier? I'm open to suggestions if you have any. Thanks in advance.

adevade
  • 29
  • 1
  • 3
  • https://stackoverflow.com/a/544741/1839439 – Dharman Apr 12 '19 at 22:09
  • Storing hierarchies efficiently in relational DB is tricky. I would recommend closure table. Check out this presentation for more information: https://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql/62-Naive_Trees_Closure_Tables_example – Dharman Apr 12 '19 at 22:12

0 Answers0