Create a PHP class from a database table
Simple script to get the columns from a database table and write out a PHP class.
$table = preg_replace('/\W/','',$argv[1]);
if (empty($table)) die ('Invalid table'.PHP_EOL);
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root');
$className = ucfirst($table);
echo <<<START
<?php
Class Model_$className
extends Model_Abstract {
START;
$query = $dbh->query('DESCRIBE '.$argv[1]);
$columns = $query->fetchAll(PDO::FETCH_CLASS);
foreach ($columns as $row) {
echo "\tprivate \${$row->Field};\n";
}
echo '}';
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "
";
die();
}
?>
Print article | This entry was posted by elvis on 09/18/13 at 02:24:00 pm . Follow any responses to this post through RSS 2.0. |