Improved model creation

This commit is contained in:
Pinga 2023-09-05 01:12:44 +03:00
parent ab69657429
commit 6ebb91f077

View file

@ -1,6 +1,6 @@
<?php <?php
use Delight\Db\PdoDatabase; use Pinga\Db\PdoDatabase;
// Include the Delight-IM/db package // Include the Delight-IM/db package
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
@ -9,7 +9,8 @@ require_once __DIR__ . '/../vendor/autoload.php';
$tableName = readline('Enter table name: '); $tableName = readline('Enter table name: ');
// Connect to the database using the PDO driver // Connect to the database using the PDO driver
$db = new PdoDatabase('mysql:host=localhost;dbname=my_database;charset=utf8mb4', 'my_username', 'my_password'); $pdo = new PDO('mysql:host=localhost;dbname=my_database;charset=utf8mb4', 'my_username', 'my_password');
$db = \Pinga\Db\PdoDatabase::fromPdo($pdo);
// Get the column names and types for the specified table // Get the column names and types for the specified table
$columnData = $db->select('DESCRIBE ' . $tableName); $columnData = $db->select('DESCRIBE ' . $tableName);
@ -17,11 +18,30 @@ $columnData = $db->select('DESCRIBE ' . $tableName);
// Create the class name based on the table name (e.g. "users" -> "User") // Create the class name based on the table name (e.g. "users" -> "User")
$className = ucwords($tableName, '_'); $className = ucwords($tableName, '_');
// Generate the necessary lists outside of the heredoc
$columnFieldsList = implode(', ', array_map(function ($column) {
return $column['Field'];
}, $columnData));
$columnValuesList = implode(', ', array_map(function ($column) {
return '$' . $column['Field'];
}, $columnData));
$quotedColumnValuesList = implode(', ', array_map(function ($column) {
return '$' . $column['Field'] . ' = $this->db->quote($' . $column['Field'] . ');';
}, $columnData));
$setColumnsList = implode(', ', array_map(function ($column) {
return $column['Field'] . ' = $' . $column['Field'];
}, $columnData));
// Generate the PHP code for the CRUD model based on the column data // Generate the PHP code for the CRUD model based on the column data
$modelCode = <<<PHP $modelCode = <<<PHP
<?php
namespace App\Models; namespace App\Models;
use Delight\Db\PdoDatabase; use Pinga\Db\PdoDatabase;
class $className class $className
{ {
@ -32,63 +52,44 @@ class $className
\$this->db = \$db; \$this->db = \$db;
} }
public function getAll$className() public function getAll{$className}()
{ {
return \$this->db->select('SELECT * FROM $tableName'); return \$this->db->select('SELECT * FROM $tableName');
} }
public function get$classNameById(\$id) public function get{$className}ById(\$id)
{ {
return \$this->db->select('SELECT * FROM $tableName WHERE id = ?', [\$id])->fetch(); return \$this->db->select('SELECT * FROM $tableName WHERE id = ?', [\$id])->fetch();
} }
public function create$className(${ public function create{$className}($columnValuesList)
implode(', ', array_map(function ($column) {
return '$' . $column['Field'];
}, $columnData))
})
{ {
${implode("\n ", array_map(function ($column) { $quotedColumnValuesList
return '$' . $column['Field'] . ' = $this->db->quote($' . $column['Field'] . ');';
}, $columnData))}
\$this->db->insert('INSERT INTO $tableName (${implode(', ', array_map(function ($column) {
return $column['Field'];
}, $columnData))}) VALUES (${implode(', ', array_map(function ($column) {
return '$' . $column['Field'];
}, $columnData))})');
\$this->db->insert('INSERT INTO $tableName ($columnFieldsList) VALUES ($columnValuesList)');
return \$this->db->lastInsertId(); return \$this->db->lastInsertId();
} }
public function update$className(\$id${implode(', ', array_map(function ($column) { public function update{$className}(\$id, $columnValuesList)
return ', $' . $column['Field'];
}, $columnData))})
{ {
${implode("\n ", array_map(function ($column) { $quotedColumnValuesList
return '$' . $column['Field'] . ' = $this->db->quote($' . $column['Field'] . ');';
}, $columnData))}
\$this->db->update('UPDATE $tableName SET ${implode(', ', array_map(function ($column) {
return $column['Field'] . ' = $' . $column['Field'];
}, $columnData))} WHERE id = ?', array_merge([\$id], array_map(function ($column) {
return '$' . $column['Field'];
}, $columnData)));
\$this->db->update('UPDATE $tableName SET $setColumnsList WHERE id = ?', [\$id]);
return true; return true;
} }
public function delete$className(\$id) public function delete{$className}(\$id)
{ {
\$this->db->delete('DELETE FROM $tableName WHERE id = ?', [\$id]); \$this->db->delete('DELETE FROM $tableName WHERE id = ?', [\$id]);
return true; return true;
} }
} }
PHP; PHP;
// Save the generated PHP code to a file // Save the generated PHP code to a file
file_put_contents(__DIR__ . "/app/Models/$className.php", $modelCode); file_put_contents(__DIR__ . "/../app/Models/$className.php", $modelCode);
// Output a success message // Output a success message
echo "CRUD model for table '$tableName' generated successfully.\n"; echo "CRUD model for table '$tableName' generated successfully.\n";