/
generate_model.php
109 lines (76 loc) · 2.53 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
<?php
include("../../autoload.php");
/*you can these settings to meet your needs*/
$host="";//database host
$user="";//database user
$pass="";//database pass
$database="";//database name
$model_directory="../../../TModel/"; //this is the directory where your models will be created
/*pry don't edit under here unless you know what your doing....*/
$base_model_directory=$model_directory."Base/";
$options=getopt("",array("table:","host:","user:","pass:","database:"));
$table=$options['table'];
if($options["host"]){ $host=$options["host"]; }
if($options["user"]){ $user=$options["user"]; }
if($options["pass"]){ $pass=$options["pass"]; }
if($options["database"]){$database=$options["database"]; }
$db=EasyConnect::getInstance($host,$user,$pass,$database);
$query="Describe `$table`";
$results=$db->fetchAssoc($query);
$table_new_name=str_replace("_", " ",$table);
$table_new_name=Ucwords($table_new_name);
$table_new_name=str_replace(" ", "",$table_new_name);
$file_content=
'<?php
namespace TModel\Base;
use TaysORM\TaysORMBase;
class '.$table_new_name.'Base extends TaysORMBase {
public $table_name="'.$table.'";
public $primary_keys = '.get_primary_key($results).';
public $columns=array(
';
foreach ($results as $column){
$field=$column['Field'];
$datatype=$column['Type'];
$auto_increment =($column['Extra']=="auto_increment") ? ',"auto_increment"=>true' : "";
$primary_key =($column['Key']=="PRI") ? ',"primary_key"=>true' : "" ;
$file_content.=
'"'.$field.'"=>array("dataype"=>"'.$datatype.'"'.$auto_increment.$primary_key.'),
';
}
$file_content.=');
}
';
$new_file_path= $base_model_directory.$table_new_name."Base.php";
file_put_contents($new_file_path,$file_content);
//then we just need to build the regular model
$model =
'<?php
namespace TModel;
use TModel\Base\\'.$table_new_name.'Base;
class '.$table_new_name.' extends '.$table_new_name.'Base
{
}
';
//only put content if file does not exist
$new_file_path= $model_directory.$table_new_name.".php";
if(!file_exists($new_file_path)) {
file_put_contents($new_file_path,$model);
}
/*
* get the primary key based on results
*/
function get_primary_key($results)
{
$pkeys='array(';
foreach($results as $column)
{
if($column['Key']=="PRI"){
$pkeys.="'".$column["Field"]."',";
}
}
$pkeys=substr($pkeys,0,-1);
$pkeys.=')';
return $pkeys;
}
?>