php - To clean/fill table after table in the correct order, can I simply sort all tables based on FKs? -
this more conceptual question.
i have scheduled task many input tables (sqlite) table1 ... tablen
, need perform series of delete/insert mysql, processing table after table.
read sqlite table -> empty mysql table -> fill mysql table
of course if (mysql) table1
has foreign key poiting table2
, table2
should deleted , filled before table1
. manually sort incoming tables, it's not idea 30+ table , can lead errors.
set foreign_key_checks = 0
not option.
so problem sorting mysql table based on number of incoming fks (high low), like:
// doctrine dbal example it's understandable $schema = $this->connection->getschemamanager(); // init chart keys table names, values # of incoming fks $chart = array_fill_keys($schema->listtablenames(), 0); // build chart foreach ($schema->listtables() $table) { foreach ($table->getforeignkeys() $foreignkey) { $chart[$foreignkey->getforeigntablename()]++; } } // sort high low values arsort($chart);
are there flaws in reasoning? question how deal self-foreign keys (simply ignore it?) , how deal circular references (i.e. table1 -> table2 -> table3 -> table1
).
Comments
Post a Comment