Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
TableReversor.php
Go to the documentation of this file.
1<?php
3
7
18
19 private $model;
20
21 private $fkFieldsToAdd = [];
22
23 private $fkFieldTypesToAdd = [];
24
25 private $metas;
26
27 public function __construct($model = null) {
28 $this->model = $model;
29 }
30
31 public function initFromClass() {
32 if (isset($this->model))
33 $this->metas = OrmUtils::getModelMetadata($this->model);
34 }
35
36 public function init($metas) {
37 $this->metas = $metas;
38 }
39
40 public function getTable() {
41 return $this->metas['#tableName'];
42 }
43
44 public function generateSQL(DbGenerator $generator) {
45 $table = $this->metas['#tableName'];
46 $primaryKeys = $this->metas['#primaryKeys'];
47 $serializables = $this->getSerializableFields();
48 $nullables = $this->metas['#nullable'];
49 $fieldTypes = $this->metas['#fieldTypes'];
50 $manyToOnes = $this->metas['#manyToOne'];
51 $this->scanManyToManys($generator);
52 $this->generatePks($generator, $primaryKeys, $table, $fieldTypes, $nullables);
53 $this->generateForeignKeys($generator, $manyToOnes, $table);
54 $serializables = \array_unique(\array_merge($serializables, $this->fkFieldsToAdd));
55 $fieldTypes = \array_merge($fieldTypes, $this->fkFieldTypesToAdd);
56 $fieldsAttributes = $this->generateFieldsAttributes($serializables, $fieldTypes, $nullables);
57 $generator->createTable($table, $fieldsAttributes);
58 foreach ($this->fkFieldsToAdd as $fkField) {
59 $generator->addKey($table, [
60 $fkField
61 ], '');
62 }
63 }
64
65 protected function getSerializableFields() {
66 $notSerializable = $this->metas['#notSerializable'];
67 $fieldNames = $this->metas['#fieldNames'];
68 return \array_diff($fieldNames, $notSerializable);
69 }
70
71 public function scanManyToManys(DbGenerator $generator) {
72 if (isset($this->metas['#manyToMany'])) {
73 $manyToManys = $this->metas['#manyToMany'];
74 foreach ($manyToManys as $member => $manyToMany) {
75 if (isset($this->metas['#joinTable'][$member])) {
76 $annotJoinTable = $this->metas['#joinTable'][$member];
77 $generator->addManyToMany($annotJoinTable, $manyToMany['targetEntity']);
78 }
79 }
80 }
81 }
82
83 public function addPrimaryKeys(DbGenerator $generator,array $primayKeys){
84 $nullables = $this->metas['#nullable'];
85 $fieldTypes = $this->metas['#fieldTypes'];
86 $this->generatePks($generator,$primayKeys,$this->getTable(),$fieldTypes,$nullables);
87 }
88
89 protected function generatePks(DbGenerator $generator, $primaryKeys, $table, $fieldTypes, $nullables) {
90 if(\is_array($primaryKeys)) {
91 $generator->addKey($table, $primaryKeys);
92 if (\count($primaryKeys) === 1 && $generator->isInt($fieldTypes[$fpk=\current($primaryKeys)])) {
93 $generator->addAutoInc($table, $fpk,$this->getFieldAttributes($generator, $fpk, $nullables, $fieldTypes, true));
94 }
95 }
96 }
97
98 protected function generateFieldsAttributes($serializables, $fieldTypes, $nullables) {
99 $fieldsAttributes = [];
100 foreach ($serializables as $field) {
101 $fieldsAttributes[] = $this->_generateFieldAttributes($field, $nullables, $fieldTypes);
102 }
103 return $fieldsAttributes;
104 }
105
106 public function getFieldAttributes(DbGenerator $generator, $field, $nullables, $fieldTypes, $forPk = false) {
107 return $generator->generateField($this->_generateFieldAttributes($field, $nullables, $fieldTypes), $forPk);
108 }
109
110 protected function _generateFieldAttributes($field, $nullables, $fieldTypes) {
111 $nullable = 'NOT NULL';
112 if (\array_search($field, $nullables) !== false) {
113 $nullable = '';
114 }
115 return [
116 'name' => $field,
117 'type' => $fieldTypes[$field]??DbTypes::DEFAULT_TYPE,
118 'extra' => $nullable
119 ];
120 }
121
122 protected function generateForeignKey(DbGenerator $generator, $tableName, $member) {
123 $fieldAnnot = OrmUtils::getMemberJoinColumns('', $member, $this->metas);
124 if ($fieldAnnot !== null) {
125 $annotationArray = $fieldAnnot[1];
126 $referencesTableName = OrmUtils::getTableName($annotationArray['className']);
127 $referencesFieldName = OrmUtils::getFirstKey($annotationArray['className']);
128 $fkFieldName = $fieldAnnot[0];
129 $this->fkFieldsToAdd[] = $fkFieldName;
130 $this->fkFieldTypesToAdd[$fkFieldName] = OrmUtils::getFieldType($annotationArray['className'], $referencesFieldName);
131 $generator->addForeignKey($tableName, $fkFieldName, $referencesTableName, $referencesFieldName);
132 }
133 }
134
135 protected function generateForeignKeys(DbGenerator $generator, $manyToOnes, $tableName) {
136 foreach ($manyToOnes as $member) {
137 $this->generateForeignKey($generator, $tableName, $member);
138 }
139 }
140}
createTable($name, $fieldsAttributes, $attributes=[])
addAutoInc($tableName, $fieldName, $fieldInfos, $value=1)
generateField($fieldAttributes, $forPk=false)
addManyToMany($jointableInfos, $targetEntity)
addKey($tableName, $fieldNames, $type='PRIMARY')
addForeignKey($tableName, $fkFieldName, $referencesTableName, $referencesFieldName, $fkName=null, $onDelete='CASCADE', $onUpdate='NO ACTION')
Manage Databases types.
Definition DbTypes.php:14
Object/relational mapping utilities.
Definition OrmUtils.php:17
static getTableName($class)
Definition OrmUtils.php:45
static getModelMetadata($className)
Definition OrmUtils.php:21
addPrimaryKeys(DbGenerator $generator, array $primayKeys)
generateSQL(DbGenerator $generator)
scanManyToManys(DbGenerator $generator)
_generateFieldAttributes($field, $nullables, $fieldTypes)
getFieldAttributes(DbGenerator $generator, $field, $nullables, $fieldTypes, $forPk=false)
generateForeignKey(DbGenerator $generator, $tableName, $member)
generatePks(DbGenerator $generator, $primaryKeys, $table, $fieldTypes, $nullables)
generateFieldsAttributes($serializables, $fieldTypes, $nullables)
generateForeignKeys(DbGenerator $generator, $manyToOnes, $tableName)
static getMemberJoinColumns($instance, $member, $metaDatas=NULL)