Skip to content

Commit 4937cac

Browse files
authored
Merge pull request #64 from dreamfactorysoftware/virtual-foreign-key-fix
fix virtual foreign key slider in schema tab when creating new virtua…
2 parents 5192c2e + 1daf26f commit 4937cac

4 files changed

Lines changed: 111 additions & 7 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddForeignKeyFieldsToDbFieldExtras extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('db_field_extras', function (Blueprint $table) {
16+
$table->boolean('is_foreign_key')->default(0);
17+
$table->string('ref_table')->nullable();
18+
$table->string('ref_field')->nullable();
19+
$table->string('ref_on_update')->nullable();
20+
$table->string('ref_on_delete')->nullable();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::table('db_field_extras', function (Blueprint $table) {
32+
$table->dropColumn([
33+
'is_foreign_key',
34+
'ref_table',
35+
'ref_field',
36+
'ref_on_update',
37+
'ref_on_delete',
38+
]);
39+
});
40+
}
41+
}

src/Components/DbSchemaExtras.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public function setSchemaFieldExtras($extras)
158158
'db_function',
159159
'is_virtual',
160160
'is_aggregate',
161+
'is_foreign_key',
162+
'ref_table',
163+
'ref_field',
164+
'ref_on_update',
165+
'ref_on_delete',
161166
]));
162167
}
163168
}

src/Models/DbFieldExtras.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ class DbFieldExtras extends BaseSystemModel
4545
'db_function',
4646
'is_virtual',
4747
'is_aggregate',
48+
'is_foreign_key',
49+
'ref_table',
50+
'ref_field',
51+
'ref_on_update',
52+
'ref_on_delete',
4853
];
4954

5055
protected $casts = [
51-
'id' => 'integer',
52-
'service_id' => 'integer',
53-
'is_virtual' => 'boolean',
54-
'is_aggregate' => 'boolean',
55-
'picklist' => 'array',
56-
'validation' => 'array',
57-
'db_function' => 'array',
56+
'id' => 'integer',
57+
'service_id' => 'integer',
58+
'is_virtual' => 'boolean',
59+
'is_aggregate' => 'boolean',
60+
'is_foreign_key' => 'boolean',
61+
'picklist' => 'array',
62+
'validation' => 'array',
63+
'db_function' => 'array',
5864
];
5965
}

