-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-model.php
167 lines (140 loc) · 6.4 KB
/
generate-model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
$host = ""; // Database host
$dbname = ""; // Database name
$username = ""; // Database username
$password = ""; // Database password
$prefix = ""; // Database table prefix
$dataTypeMapping = [
'tinyint() unsigned' => 'unsignedTinyInteger',
'tinyint()' => 'tinyInteger',
'smallint() unsigned' => 'unsignedSmallInteger',
'smallint()' => 'smallInteger',
'mediumint() unsigned' => 'unsignedMediumInteger',
'mediumint()' => 'mediumInteger',
'bigint() unsigned' => 'unsignedBigInteger',
'bigint()' => 'bigInteger',
'int() unsigned' => 'unsignedInteger',
'int()' => 'integer',
'varchar()' => 'string',
'mediumtext' => 'mediumText',
'longtext' => 'longText',
'text' => 'text',
'char()' => 'char',
'date' => 'date',
'time' => 'time',
'datetime' => 'dateTime',
// Add more data types as needed
];
function camelCaseTableName(string $tableName): string {
return str_replace(' ', '', ucwords(str_replace('_', ' ', $tableName)));
}
// Create a temporary directory to store model files
$zipDir = __DIR__ . '/temp_models/';
if (!is_dir($zipDir)) {
mkdir($zipDir);
}
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
$tableData = array();
foreach ($tables as $table) {
if (!isset($tableData[$table])) {
$tableData[$table] = array(
'name' => $table,
'columns' => array(),
'belongsToClasses' => array(),
'hasManyClasses' => array(),
);
}
$columns = $pdo->query("DESCRIBE $table")->fetchAll(PDO::FETCH_ASSOC);
foreach ($columns as $column) {
$tableData[$table]['columns'][] = "'" . $column['Field'] . "'";
}
$foreignKeys = $pdo->query("SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '$dbname' AND TABLE_NAME = '$table'")->fetchAll(PDO::FETCH_ASSOC);
foreach ($foreignKeys as $foreignKey) {
$tableData[$table]['belongsToClasses'][] = array(
'localColumnName' => $foreignKey['COLUMN_NAME'],
'referencedTableName' => $foreignKey['REFERENCED_TABLE_NAME'],
'referencedColumnName' => $foreignKey['REFERENCED_COLUMN_NAME'],
);
if (!isset($tableData[$foreignKey['REFERENCED_TABLE_NAME']])) {
$tableData[$foreignKey['REFERENCED_TABLE_NAME']] = array(
'name' => $foreignKey['REFERENCED_TABLE_NAME'],
'columns' => array(),
'belongsToClasses' => array(),
'hasManyClasses' => array(),
);
}
$tableData[$foreignKey['REFERENCED_TABLE_NAME']]['hasManyClasses'][] = array(
'localColumnName' => $foreignKey['REFERENCED_COLUMN_NAME'],
'referencedTableName' => $table,
'referencedColumnName' => $foreignKey['COLUMN_NAME'],
);
}
}
foreach ($tableData as $tableName => $tableProperties) {
$tableWithoutPrefix = str_replace($prefix, '', $tableName);
$tableCamelCase = camelCaseTableName($tableWithoutPrefix);
$modelContent = "<?php\n\n";
$modelContent .= "namespace App\Models;\n\n";
$modelContent .= "use Illuminate\Database\Eloquent\Model;\n\n";
$modelContent .= "class " . $tableCamelCase . " extends Model\n";
$modelContent .= "{\n";
// Generate $fillable property in the model
$modelContent .= " protected \$fillable = [" . implode(', ', $tableProperties['columns']) . "];\n\n";
// Generate empty $hidden property in the model
$modelContent .= " protected \$hidden = [];\n\n";
// Generate belongsTo relationship function
foreach ($tableProperties['belongsToClasses'] as $belongsToClass) {
$referencedTableWithoutPrefix = str_replace($prefix, '', $belongsToClass['referencedTableName']);
$referencedTableCamelCase = camelCaseTableName($referencedTableWithoutPrefix);
$localColumnName = $belongsToClass['localColumnName'];
$referencedColumnName = $belongsToClass['referencedColumnName'];
$modelContent .= " public function " . lcfirst($referencedTableCamelCase) . "() {\n";
$modelContent .= " return \$this->belongsTo(App\\Models\\$referencedTableCamelCase::class, '$localColumnName', '$referencedColumnName');\n";
$modelContent .= " }\n\n";
}
// Generate belongsTo relationship function
foreach ($tableProperties['hasManyClasses'] as $hasManyClass) {
$referencedTableWithoutPrefix = str_replace($prefix, '', $hasManyClass['referencedTableName']);
$referencedTableCamelCase = camelCaseTableName($referencedTableWithoutPrefix);
$localColumnName = $hasManyClass['localColumnName'];
$referencedColumnName = $hasManyClass['referencedColumnName'];
$modelContent .= " public function " . lcfirst($referencedTableCamelCase) . "s() {\n";
$modelContent .= " return \$this->hasMany(App\\Models\\$referencedTableCamelCase::class, '$localColumnName', '$referencedColumnName');\n";
$modelContent .= " }\n\n";
}
$modelContent .= "}\n";
// Save the model to a file
file_put_contents($zipDir . $tableCamelCase . '.php', $modelContent);
}
// Create a zip archive
$zip = new ZipArchive();
$zipFileName = 'models.zip';
if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
// Add model files to the zip archive
$modelFiles = glob($zipDir . '*.php');
foreach ($modelFiles as $file) {
$zip->addFile($file, basename($file));
}
$zip->close();
// Delete temporary model files
foreach ($modelFiles as $file) {
unlink($file);
}
// Prompt the browser to download the zip file
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"$zipFileName\"");
header('Content-Length: ' . filesize($zipFileName));
readfile($zipFileName);
// Clean up by deleting the zip file
unlink($zipFileName);
} else {
echo 'Failed to create the zip archive.';
}
// Remove the temporary directory
rmdir($zipDir);