annotate src/Database/MyBlueprint.php @ 7:e6132a1e8e24 default tip

Adding better support for test among other base changes.
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 25 Sep 2025 19:58:32 -0400
parents 84c75d9d90be
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
1 <?php
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
2
4
84c75d9d90be Changing usage to be bootstrap 5, not everything is reviewed but it's been started
luka
parents: 2
diff changeset
3 namespace Wizard\Framework\Database;
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
4
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
5 use Illuminate\Database\Schema\ColumnDefinition;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
6
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
7 class MyBlueprint extends \Illuminate\Database\Schema\Blueprint
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
8 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
9 /**
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
10 * Generates the base columns for a regular table
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
11 */
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
12 public function baseColumns(): void
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
13 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
14 $this->id();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
15 $this->reference('users', 'created_by')->nullable();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
16 $this->reference('users', 'updated_by')->nullable();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
17 $this->timestamps();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
18 $this->softDeletes();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
19 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
20
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
21 /**
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
22 * @param mixed $abbreviation
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
23 */
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
24 public function baseTextColumns($abbreviation = false): void
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
25 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
26 $this->string('name', 255);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
27 $this->text('description')->nullable();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
28 if ($abbreviation) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
29 $this->string('abbreviation', 50)->nullable();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
30 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
31 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
32
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
33 public function pivotColumns(string $table1, string $table2): void
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
34 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
35 $this->comment('PIVOT:'.json_encode(['table1' => $table1, 'table2' => $table2]));
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
36 $this->id();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
37 $this->reference($table1);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
38 $this->reference($table2);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
39 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
40
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
41 public function settingsColumns(): void
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
42 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
43 $this->baseColumns();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
44 $this->baseTextColumns();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
45 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
46
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
47 /**
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
48 * @param mixed $keep_column
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
49 */
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
50 public function dropReference(string $table, ?string $override_column_name = null, $keep_column = false): void
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
51 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
52 // set up relevant strings
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
53 $column_name = self::get_foreign_column_name($table, $override_column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
54 $fk_name = self::get_foreign_key_name($this->getTable(), $table, $column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
55
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
56 // drop the reference
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
57 $this->dropForeign($fk_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
58
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
59 // drop the column if necessary
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
60 if (! $keep_column) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
61 $this->dropColumn($column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
62 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
63 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
64
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
65 public function reference(string $table, ?string $override_column_name = null): ColumnDefinition
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
66 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
67 // set up relevant strings
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
68 $column_name = self::get_foreign_column_name($table, $override_column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
69 $fk_name = self::get_foreign_key_name($this->getTable(), $table, $column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
70
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
71 // create the column
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
72 $column = $this->unsignedBigInteger($column_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
73
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
74 // create the foreign key
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
75 $this->foreign($column_name, $fk_name)->references('id')->on($table);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
76
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
77 return $column;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
78 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
79
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
80 protected static function get_foreign_column_name(string $input, ?string $override = null): string
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
81 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
82 return ! is_null($override) ? $override : \Str::singular($input).'_id';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
83 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
84
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
85 /*
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
86 * Generates a foreign key name
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
87 * 'FK_' + current table name + foreign column name
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
88 *
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
89 * */
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
90 protected static function get_foreign_key_name(string $current_table, string $table, string $column_name): string
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
91 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
92 $fk_name = 'FK_';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
93
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
94 // add the current table name
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
95 $fk_name .= \Str::singular($current_table);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
96
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
97 // add the foreign column name
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
98 $fk_name .= '_'.$column_name;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
99
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
100 return $fk_name;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
101 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
102 }