简体   繁体   中英

Build tree of database schema in PHP

I have some files with names of tables and names of tables which are referenced by the current table like this:

table1
table2, table3
-
table 2
table 4

This means table1 references to tables table2 and table3, and table2 references to table4. How can I to build the tree of tables, where keys are "parent" tables and children are referenced tables like this:

[table4] => array(
    [table2] => array([table1] => null),
)
[table3] => array([table1] => null)

So, my problem is I know how to parse to tree records in file like this:

name    | parent
------------------
table 1 | table 2
table 2 | table 4

But I don't know the best way how to parse to tree the structure, when ONE node has MULTIPLE parents.

Correct me if I'm wrong but that's not enough to have just the table you need also a table design. Hence you need a field value and a field id. That field id is most likely an unique id and can reference to the same table or to another table. For example table 1 has a field title and a field id. If the id reference to the same table you can use a self join. This model is called adjacency list model but I'm not sure what's the direction of the reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM