I have a MySQL query that I use with PHP to build hierarchical JSON for a d3.js tree/dendrogram.
Fiddle to view schema and existing queries.
I am now considering how to add additional data relationships Dbetween program_outcome dataOand unit dataUand development_level data, with multiple pairs between them Many relationships.
There are only three types ofD, as shown in the figure below.
Concept map I need:
EachUis related to only oneDof eachO(only oneObranch is shown for clarity).
So U needs to be the descendant ofDand the grandson ofO. The sameUmay have the same or different type ofDfor otherObranches.
As you can see in the fiddle, the relationship betweenOandUis currently implemented via the lookup/relationship tableprogram_outcome_unit_lookup
.
Also, it is possible to change the lookup tables so there are two lookup tables instead of theprogram_outcome_unit_lookup
table, which might work:
O->U
D->U
Any ideas how to achieve this?
The PHP after the query (not in the database fiddle...) is as follows, but may not be relevant to the solution, this is essentially a database issue.
$result = $connection->query($query); $data = array(); while ($row = $result->fetch_object()) { $data[$row->global_id] = $row; } $roots = array(); foreach ($data as $row) { $row->type = end(explode(",",(implode(array_slice(explode ( ':', $row->global_id), -2, 1))))); if ($row->parent_global_id === null) { $roots[]= $row; } else { $data[$row->parent_global_id]->children[] = $row; } unset($row->parent_global_id); unset($row->global_id); } $json = json_encode($roots); $json = trim($json, '[]'); $fp = fopen('data.json', 'w'); fwrite($fp, $json); fclose($fp);
renew
View the expanded concept diagram with two branches:
Your model appears to be: each (unique) tuple
(O, U)
is assigned a mandatory valueD
.You can implement this model by adding the
D
column to theprogram_outcome_unit_lookup
table:(program_outcome_fk, unit_fk)
can also be your primary key, but either way it must be unique (you are not currently enforcing this constraint).Now, each
U
can be a member of any number ofO
, but as per the requirement, "eachU
will be associated only with eachD
Association”.For example to store
U1
(O1-D2-U1
andO2-D1-U1
) from the updated chart, you can change the values((1,2,1),(2,1, 1))
. As required, you cannot yet add e.g.O2-D2-U1
as it would violate the unique constraint.You should also add a new table for
D
. If not everyO
is allowed to use everyD
(e.g. if theO2
branch is not allowed to useD1
), then one more Table(O, D)
, otherwise it is not necessary.