43 public function create(array
$config,
bool $initCache =
true, ?
string $singleTable =
null,
string $offset =
'default',
string $memberAccess =
'private') {
45 $engine = CacheManager::getAnnotationsEngineInstance();
46 $this->
init($config, $offset);
50 if ($offset !==
'default') {
51 $dirPostfix = \DS . $offset;
54 $modelsDir = Startup::getModelsCompletePath() . $dirPostfix;
55 if (UFileSystem::safeMkdir($modelsDir)) {
57 CacheManager::checkCache(
$config);
59 foreach ($this->tables as $table) {
60 $class =
new Model($engine, $table, Startup::getNS(
'models') . $nsPostfix,
$memberAccess);
61 $class->setDatabase($offset);
64 $class->setSimpleMembers(\array_keys($fieldsInfos));
66 foreach ($fieldsInfos as $field => $info) {
68 if (\in_array($field, $keys)) {
69 $member->setPrimary();
71 $member->setDbType($info);
72 $member->addValidators();
73 $member->setTransformer();
74 $class->addMember($member);
76 $class->addMainAnnots();
77 $this->classes[$table] = $class;
81 if (isset($singleTable)) {
84 foreach ($this->classes as $table => $class) {
85 $name = $class->getSimpleName();
86 echo
"Creating the {$name} class\n";
87 $classContent = $class->__toString();
88 $this->
writeFile($modelsDir . \DS . $name .
'.php', $classContent);
91 if ($initCache ===
true) {
92 CacheManager::initCache(
$config,
'models', $this->silent);
114 foreach ($this->classes as $table => $class) {
116 foreach ($keys as $key) {
117 $fks = $this->
getForeignKeys($table, $key, $this->config[
'dbName'] ??
'');
118 foreach ($fks as $fk) {
119 $field = \lcfirst($table);
120 $fkTable = $fk[
'TABLE_NAME'];
121 $this->classes[$table]->addOneToMany(\lcfirst($fkTable) .
's', \lcfirst($table), $this->classes[$fkTable]->getName(), $this->
getAlternateName($fk[
'COLUMN_NAME'], $fk[
'REFERENCED_COLUMN_NAME'] ?? $field) .
's');
122 $this->classes[$fkTable]->addManyToOne($field, \lcfirst($fk[
'COLUMN_NAME']), $class->getName(), $this->getAlternateName($fk[
'COLUMN_NAME'], $fk[
'REFERENCED_COLUMN_NAME'] ?? $field));
131 $pkName = \ucfirst($pkName);
132 if (\substr($fkName, 0, \strlen($pkName)) == $pkName) {
133 $alter = \substr($fkName, \strlen($pkName));
135 $needle_position = \strlen($pkName) * - 1;
137 if (\substr($alter, $needle_position) == $pkName) {
138 $alter = \substr($alter, 0, $needle_position);
140 $alter = \trim($alter,
'_');
141 return \lcfirst($alter);
145 foreach ($this->classes as $table => $class) {
146 if ($class->getName() === $classname) {
150 $posSlash = strrpos($classname,
'\\');
151 $tablename = substr($classname, $posSlash + 1);
152 return \lcfirst($tablename);
156 foreach ($this->classes as $table => $class) {
157 if ($class->isAssociation() ===
true) {
158 $members = $class->getManyToOneMembers();
159 if (\count($members) == 2) {
160 $manyToOne1 = $members[0]->getManyToOne();
161 $manyToOne2 = $members[1]->getManyToOne();
162 $table1 = $this->getTableName($manyToOne1->className);
163 $table2 = $this->getTableName($manyToOne2->className);
164 $class1 = $this->classes[$table1];
165 $class2 = $this->classes[$table2];
166 $reflexive = ($class1 === $class2);
168 $table1Member = $table2Member = $table .
's';
169 $altName1 = $this->getAlternateName($manyToOne2->name, \current($this->getPrimaryKeys($table1))) .
's';
171 $table1Member = \lcfirst($table1) .
's';
172 $table2Member = \lcfirst($table2) .
's';
173 $altName1 = $altName2 = $table .
's';
175 $joinTable1 = $this->getJoinTableArray($class1, $manyToOne1);
176 $joinTable2 = $this->getJoinTableArray($class2, $manyToOne2);
177 $class1->removeOneToManyMemberByClassAssociation($class->getName());
178 $class1->addManyToMany($table2Member, $manyToOne2->className, $table1Member, $table, $joinTable1, $joinTable2, $altName1);
180 $class2->removeOneToManyMemberByClassAssociation($class->getName());
181 $class2->addManyToMany($table1Member, $manyToOne1->className, $table2Member, $table, $joinTable2, $joinTable1, $altName2);
183 unset($this->classes[$table]);