|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ubiquity\orm\creator; |
|
3
|
|
|
|
|
4
|
|
|
use Ubiquity\annotations\JoinColumnAnnotation; |
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
|
6
|
|
|
use Ubiquity\controllers\Startup; |
|
7
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
abstract class ModelsCreator { |
|
11
|
|
|
protected $config; |
|
12
|
|
|
protected $tables=array(); |
|
13
|
|
|
protected $classes=array(); |
|
14
|
|
|
|
|
15
|
|
|
protected function init($config){ |
|
16
|
|
|
$this->config=$config["database"]; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function create($config,$initCache=true,$singleTable=null){ |
|
20
|
|
|
$this->init($config); |
|
21
|
|
|
$modelsDir=Startup::getModelsCompletePath(); |
|
22
|
|
|
if(UFileSystem::safeMkdir($modelsDir)){ |
|
23
|
|
|
$this->tables=$this->getTablesName(); |
|
24
|
|
|
CacheManager::checkCache($config); |
|
25
|
|
|
|
|
26
|
|
|
foreach ($this->tables as $table){ |
|
27
|
|
|
$class=new Model($table,$config["mvcNS"]["models"]); |
|
28
|
|
|
$fieldsInfos=$this->getFieldsInfos($table); |
|
29
|
|
|
$keys=$this->getPrimaryKeys($table); |
|
30
|
|
|
foreach ($fieldsInfos as $field=>$info){ |
|
31
|
|
|
$member=new Member($field); |
|
32
|
|
|
if(in_array($field, $keys)){ |
|
33
|
|
|
$member->setPrimary(); |
|
34
|
|
|
} |
|
35
|
|
|
$member->setDbType($info); |
|
36
|
|
|
$class->addMember($member); |
|
37
|
|
|
} |
|
38
|
|
|
$this->classes[$table]=$class; |
|
39
|
|
|
} |
|
40
|
|
|
$this->createRelations(); |
|
41
|
|
|
if(isset($singleTable)){ |
|
42
|
|
|
$this->createOneClass($singleTable,$modelsDir); |
|
43
|
|
|
}else{ |
|
44
|
|
|
foreach ($this->classes as $table=>$class){ |
|
45
|
|
|
$name=$class->getSimpleName(); |
|
46
|
|
|
echo "Creating the {$name} class\n"; |
|
47
|
|
|
$this->writeFile($modelsDir.DS.$name.".php", $class); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
if($initCache===true){ |
|
51
|
|
|
CacheManager::initCache($config,"models"); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function createOneClass($singleTable,$modelsDir){ |
|
57
|
|
|
if(isset($this->classes[$singleTable])){ |
|
58
|
|
|
$class=$this->classes[$singleTable]; |
|
59
|
|
|
echo "Creating the {$class->getName()} class\n"; |
|
60
|
|
|
$this->writeFile($modelsDir.DS.$singleTable.".php", $class); |
|
61
|
|
|
}else{ |
|
62
|
|
|
echo "The {$singleTable} table does not exist in the database\n"; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function createRelations(){ |
|
67
|
|
|
foreach ($this->classes as $table=>$class){ |
|
68
|
|
|
$keys=$this->getPrimaryKeys($table); |
|
69
|
|
|
foreach ($keys as $key){ |
|
70
|
|
|
$fks=$this->getForeignKeys($table, $key); |
|
71
|
|
|
foreach ($fks as $fk){ |
|
72
|
|
|
$field=strtolower($table); |
|
73
|
|
|
$fkTable=$fk["TABLE_NAME"]; |
|
74
|
|
|
$this->classes[$table]->addOneToMany($fkTable."s",$table, $this->classes[$fkTable]->getName()); |
|
75
|
|
|
$this->classes[$fkTable]->addManyToOne($field, $fk["COLUMN_NAME"], $class->getName()); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$this->createManyToMany(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function getTableName($classname){ |
|
83
|
|
|
foreach ($this->classes as $table=>$class){ |
|
84
|
|
|
if($class->getName()===$classname) |
|
85
|
|
|
return $table; |
|
86
|
|
|
} |
|
87
|
|
|
$posSlash=strrpos($classname, '\\'); |
|
88
|
|
|
$tablename=substr($classname, $posSlash+ 1); |
|
89
|
|
|
return lcfirst($tablename); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function createManyToMany(){ |
|
93
|
|
|
foreach ($this->classes as $table=>$class){ |
|
94
|
|
|
if($class->isAssociation()===true){ |
|
95
|
|
|
$members=$class->getManyToOneMembers(); |
|
96
|
|
|
if(sizeof($members)==2){ |
|
97
|
|
|
$manyToOne1=$members[0]->getManyToOne(); |
|
98
|
|
|
$manyToOne2=$members[1]->getManyToOne(); |
|
99
|
|
|
$table1=$this->getTableName($manyToOne1->className); |
|
100
|
|
|
$table2=$this->getTableName($manyToOne2->className); |
|
101
|
|
|
$class1=$this->classes[$table1]; |
|
102
|
|
|
$class2=$this->classes[$table2]; |
|
103
|
|
|
$tableMember=\lcfirst($table)."s"; |
|
104
|
|
|
$table1Member=\lcfirst($table1)."s"; |
|
105
|
|
|
$table2Member=\lcfirst($table2)."s"; |
|
106
|
|
|
$joinTable1=$this->getJoinTableArray($class1, $manyToOne1); |
|
107
|
|
|
$joinTable2=$this->getJoinTableArray($class2, $manyToOne2); |
|
108
|
|
|
$class1->addManyToMany($table2Member, $manyToOne2->className, $table1Member, $table,$joinTable1,$joinTable2); |
|
109
|
|
|
$class1->removeMember($tableMember); |
|
110
|
|
|
|
|
111
|
|
|
$class2->addManyToMany($table1Member, $manyToOne1->className, $table2Member, $table,$joinTable2,$joinTable1); |
|
112
|
|
|
$class2->removeMember($tableMember); |
|
113
|
|
|
unset($this->classes[$table]); |
|
114
|
|
|
}else{ |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function getJoinTableArray(Model $class,JoinColumnAnnotation $joinColumn){ |
|
122
|
|
|
$pk=$class->getPrimaryKey(); |
|
123
|
|
|
$fk=$joinColumn->name; |
|
124
|
|
|
$dFk=$class->getDefaultFk(); |
|
125
|
|
|
if($fk!==$dFk){ |
|
126
|
|
|
if($pk!==null && $fk!==null) |
|
127
|
|
|
return ["name"=>$fk, "referencedColumnName"=>$pk->getName()]; |
|
128
|
|
|
} |
|
129
|
|
|
return []; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
abstract protected function getTablesName(); |
|
133
|
|
|
|
|
134
|
|
|
abstract protected function getFieldsInfos($tableName); |
|
135
|
|
|
|
|
136
|
|
|
abstract protected function getPrimaryKeys($tableName); |
|
137
|
|
|
|
|
138
|
|
|
protected function writeFile($filename,$data){ |
|
139
|
|
|
return file_put_contents($filename,$data); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|