Closed lookingdown closed 4 years ago
There's no an easy way to do that, but you can use the database scheme to iterate all tables and check if they are related:
foreach ($db->getScheme()->getTables() as $tableName) {
$table = $db->{$tableName};
if ($db->order->getJoinField($table)) {
// Relation one to many
}
if ($table->getJoinField($db->order)) {
// Relation many to one
}
if ($db->order->getJoinTable($table)) {
// Relation many to many
}
}
Thanks.. Seems a bit cumbersome.. This works, not sure how efficient it is though...
$table='sometable';
$id='someid';
$data = $db->$table[$id];
$stmt = $pdo->query("SELECT column_name AS 'table' FROM information_schema.columns WHERE table_name='$table' AND column_name LIKE '%_id'");
$tbls = $stmt->fetchAll();
foreach($tbls AS $t){
$i=substr($t['table'], 0, -3);
$arr[$i] = $data->$i;
}
// Array that contains all related data keyed on table names ..
$arr[$table]= $data;
For the above to work value of $table is required to be same as name of queried table in simple-crud in this case 'order' ($db->order[num])..
Should work for any table that have matching content in related tables 'brand' for example (brand_id) must have a matching id in 'brand' table otherwise this will fail. In my case I have a flat array with a few table names i dont want to include and a condition to exclude them.
Simple enough..
Any thoughts :)
Thanks again
Hello, I wonder if it is possible to list all tables related to choosen table. Now I do like this $order=$db->order[298], then $order->customer and I merge customer with $order (on "customer" key), and output as json from API and it works perfect.
I have not found a way to get all related tables data without doing like this. Is there a way to output all values from $order in this case AND all related tables. Or os there away to get a list of all the related to be able to iterate them. I hope to set up an automated API (fetching data) where providing only table name and ID would be enough. Thanks again for a terrific script:)