src/Resources/DbSchemaResource.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use DreamFactory\Core\Models\Service;
2424
use DreamFactory\Core\Utility\ResourcesWrapper;
2525
use GraphQL\Type\Definition\Type;
26+
use Illuminate\Support\Str;
2627
use ServiceManager;
2728
use DreamFactory\Core\Database\Components\Schema;
2829
use DreamFactory\Core\Resources\BaseRestResource;
@@ -65,6 +66,11 @@ class DbSchemaResource extends BaseRestResource
6566
'db_function',
6667
'is_virtual',
6768
'is_aggregate',
69+
'is_foreign_key',
70+
'ref_table',
71+
'ref_field',
72+
'ref_on_update',
73+
'ref_on_delete',
6874
];
6975
/**
7076
* @var array
@@ -1732,7 +1738,12 @@ public function updateSchema($tables, $allow_merge = false, $allow_delete = fals
17321738
$this->setSchemaTableExtras($tableExtras);
17331739
}
17341740
if (!empty($fieldExtras)) {
1741+
// DEBUG: Log what's being saved to database
1742+
\Log::info('updateSchema - SAVING to database - fieldExtras:', $fieldExtras);
17351743
$this->setSchemaFieldExtras($fieldExtras);
1744+
\Log::info('updateSchema - SAVE COMPLETE');
1745+
} else {
1746+
\Log::info('updateSchema - NO fieldExtras to save (array is empty)');
17361747
}
17371748
if (!empty($fieldDrops)) {
17381749
foreach ($fieldDrops as $table => $dropped) {
@@ -1878,8 +1889,17 @@ protected function buildTableFields(
18781889

18791890
$oldArray = $oldField->toArray();
18801891
$diffFields = array_diff($this->fieldExtras, ['picklist', 'validation', 'db_function']);
1892+
1893+
// DEBUG: Log what we're comparing
1894+
\Log::info("UPDATE FIELD '$name' - diffFields:", $diffFields);
1895+
\Log::info("UPDATE FIELD '$name' - field data for comparison:", array_only($field, $diffFields));
1896+
\Log::info("UPDATE FIELD '$name' - oldArray data for comparison:", array_only($oldArray, $diffFields));
1897+
18811898
$extraNew = array_diff_assoc(array_only($field, $diffFields), array_only($oldArray, $diffFields));
18821899

1900+
// DEBUG: Log extracted extras
1901+
\Log::info("UPDATE FIELD '$name' - extraNew (changes detected):", $extraNew);
1902+
18831903
if (array_key_exists('picklist', $field)) {
18841904
$picklist = (array)array_get($field, 'picklist');
18851905
$oldPicklist = (array)$oldField->picklist;
@@ -1926,11 +1946,17 @@ protected function buildTableFields(
19261946
}
19271947

19281948
// if empty, nothing to do here, check extras
1949+
\Log::info("UPDATE FIELD '$name' - settingsNew check - empty=" . (empty($settingsNew) ? 'YES' : 'NO'));
19291950
if (empty($settingsNew)) {
19301951
if (!empty($extraNew)) {
19311952
$extraNew['table'] = $table_schema->name;
19321953
$extraNew['field'] = $name;
19331954
$extras[] = $extraNew;
1955+
1956+
// DEBUG: Log early exit save
1957+
\Log::info("UPDATE FIELD '$name' - EARLY EXIT SAVE - Adding to extras:", $extraNew);
1958+
} else {
1959+
\Log::info("UPDATE FIELD '$name' - EARLY EXIT - No extras to save");
19341960
}
19351961

19361962
continue;
@@ -1988,6 +2014,12 @@ protected function buildTableFields(
19882014
$extraNew['table'] = $table_schema->name;
19892015
$extraNew['field'] = $name;
19902016
$extras[] = $extraNew;
2017+
2018+
// DEBUG: Log what's being added to extras array
2019+
\Log::info("SAVING FIELD '$name' - Adding to extras array:", $extraNew);
2020+
} else {
2021+
// DEBUG: Log when extraNew is empty
2022+
\Log::info("SAVING FIELD '$name' - extraNew is EMPTY, nothing to save");
19912023
}
19922024
}
19932025

@@ -2011,6 +2043,9 @@ protected function buildTableFields(
20112043
}
20122044
}
20132045

2046+
// DEBUG: Log what extras are being returned
2047+
\Log::info('buildTableFields - RETURNING extras array:', $extras);
2048+
20142049
return [
20152050
'columns' => $columns,
20162051
'alter_columns' => $alterColumns,
@@ -2301,7 +2336,24 @@ public function buildTableRelated(
23012336
*/
23022337
protected function cleanClientField(array &$field)
23032338
{
2339+
// DEBUG: Log original field data
2340+
\Log::info('cleanClientField - ORIGINAL field data:', $field);
2341+
2342+
// Convert camelCase keys to snake_case first
2343+
$converted = [];
2344+
foreach ($field as $key => $value) {
2345+
$snakeKey = Str::snake($key);
2346+
$converted[$snakeKey] = $value;
2347+
}
2348+
$field = $converted;
2349+
2350+
// DEBUG: Log after snake_case conversion
2351+
\Log::info('cleanClientField - AFTER snake_case conversion:', $field);
2352+
23042353
$field = array_change_key_case($field, CASE_LOWER);
2354+
2355+
// DEBUG: Log after lowercase conversion
2356+
\Log::info('cleanClientField - AFTER lowercase conversion:', $field);
23052357
if (empty($name = array_get($field, 'name'))) {
23062358
throw new \Exception("Invalid schema detected - no name element.");
23072359
}

0 commit comments

Comments
 (0)