-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateTables.php
More file actions
50 lines (42 loc) · 1.14 KB
/
generateTables.php
File metadata and controls
50 lines (42 loc) · 1.14 KB
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
<?php
/**
* generate classes that exted DBObject.php that define a table
*/
$databases = array('test');
$tables = 'all'; //$tables = array('table1', 'table2');
foreach($databases as $d)
{
$dbh = new PDO("mysql:host=localhost;dbname=$d",'root' , '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(!is_array($tables) && $tables == 'all')
{
$tables = array();
$stmt = $dbh->prepare("show tables");
$row = array();
$stmt->execute();
while($row = $stmt->fetch() )
{
//print_r($row);
$tables[] = $row[0];
}
}
foreach($tables as $t)
{
$stmt = $dbh->prepare("desc $t");
$row = array();
$stmt->execute();
$items = '';
while($row = $stmt->fetch())
{
$fname = $row['Field'];
if($items)
$items .= ',';
$items .= "'$fname'";
}
$classname = preg_replace("/_(\w)/e", 'strtoupper(\\1)', ucfirst($t));
$class_contents = "<?php\n\nrequire_once 'DBObject.php';\n\nclass $classname extends DBObject\n{\n\tfunction __construct()\n\t{\n\t\tparent::__construct('$d', '$t', array($items));\n\t}\n}";
$fp = fopen($classname . '.php' , 'w');
fputs($fp, $class_contents);
fclose($fp);
}
}