@@ -16,241 +16,241 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class BeanDescriptor implements BeanDescriptorInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * @var Table |
|
21 | - */ |
|
22 | - private $table; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var SchemaAnalyzer |
|
26 | - */ |
|
27 | - private $schemaAnalyzer; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var Schema |
|
31 | - */ |
|
32 | - private $schema; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var AbstractBeanPropertyDescriptor[] |
|
36 | - */ |
|
37 | - private $beanPropertyDescriptors = []; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var TDBMSchemaAnalyzer |
|
41 | - */ |
|
42 | - private $tdbmSchemaAnalyzer; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var NamingStrategyInterface |
|
46 | - */ |
|
47 | - private $namingStrategy; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param Table $table |
|
51 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
52 | - * @param Schema $schema |
|
53 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
54 | - * @param NamingStrategyInterface $namingStrategy |
|
55 | - */ |
|
56 | - public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
57 | - { |
|
58 | - $this->table = $table; |
|
59 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
60 | - $this->schema = $schema; |
|
61 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
62 | - $this->namingStrategy = $namingStrategy; |
|
63 | - $this->initBeanPropertyDescriptors(); |
|
64 | - } |
|
65 | - |
|
66 | - private function initBeanPropertyDescriptors() |
|
67 | - { |
|
68 | - $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
73 | - * |
|
74 | - * @param Table $table |
|
75 | - * @param Column $column |
|
76 | - * |
|
77 | - * @return ForeignKeyConstraint|null |
|
78 | - */ |
|
79 | - private function isPartOfForeignKey(Table $table, Column $column) |
|
80 | - { |
|
81 | - $localColumnName = $column->getName(); |
|
82 | - foreach ($table->getForeignKeys() as $foreignKey) { |
|
83 | - foreach ($foreignKey->getColumns() as $columnName) { |
|
84 | - if ($columnName === $localColumnName) { |
|
85 | - return $foreignKey; |
|
86 | - } |
|
87 | - } |
|
88 | - } |
|
89 | - |
|
90 | - return; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @return AbstractBeanPropertyDescriptor[] |
|
95 | - */ |
|
96 | - public function getBeanPropertyDescriptors() |
|
97 | - { |
|
98 | - return $this->beanPropertyDescriptors; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
103 | - * |
|
104 | - * @return AbstractBeanPropertyDescriptor[] |
|
105 | - */ |
|
106 | - public function getConstructorProperties() |
|
107 | - { |
|
108 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
109 | - return $property->isCompulsory(); |
|
110 | - }); |
|
111 | - |
|
112 | - return $constructorProperties; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the list of columns that have default values for a given table. |
|
117 | - * |
|
118 | - * @return AbstractBeanPropertyDescriptor[] |
|
119 | - */ |
|
120 | - public function getPropertiesWithDefault() |
|
121 | - { |
|
122 | - $properties = $this->getPropertiesForTable($this->table); |
|
123 | - $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
124 | - return $property->hasDefault(); |
|
125 | - }); |
|
126 | - |
|
127 | - return $defaultProperties; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Returns the list of properties exposed as getters and setters in this class. |
|
132 | - * |
|
133 | - * @return AbstractBeanPropertyDescriptor[] |
|
134 | - */ |
|
135 | - public function getExposedProperties(): array |
|
136 | - { |
|
137 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
138 | - return $property->getTable()->getName() == $this->table->getName(); |
|
139 | - }); |
|
140 | - |
|
141 | - return $exposedProperties; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns the list of properties for this table (including parent tables). |
|
146 | - * |
|
147 | - * @param Table $table |
|
148 | - * |
|
149 | - * @return AbstractBeanPropertyDescriptor[] |
|
150 | - */ |
|
151 | - private function getProperties(Table $table) |
|
152 | - { |
|
153 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
154 | - if ($parentRelationship) { |
|
155 | - $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
156 | - $properties = $this->getProperties($parentTable); |
|
157 | - // we merge properties by overriding property names. |
|
158 | - $localProperties = $this->getPropertiesForTable($table); |
|
159 | - foreach ($localProperties as $name => $property) { |
|
160 | - // We do not override properties if this is a primary key! |
|
161 | - if ($property->isPrimaryKey()) { |
|
162 | - continue; |
|
163 | - } |
|
164 | - $properties[$name] = $property; |
|
165 | - } |
|
166 | - } else { |
|
167 | - $properties = $this->getPropertiesForTable($table); |
|
168 | - } |
|
169 | - |
|
170 | - return $properties; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Returns the list of properties for this table (ignoring parent tables). |
|
175 | - * |
|
176 | - * @param Table $table |
|
177 | - * |
|
178 | - * @return AbstractBeanPropertyDescriptor[] |
|
179 | - */ |
|
180 | - private function getPropertiesForTable(Table $table) |
|
181 | - { |
|
182 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
183 | - if ($parentRelationship) { |
|
184 | - $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
185 | - } else { |
|
186 | - $ignoreColumns = []; |
|
187 | - } |
|
188 | - |
|
189 | - $beanPropertyDescriptors = []; |
|
190 | - |
|
191 | - foreach ($table->getColumns() as $column) { |
|
192 | - if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
193 | - continue; |
|
194 | - } |
|
195 | - |
|
196 | - $fk = $this->isPartOfForeignKey($table, $column); |
|
197 | - if ($fk !== null) { |
|
198 | - // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
199 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
200 | - if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
201 | - continue 2; |
|
202 | - } |
|
203 | - } |
|
204 | - // Check that this property is not an inheritance relationship |
|
205 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
206 | - if ($parentRelationship === $fk) { |
|
207 | - continue; |
|
208 | - } |
|
209 | - |
|
210 | - $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
211 | - } else { |
|
212 | - $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - // Now, let's get the name of all properties and let's check there is no duplicate. |
|
217 | - /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
218 | - $names = []; |
|
219 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | - if (isset($names[$name])) { |
|
222 | - $names[$name]->useAlternativeName(); |
|
223 | - $beanDescriptor->useAlternativeName(); |
|
224 | - } else { |
|
225 | - $names[$name] = $beanDescriptor; |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - // Final check (throw exceptions if problem arises) |
|
230 | - $names = []; |
|
231 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
232 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
233 | - if (isset($names[$name])) { |
|
234 | - throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
235 | - } else { |
|
236 | - $names[$name] = $beanDescriptor; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - // Last step, let's rebuild the list with a map: |
|
241 | - $beanPropertyDescriptorsMap = []; |
|
242 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
243 | - $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
244 | - } |
|
245 | - |
|
246 | - return $beanPropertyDescriptorsMap; |
|
247 | - } |
|
248 | - |
|
249 | - private function generateBeanConstructor() : string |
|
250 | - { |
|
251 | - $constructorProperties = $this->getConstructorProperties(); |
|
252 | - |
|
253 | - $constructorCode = ' /** |
|
19 | + /** |
|
20 | + * @var Table |
|
21 | + */ |
|
22 | + private $table; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var SchemaAnalyzer |
|
26 | + */ |
|
27 | + private $schemaAnalyzer; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var Schema |
|
31 | + */ |
|
32 | + private $schema; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var AbstractBeanPropertyDescriptor[] |
|
36 | + */ |
|
37 | + private $beanPropertyDescriptors = []; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var TDBMSchemaAnalyzer |
|
41 | + */ |
|
42 | + private $tdbmSchemaAnalyzer; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var NamingStrategyInterface |
|
46 | + */ |
|
47 | + private $namingStrategy; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param Table $table |
|
51 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
52 | + * @param Schema $schema |
|
53 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
54 | + * @param NamingStrategyInterface $namingStrategy |
|
55 | + */ |
|
56 | + public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
57 | + { |
|
58 | + $this->table = $table; |
|
59 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
60 | + $this->schema = $schema; |
|
61 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
62 | + $this->namingStrategy = $namingStrategy; |
|
63 | + $this->initBeanPropertyDescriptors(); |
|
64 | + } |
|
65 | + |
|
66 | + private function initBeanPropertyDescriptors() |
|
67 | + { |
|
68 | + $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
73 | + * |
|
74 | + * @param Table $table |
|
75 | + * @param Column $column |
|
76 | + * |
|
77 | + * @return ForeignKeyConstraint|null |
|
78 | + */ |
|
79 | + private function isPartOfForeignKey(Table $table, Column $column) |
|
80 | + { |
|
81 | + $localColumnName = $column->getName(); |
|
82 | + foreach ($table->getForeignKeys() as $foreignKey) { |
|
83 | + foreach ($foreignKey->getColumns() as $columnName) { |
|
84 | + if ($columnName === $localColumnName) { |
|
85 | + return $foreignKey; |
|
86 | + } |
|
87 | + } |
|
88 | + } |
|
89 | + |
|
90 | + return; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @return AbstractBeanPropertyDescriptor[] |
|
95 | + */ |
|
96 | + public function getBeanPropertyDescriptors() |
|
97 | + { |
|
98 | + return $this->beanPropertyDescriptors; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
103 | + * |
|
104 | + * @return AbstractBeanPropertyDescriptor[] |
|
105 | + */ |
|
106 | + public function getConstructorProperties() |
|
107 | + { |
|
108 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
109 | + return $property->isCompulsory(); |
|
110 | + }); |
|
111 | + |
|
112 | + return $constructorProperties; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the list of columns that have default values for a given table. |
|
117 | + * |
|
118 | + * @return AbstractBeanPropertyDescriptor[] |
|
119 | + */ |
|
120 | + public function getPropertiesWithDefault() |
|
121 | + { |
|
122 | + $properties = $this->getPropertiesForTable($this->table); |
|
123 | + $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
124 | + return $property->hasDefault(); |
|
125 | + }); |
|
126 | + |
|
127 | + return $defaultProperties; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Returns the list of properties exposed as getters and setters in this class. |
|
132 | + * |
|
133 | + * @return AbstractBeanPropertyDescriptor[] |
|
134 | + */ |
|
135 | + public function getExposedProperties(): array |
|
136 | + { |
|
137 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
138 | + return $property->getTable()->getName() == $this->table->getName(); |
|
139 | + }); |
|
140 | + |
|
141 | + return $exposedProperties; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns the list of properties for this table (including parent tables). |
|
146 | + * |
|
147 | + * @param Table $table |
|
148 | + * |
|
149 | + * @return AbstractBeanPropertyDescriptor[] |
|
150 | + */ |
|
151 | + private function getProperties(Table $table) |
|
152 | + { |
|
153 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
154 | + if ($parentRelationship) { |
|
155 | + $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
156 | + $properties = $this->getProperties($parentTable); |
|
157 | + // we merge properties by overriding property names. |
|
158 | + $localProperties = $this->getPropertiesForTable($table); |
|
159 | + foreach ($localProperties as $name => $property) { |
|
160 | + // We do not override properties if this is a primary key! |
|
161 | + if ($property->isPrimaryKey()) { |
|
162 | + continue; |
|
163 | + } |
|
164 | + $properties[$name] = $property; |
|
165 | + } |
|
166 | + } else { |
|
167 | + $properties = $this->getPropertiesForTable($table); |
|
168 | + } |
|
169 | + |
|
170 | + return $properties; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Returns the list of properties for this table (ignoring parent tables). |
|
175 | + * |
|
176 | + * @param Table $table |
|
177 | + * |
|
178 | + * @return AbstractBeanPropertyDescriptor[] |
|
179 | + */ |
|
180 | + private function getPropertiesForTable(Table $table) |
|
181 | + { |
|
182 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
183 | + if ($parentRelationship) { |
|
184 | + $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
185 | + } else { |
|
186 | + $ignoreColumns = []; |
|
187 | + } |
|
188 | + |
|
189 | + $beanPropertyDescriptors = []; |
|
190 | + |
|
191 | + foreach ($table->getColumns() as $column) { |
|
192 | + if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
193 | + continue; |
|
194 | + } |
|
195 | + |
|
196 | + $fk = $this->isPartOfForeignKey($table, $column); |
|
197 | + if ($fk !== null) { |
|
198 | + // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
199 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
200 | + if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
201 | + continue 2; |
|
202 | + } |
|
203 | + } |
|
204 | + // Check that this property is not an inheritance relationship |
|
205 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
206 | + if ($parentRelationship === $fk) { |
|
207 | + continue; |
|
208 | + } |
|
209 | + |
|
210 | + $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
211 | + } else { |
|
212 | + $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + // Now, let's get the name of all properties and let's check there is no duplicate. |
|
217 | + /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
218 | + $names = []; |
|
219 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | + if (isset($names[$name])) { |
|
222 | + $names[$name]->useAlternativeName(); |
|
223 | + $beanDescriptor->useAlternativeName(); |
|
224 | + } else { |
|
225 | + $names[$name] = $beanDescriptor; |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + // Final check (throw exceptions if problem arises) |
|
230 | + $names = []; |
|
231 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
232 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
233 | + if (isset($names[$name])) { |
|
234 | + throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
235 | + } else { |
|
236 | + $names[$name] = $beanDescriptor; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + // Last step, let's rebuild the list with a map: |
|
241 | + $beanPropertyDescriptorsMap = []; |
|
242 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
243 | + $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
244 | + } |
|
245 | + |
|
246 | + return $beanPropertyDescriptorsMap; |
|
247 | + } |
|
248 | + |
|
249 | + private function generateBeanConstructor() : string |
|
250 | + { |
|
251 | + $constructorProperties = $this->getConstructorProperties(); |
|
252 | + |
|
253 | + $constructorCode = ' /** |
|
254 | 254 | * The constructor takes all compulsory arguments. |
255 | 255 | * |
256 | 256 | %s |
@@ -260,110 +260,110 @@ discard block |
||
260 | 260 | %s%s } |
261 | 261 | '; |
262 | 262 | |
263 | - $paramAnnotations = []; |
|
264 | - $arguments = []; |
|
265 | - $assigns = []; |
|
266 | - $parentConstructorArguments = []; |
|
267 | - |
|
268 | - foreach ($constructorProperties as $property) { |
|
269 | - $className = $property->getClassName(); |
|
270 | - if ($className) { |
|
271 | - $arguments[] = $className.' '.$property->getVariableName(); |
|
272 | - } else { |
|
273 | - $arguments[] = $property->getVariableName(); |
|
274 | - } |
|
275 | - $paramAnnotations[] = $property->getParamAnnotation(); |
|
276 | - if ($property->getTable()->getName() === $this->table->getName()) { |
|
277 | - $assigns[] = $property->getConstructorAssignCode()."\n"; |
|
278 | - } else { |
|
279 | - $parentConstructorArguments[] = $property->getVariableName(); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
284 | - |
|
285 | - foreach ($this->getPropertiesWithDefault() as $property) { |
|
286 | - $assigns[] = $property->assignToDefaultCode()."\n"; |
|
287 | - } |
|
288 | - |
|
289 | - return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns)); |
|
290 | - } |
|
291 | - |
|
292 | - public function getDirectForeignKeysDescriptors(): array |
|
293 | - { |
|
294 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
295 | - |
|
296 | - $descriptors = []; |
|
297 | - |
|
298 | - foreach ($fks as $fk) { |
|
299 | - $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy); |
|
300 | - } |
|
301 | - |
|
302 | - return $descriptors; |
|
303 | - } |
|
304 | - |
|
305 | - private function getPivotTableDescriptors(): array |
|
306 | - { |
|
307 | - $descs = []; |
|
308 | - foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
309 | - // There are exactly 2 FKs since this is a pivot table. |
|
310 | - $fks = array_values($table->getForeignKeys()); |
|
311 | - |
|
312 | - if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
313 | - list($localFk, $remoteFk) = $fks; |
|
314 | - } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
315 | - list($remoteFk, $localFk) = $fks; |
|
316 | - } else { |
|
317 | - continue; |
|
318 | - } |
|
319 | - |
|
320 | - $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy); |
|
321 | - } |
|
322 | - |
|
323 | - return $descs; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Returns the list of method descriptors (and applies the alternative name if needed). |
|
328 | - * |
|
329 | - * @return MethodDescriptorInterface[] |
|
330 | - */ |
|
331 | - private function getMethodDescriptors(): array |
|
332 | - { |
|
333 | - $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors(); |
|
334 | - $pivotTableDescriptors = $this->getPivotTableDescriptors(); |
|
335 | - |
|
336 | - $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors); |
|
337 | - |
|
338 | - // Descriptors by method names |
|
339 | - $descriptorsByMethodName = []; |
|
340 | - |
|
341 | - foreach ($descriptors as $descriptor) { |
|
342 | - $descriptorsByMethodName[$descriptor->getName()][] = $descriptor; |
|
343 | - } |
|
344 | - |
|
345 | - foreach ($descriptorsByMethodName as $descriptorsForMethodName) { |
|
346 | - if (count($descriptorsForMethodName) > 1) { |
|
347 | - foreach ($descriptorsForMethodName as $descriptor) { |
|
348 | - $descriptor->useAlternativeName(); |
|
349 | - } |
|
350 | - } |
|
351 | - } |
|
352 | - |
|
353 | - return $descriptors; |
|
354 | - } |
|
355 | - |
|
356 | - public function generateJsonSerialize(): string |
|
357 | - { |
|
358 | - $tableName = $this->table->getName(); |
|
359 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
360 | - if ($parentFk !== null) { |
|
361 | - $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
362 | - } else { |
|
363 | - $initializer = '$array = [];'; |
|
364 | - } |
|
365 | - |
|
366 | - $str = ' |
|
263 | + $paramAnnotations = []; |
|
264 | + $arguments = []; |
|
265 | + $assigns = []; |
|
266 | + $parentConstructorArguments = []; |
|
267 | + |
|
268 | + foreach ($constructorProperties as $property) { |
|
269 | + $className = $property->getClassName(); |
|
270 | + if ($className) { |
|
271 | + $arguments[] = $className.' '.$property->getVariableName(); |
|
272 | + } else { |
|
273 | + $arguments[] = $property->getVariableName(); |
|
274 | + } |
|
275 | + $paramAnnotations[] = $property->getParamAnnotation(); |
|
276 | + if ($property->getTable()->getName() === $this->table->getName()) { |
|
277 | + $assigns[] = $property->getConstructorAssignCode()."\n"; |
|
278 | + } else { |
|
279 | + $parentConstructorArguments[] = $property->getVariableName(); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
284 | + |
|
285 | + foreach ($this->getPropertiesWithDefault() as $property) { |
|
286 | + $assigns[] = $property->assignToDefaultCode()."\n"; |
|
287 | + } |
|
288 | + |
|
289 | + return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns)); |
|
290 | + } |
|
291 | + |
|
292 | + public function getDirectForeignKeysDescriptors(): array |
|
293 | + { |
|
294 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
295 | + |
|
296 | + $descriptors = []; |
|
297 | + |
|
298 | + foreach ($fks as $fk) { |
|
299 | + $descriptors[] = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy); |
|
300 | + } |
|
301 | + |
|
302 | + return $descriptors; |
|
303 | + } |
|
304 | + |
|
305 | + private function getPivotTableDescriptors(): array |
|
306 | + { |
|
307 | + $descs = []; |
|
308 | + foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
309 | + // There are exactly 2 FKs since this is a pivot table. |
|
310 | + $fks = array_values($table->getForeignKeys()); |
|
311 | + |
|
312 | + if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
313 | + list($localFk, $remoteFk) = $fks; |
|
314 | + } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
315 | + list($remoteFk, $localFk) = $fks; |
|
316 | + } else { |
|
317 | + continue; |
|
318 | + } |
|
319 | + |
|
320 | + $descs[] = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy); |
|
321 | + } |
|
322 | + |
|
323 | + return $descs; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Returns the list of method descriptors (and applies the alternative name if needed). |
|
328 | + * |
|
329 | + * @return MethodDescriptorInterface[] |
|
330 | + */ |
|
331 | + private function getMethodDescriptors(): array |
|
332 | + { |
|
333 | + $directForeignKeyDescriptors = $this->getDirectForeignKeysDescriptors(); |
|
334 | + $pivotTableDescriptors = $this->getPivotTableDescriptors(); |
|
335 | + |
|
336 | + $descriptors = array_merge($directForeignKeyDescriptors, $pivotTableDescriptors); |
|
337 | + |
|
338 | + // Descriptors by method names |
|
339 | + $descriptorsByMethodName = []; |
|
340 | + |
|
341 | + foreach ($descriptors as $descriptor) { |
|
342 | + $descriptorsByMethodName[$descriptor->getName()][] = $descriptor; |
|
343 | + } |
|
344 | + |
|
345 | + foreach ($descriptorsByMethodName as $descriptorsForMethodName) { |
|
346 | + if (count($descriptorsForMethodName) > 1) { |
|
347 | + foreach ($descriptorsForMethodName as $descriptor) { |
|
348 | + $descriptor->useAlternativeName(); |
|
349 | + } |
|
350 | + } |
|
351 | + } |
|
352 | + |
|
353 | + return $descriptors; |
|
354 | + } |
|
355 | + |
|
356 | + public function generateJsonSerialize(): string |
|
357 | + { |
|
358 | + $tableName = $this->table->getName(); |
|
359 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
360 | + if ($parentFk !== null) { |
|
361 | + $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
362 | + } else { |
|
363 | + $initializer = '$array = [];'; |
|
364 | + } |
|
365 | + |
|
366 | + $str = ' |
|
367 | 367 | /** |
368 | 368 | * Serializes the object for JSON encoding. |
369 | 369 | * |
@@ -379,77 +379,77 @@ discard block |
||
379 | 379 | } |
380 | 380 | '; |
381 | 381 | |
382 | - $propertiesCode = ''; |
|
383 | - foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
384 | - $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
385 | - } |
|
386 | - |
|
387 | - // Many2many relationships |
|
388 | - $methodsCode = ''; |
|
389 | - foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
390 | - $methodsCode .= $methodDescriptor->getJsonSerializeCode(); |
|
391 | - } |
|
392 | - |
|
393 | - return sprintf($str, $initializer, $propertiesCode, $methodsCode); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Returns as an array the class we need to extend from and the list of use statements. |
|
398 | - * |
|
399 | - * @param ForeignKeyConstraint|null $parentFk |
|
400 | - * @return array |
|
401 | - */ |
|
402 | - private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
|
403 | - { |
|
404 | - $classes = []; |
|
405 | - if ($parentFk !== null) { |
|
406 | - $extends = $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
407 | - $classes[] = $extends; |
|
408 | - } |
|
409 | - |
|
410 | - foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
411 | - $className = $beanPropertyDescriptor->getClassName(); |
|
412 | - if (null !== $className) { |
|
413 | - $classes[] = $beanPropertyDescriptor->getClassName(); |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - foreach ($this->getMethodDescriptors() as $descriptor) { |
|
418 | - $classes = array_merge($classes, $descriptor->getUsedClasses()); |
|
419 | - } |
|
420 | - |
|
421 | - $classes = array_unique($classes); |
|
422 | - |
|
423 | - return $classes; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
428 | - * |
|
429 | - * @param string $beannamespace The namespace of the bean |
|
430 | - * @return string |
|
431 | - */ |
|
432 | - public function generatePhpCode($beannamespace): string |
|
433 | - { |
|
434 | - $tableName = $this->table->getName(); |
|
435 | - $baseClassName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
436 | - $className = $this->namingStrategy->getBeanClassName($tableName); |
|
437 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
438 | - |
|
439 | - $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
440 | - |
|
441 | - $uses = array_map(function ($className) use ($beannamespace) { |
|
442 | - return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
443 | - }, $classes); |
|
444 | - $use = implode('', $uses); |
|
445 | - |
|
446 | - $extends = $this->getExtendedBeanClassName(); |
|
447 | - if ($extends === null) { |
|
448 | - $extends = 'AbstractTDBMObject'; |
|
449 | - $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
450 | - } |
|
451 | - |
|
452 | - $str = "<?php |
|
382 | + $propertiesCode = ''; |
|
383 | + foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
384 | + $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
385 | + } |
|
386 | + |
|
387 | + // Many2many relationships |
|
388 | + $methodsCode = ''; |
|
389 | + foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
390 | + $methodsCode .= $methodDescriptor->getJsonSerializeCode(); |
|
391 | + } |
|
392 | + |
|
393 | + return sprintf($str, $initializer, $propertiesCode, $methodsCode); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Returns as an array the class we need to extend from and the list of use statements. |
|
398 | + * |
|
399 | + * @param ForeignKeyConstraint|null $parentFk |
|
400 | + * @return array |
|
401 | + */ |
|
402 | + private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
|
403 | + { |
|
404 | + $classes = []; |
|
405 | + if ($parentFk !== null) { |
|
406 | + $extends = $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
407 | + $classes[] = $extends; |
|
408 | + } |
|
409 | + |
|
410 | + foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
411 | + $className = $beanPropertyDescriptor->getClassName(); |
|
412 | + if (null !== $className) { |
|
413 | + $classes[] = $beanPropertyDescriptor->getClassName(); |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + foreach ($this->getMethodDescriptors() as $descriptor) { |
|
418 | + $classes = array_merge($classes, $descriptor->getUsedClasses()); |
|
419 | + } |
|
420 | + |
|
421 | + $classes = array_unique($classes); |
|
422 | + |
|
423 | + return $classes; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
428 | + * |
|
429 | + * @param string $beannamespace The namespace of the bean |
|
430 | + * @return string |
|
431 | + */ |
|
432 | + public function generatePhpCode($beannamespace): string |
|
433 | + { |
|
434 | + $tableName = $this->table->getName(); |
|
435 | + $baseClassName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
436 | + $className = $this->namingStrategy->getBeanClassName($tableName); |
|
437 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
438 | + |
|
439 | + $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
440 | + |
|
441 | + $uses = array_map(function ($className) use ($beannamespace) { |
|
442 | + return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
443 | + }, $classes); |
|
444 | + $use = implode('', $uses); |
|
445 | + |
|
446 | + $extends = $this->getExtendedBeanClassName(); |
|
447 | + if ($extends === null) { |
|
448 | + $extends = 'AbstractTDBMObject'; |
|
449 | + $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
450 | + } |
|
451 | + |
|
452 | + $str = "<?php |
|
453 | 453 | namespace {$beannamespace}\\Generated; |
454 | 454 | |
455 | 455 | use Mouf\\Database\\TDBM\\ResultIterator; |
@@ -469,125 +469,125 @@ discard block |
||
469 | 469 | { |
470 | 470 | "; |
471 | 471 | |
472 | - $str .= $this->generateBeanConstructor(); |
|
472 | + $str .= $this->generateBeanConstructor(); |
|
473 | 473 | |
474 | - foreach ($this->getExposedProperties() as $property) { |
|
475 | - $str .= $property->getGetterSetterCode(); |
|
476 | - } |
|
474 | + foreach ($this->getExposedProperties() as $property) { |
|
475 | + $str .= $property->getGetterSetterCode(); |
|
476 | + } |
|
477 | 477 | |
478 | - foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
479 | - $str .= $methodDescriptor->getCode(); |
|
480 | - } |
|
481 | - $str .= $this->generateJsonSerialize(); |
|
478 | + foreach ($this->getMethodDescriptors() as $methodDescriptor) { |
|
479 | + $str .= $methodDescriptor->getCode(); |
|
480 | + } |
|
481 | + $str .= $this->generateJsonSerialize(); |
|
482 | 482 | |
483 | - $str .= $this->generateGetUsedTablesCode(); |
|
483 | + $str .= $this->generateGetUsedTablesCode(); |
|
484 | 484 | |
485 | - $str .= $this->generateOnDeleteCode(); |
|
485 | + $str .= $this->generateOnDeleteCode(); |
|
486 | 486 | |
487 | - $str .= '} |
|
487 | + $str .= '} |
|
488 | 488 | '; |
489 | 489 | |
490 | - return $str; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * @param string $beanNamespace |
|
495 | - * @param string $beanClassName |
|
496 | - * |
|
497 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
498 | - */ |
|
499 | - public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
500 | - { |
|
501 | - $code = ''; |
|
502 | - $usedBeans = []; |
|
503 | - foreach ($this->table->getIndexes() as $index) { |
|
504 | - if (!$index->isPrimary()) { |
|
505 | - list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
506 | - $code .= $codeForIndex; |
|
507 | - $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
508 | - } |
|
509 | - } |
|
510 | - |
|
511 | - return [$usedBeans, $code]; |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * @param Index $index |
|
516 | - * @param string $beanNamespace |
|
517 | - * @param string $beanClassName |
|
518 | - * |
|
519 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
520 | - */ |
|
521 | - private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
522 | - { |
|
523 | - $columns = $index->getColumns(); |
|
524 | - $usedBeans = []; |
|
525 | - |
|
526 | - /* |
|
490 | + return $str; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * @param string $beanNamespace |
|
495 | + * @param string $beanClassName |
|
496 | + * |
|
497 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
498 | + */ |
|
499 | + public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
500 | + { |
|
501 | + $code = ''; |
|
502 | + $usedBeans = []; |
|
503 | + foreach ($this->table->getIndexes() as $index) { |
|
504 | + if (!$index->isPrimary()) { |
|
505 | + list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
506 | + $code .= $codeForIndex; |
|
507 | + $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
508 | + } |
|
509 | + } |
|
510 | + |
|
511 | + return [$usedBeans, $code]; |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * @param Index $index |
|
516 | + * @param string $beanNamespace |
|
517 | + * @param string $beanClassName |
|
518 | + * |
|
519 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
520 | + */ |
|
521 | + private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
522 | + { |
|
523 | + $columns = $index->getColumns(); |
|
524 | + $usedBeans = []; |
|
525 | + |
|
526 | + /* |
|
527 | 527 | * The list of elements building this index (expressed as columns or foreign keys) |
528 | 528 | * @var AbstractBeanPropertyDescriptor[] |
529 | 529 | */ |
530 | - $elements = []; |
|
531 | - |
|
532 | - foreach ($columns as $column) { |
|
533 | - $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
534 | - if ($fk !== null) { |
|
535 | - if (!in_array($fk, $elements)) { |
|
536 | - $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
537 | - } |
|
538 | - } else { |
|
539 | - $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column), $this->namingStrategy); |
|
540 | - } |
|
541 | - } |
|
542 | - |
|
543 | - // If the index is actually only a foreign key, let's bypass it entirely. |
|
544 | - if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
545 | - return [[], '']; |
|
546 | - } |
|
547 | - |
|
548 | - $methodNameComponent = []; |
|
549 | - $functionParameters = []; |
|
550 | - $first = true; |
|
551 | - foreach ($elements as $element) { |
|
552 | - $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
553 | - $functionParameter = $element->getClassName(); |
|
554 | - if ($functionParameter) { |
|
555 | - $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
556 | - $functionParameter .= ' '; |
|
557 | - } |
|
558 | - $functionParameter .= $element->getVariableName(); |
|
559 | - if ($first) { |
|
560 | - $first = false; |
|
561 | - } else { |
|
562 | - $functionParameter .= ' = null'; |
|
563 | - } |
|
564 | - $functionParameters[] = $functionParameter; |
|
565 | - } |
|
566 | - |
|
567 | - $functionParametersString = implode(', ', $functionParameters); |
|
568 | - |
|
569 | - $count = 0; |
|
570 | - |
|
571 | - $params = []; |
|
572 | - $filterArrayCode = ''; |
|
573 | - $commentArguments = []; |
|
574 | - foreach ($elements as $element) { |
|
575 | - $params[] = $element->getParamAnnotation(); |
|
576 | - if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
577 | - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
578 | - } else { |
|
579 | - ++$count; |
|
580 | - $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
581 | - } |
|
582 | - $commentArguments[] = substr($element->getVariableName(), 1); |
|
583 | - } |
|
584 | - $paramsString = implode("\n", $params); |
|
585 | - |
|
586 | - if ($index->isUnique()) { |
|
587 | - $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
588 | - $returnType = "{$beanClassName}"; |
|
589 | - |
|
590 | - $code = " |
|
530 | + $elements = []; |
|
531 | + |
|
532 | + foreach ($columns as $column) { |
|
533 | + $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
534 | + if ($fk !== null) { |
|
535 | + if (!in_array($fk, $elements)) { |
|
536 | + $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
|
537 | + } |
|
538 | + } else { |
|
539 | + $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column), $this->namingStrategy); |
|
540 | + } |
|
541 | + } |
|
542 | + |
|
543 | + // If the index is actually only a foreign key, let's bypass it entirely. |
|
544 | + if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
545 | + return [[], '']; |
|
546 | + } |
|
547 | + |
|
548 | + $methodNameComponent = []; |
|
549 | + $functionParameters = []; |
|
550 | + $first = true; |
|
551 | + foreach ($elements as $element) { |
|
552 | + $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
553 | + $functionParameter = $element->getClassName(); |
|
554 | + if ($functionParameter) { |
|
555 | + $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
556 | + $functionParameter .= ' '; |
|
557 | + } |
|
558 | + $functionParameter .= $element->getVariableName(); |
|
559 | + if ($first) { |
|
560 | + $first = false; |
|
561 | + } else { |
|
562 | + $functionParameter .= ' = null'; |
|
563 | + } |
|
564 | + $functionParameters[] = $functionParameter; |
|
565 | + } |
|
566 | + |
|
567 | + $functionParametersString = implode(', ', $functionParameters); |
|
568 | + |
|
569 | + $count = 0; |
|
570 | + |
|
571 | + $params = []; |
|
572 | + $filterArrayCode = ''; |
|
573 | + $commentArguments = []; |
|
574 | + foreach ($elements as $element) { |
|
575 | + $params[] = $element->getParamAnnotation(); |
|
576 | + if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
577 | + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
578 | + } else { |
|
579 | + ++$count; |
|
580 | + $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
581 | + } |
|
582 | + $commentArguments[] = substr($element->getVariableName(), 1); |
|
583 | + } |
|
584 | + $paramsString = implode("\n", $params); |
|
585 | + |
|
586 | + if ($index->isUnique()) { |
|
587 | + $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
588 | + $returnType = "{$beanClassName}"; |
|
589 | + |
|
590 | + $code = " |
|
591 | 591 | /** |
592 | 592 | * Get a $beanClassName filtered by ".implode(', ', $commentArguments).". |
593 | 593 | * |
@@ -602,11 +602,11 @@ discard block |
||
602 | 602 | return \$this->findOne(\$filter, [], \$additionalTablesFetch); |
603 | 603 | } |
604 | 604 | "; |
605 | - } else { |
|
606 | - $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
607 | - $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
605 | + } else { |
|
606 | + $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
607 | + $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
608 | 608 | |
609 | - $code = " |
|
609 | + $code = " |
|
610 | 610 | /** |
611 | 611 | * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).". |
612 | 612 | * |
@@ -623,29 +623,29 @@ discard block |
||
623 | 623 | return \$this->find(\$filter, [], \$orderBy, \$additionalTablesFetch, \$mode); |
624 | 624 | } |
625 | 625 | "; |
626 | - } |
|
627 | - |
|
628 | - return [$usedBeans, $code]; |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Generates the code for the getUsedTable protected method. |
|
633 | - * |
|
634 | - * @return string |
|
635 | - */ |
|
636 | - private function generateGetUsedTablesCode() |
|
637 | - { |
|
638 | - $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
639 | - if ($hasParentRelationship) { |
|
640 | - $code = sprintf(' $tables = parent::getUsedTables(); |
|
626 | + } |
|
627 | + |
|
628 | + return [$usedBeans, $code]; |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Generates the code for the getUsedTable protected method. |
|
633 | + * |
|
634 | + * @return string |
|
635 | + */ |
|
636 | + private function generateGetUsedTablesCode() |
|
637 | + { |
|
638 | + $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
639 | + if ($hasParentRelationship) { |
|
640 | + $code = sprintf(' $tables = parent::getUsedTables(); |
|
641 | 641 | $tables[] = %s; |
642 | 642 | |
643 | 643 | return $tables;', var_export($this->table->getName(), true)); |
644 | - } else { |
|
645 | - $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
646 | - } |
|
644 | + } else { |
|
645 | + $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
646 | + } |
|
647 | 647 | |
648 | - return sprintf(' |
|
648 | + return sprintf(' |
|
649 | 649 | /** |
650 | 650 | * Returns an array of used tables by this bean (from parent to child relationship). |
651 | 651 | * |
@@ -656,20 +656,20 @@ discard block |
||
656 | 656 | %s |
657 | 657 | } |
658 | 658 | ', $code); |
659 | - } |
|
660 | - |
|
661 | - private function generateOnDeleteCode() |
|
662 | - { |
|
663 | - $code = ''; |
|
664 | - $relationships = $this->getPropertiesForTable($this->table); |
|
665 | - foreach ($relationships as $relationship) { |
|
666 | - if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
667 | - $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n"); |
|
668 | - } |
|
669 | - } |
|
670 | - |
|
671 | - if ($code) { |
|
672 | - return sprintf(' |
|
659 | + } |
|
660 | + |
|
661 | + private function generateOnDeleteCode() |
|
662 | + { |
|
663 | + $code = ''; |
|
664 | + $relationships = $this->getPropertiesForTable($this->table); |
|
665 | + foreach ($relationships as $relationship) { |
|
666 | + if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
667 | + $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).");\n"); |
|
668 | + } |
|
669 | + } |
|
670 | + |
|
671 | + if ($code) { |
|
672 | + return sprintf(' |
|
673 | 673 | /** |
674 | 674 | * Method called when the bean is removed from database. |
675 | 675 | * |
@@ -679,73 +679,73 @@ discard block |
||
679 | 679 | parent::onDelete(); |
680 | 680 | %s } |
681 | 681 | ', $code); |
682 | - } |
|
683 | - |
|
684 | - return ''; |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * Returns the bean class name (without the namespace). |
|
689 | - * |
|
690 | - * @return string |
|
691 | - */ |
|
692 | - public function getBeanClassName() : string |
|
693 | - { |
|
694 | - return $this->namingStrategy->getBeanClassName($this->table->getName()); |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
698 | - * Returns the base bean class name (without the namespace). |
|
699 | - * |
|
700 | - * @return string |
|
701 | - */ |
|
702 | - public function getBaseBeanClassName() : string |
|
703 | - { |
|
704 | - return $this->namingStrategy->getBaseBeanClassName($this->table->getName()); |
|
705 | - } |
|
706 | - |
|
707 | - /** |
|
708 | - * Returns the DAO class name (without the namespace). |
|
709 | - * |
|
710 | - * @return string |
|
711 | - */ |
|
712 | - public function getDaoClassName() : string |
|
713 | - { |
|
714 | - return $this->namingStrategy->getDaoClassName($this->table->getName()); |
|
715 | - } |
|
716 | - |
|
717 | - /** |
|
718 | - * Returns the base DAO class name (without the namespace). |
|
719 | - * |
|
720 | - * @return string |
|
721 | - */ |
|
722 | - public function getBaseDaoClassName() : string |
|
723 | - { |
|
724 | - return $this->namingStrategy->getBaseDaoClassName($this->table->getName()); |
|
725 | - } |
|
726 | - |
|
727 | - /** |
|
728 | - * Returns the table used to build this bean. |
|
729 | - * |
|
730 | - * @return Table |
|
731 | - */ |
|
732 | - public function getTable(): Table |
|
733 | - { |
|
734 | - return $this->table; |
|
735 | - } |
|
736 | - |
|
737 | - /** |
|
738 | - * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
739 | - * |
|
740 | - * @return string |
|
741 | - */ |
|
742 | - public function getExtendedBeanClassName(): ?string |
|
743 | - { |
|
744 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
745 | - if ($parentFk !== null) { |
|
746 | - return $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
747 | - } else { |
|
748 | - return null; |
|
749 | - } |
|
750 | - } |
|
682 | + } |
|
683 | + |
|
684 | + return ''; |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * Returns the bean class name (without the namespace). |
|
689 | + * |
|
690 | + * @return string |
|
691 | + */ |
|
692 | + public function getBeanClassName() : string |
|
693 | + { |
|
694 | + return $this->namingStrategy->getBeanClassName($this->table->getName()); |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | + * Returns the base bean class name (without the namespace). |
|
699 | + * |
|
700 | + * @return string |
|
701 | + */ |
|
702 | + public function getBaseBeanClassName() : string |
|
703 | + { |
|
704 | + return $this->namingStrategy->getBaseBeanClassName($this->table->getName()); |
|
705 | + } |
|
706 | + |
|
707 | + /** |
|
708 | + * Returns the DAO class name (without the namespace). |
|
709 | + * |
|
710 | + * @return string |
|
711 | + */ |
|
712 | + public function getDaoClassName() : string |
|
713 | + { |
|
714 | + return $this->namingStrategy->getDaoClassName($this->table->getName()); |
|
715 | + } |
|
716 | + |
|
717 | + /** |
|
718 | + * Returns the base DAO class name (without the namespace). |
|
719 | + * |
|
720 | + * @return string |
|
721 | + */ |
|
722 | + public function getBaseDaoClassName() : string |
|
723 | + { |
|
724 | + return $this->namingStrategy->getBaseDaoClassName($this->table->getName()); |
|
725 | + } |
|
726 | + |
|
727 | + /** |
|
728 | + * Returns the table used to build this bean. |
|
729 | + * |
|
730 | + * @return Table |
|
731 | + */ |
|
732 | + public function getTable(): Table |
|
733 | + { |
|
734 | + return $this->table; |
|
735 | + } |
|
736 | + |
|
737 | + /** |
|
738 | + * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
739 | + * |
|
740 | + * @return string |
|
741 | + */ |
|
742 | + public function getExtendedBeanClassName(): ?string |
|
743 | + { |
|
744 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
|
745 | + if ($parentFk !== null) { |
|
746 | + return $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
|
747 | + } else { |
|
748 | + return null; |
|
749 | + } |
|
750 | + } |
|
751 | 751 | } |
@@ -9,52 +9,52 @@ |
||
9 | 9 | */ |
10 | 10 | interface BeanDescriptorInterface |
11 | 11 | { |
12 | - /** |
|
13 | - * Returns the table used to build this bean. |
|
14 | - * |
|
15 | - * @return Table |
|
16 | - */ |
|
17 | - public function getTable() : Table; |
|
18 | - |
|
19 | - /** |
|
20 | - * Returns the bean class name (without the namespace). |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function getBeanClassName(): string; |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns the base bean class name (without the namespace). |
|
28 | - * |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function getBaseBeanClassName(): string; |
|
32 | - |
|
33 | - /** |
|
34 | - * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
35 | - * |
|
36 | - * @return null|string |
|
37 | - */ |
|
38 | - public function getExtendedBeanClassName(): ?string; |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns the DAO class name (without the namespace). |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function getDaoClassName(): string; |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns the base DAO class name (without the namespace). |
|
49 | - * |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getBaseDaoClassName(): string; |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the list of properties exposed as getters and setters in this class. |
|
56 | - * |
|
57 | - * @return AbstractBeanPropertyDescriptor[] |
|
58 | - */ |
|
59 | - public function getExposedProperties(): array; |
|
12 | + /** |
|
13 | + * Returns the table used to build this bean. |
|
14 | + * |
|
15 | + * @return Table |
|
16 | + */ |
|
17 | + public function getTable() : Table; |
|
18 | + |
|
19 | + /** |
|
20 | + * Returns the bean class name (without the namespace). |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function getBeanClassName(): string; |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns the base bean class name (without the namespace). |
|
28 | + * |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function getBaseBeanClassName(): string; |
|
32 | + |
|
33 | + /** |
|
34 | + * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
|
35 | + * |
|
36 | + * @return null|string |
|
37 | + */ |
|
38 | + public function getExtendedBeanClassName(): ?string; |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns the DAO class name (without the namespace). |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function getDaoClassName(): string; |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns the base DAO class name (without the namespace). |
|
49 | + * |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getBaseDaoClassName(): string; |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the list of properties exposed as getters and setters in this class. |
|
56 | + * |
|
57 | + * @return AbstractBeanPropertyDescriptor[] |
|
58 | + */ |
|
59 | + public function getExposedProperties(): array; |
|
60 | 60 | } |
@@ -8,42 +8,42 @@ |
||
8 | 8 | */ |
9 | 9 | interface NamingStrategyInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * Returns the bean class name from the table name (excluding the namespace). |
|
13 | - * |
|
14 | - * @param string $tableName |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function getBeanClassName(string $tableName) : string; |
|
11 | + /** |
|
12 | + * Returns the bean class name from the table name (excluding the namespace). |
|
13 | + * |
|
14 | + * @param string $tableName |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function getBeanClassName(string $tableName) : string; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Returns the base bean class name from the table name (excluding the namespace). |
|
21 | - * |
|
22 | - * @param string $tableName |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function getBaseBeanClassName(string $tableName) : string; |
|
19 | + /** |
|
20 | + * Returns the base bean class name from the table name (excluding the namespace). |
|
21 | + * |
|
22 | + * @param string $tableName |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function getBaseBeanClassName(string $tableName) : string; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Returns the name of the DAO class from the table name (excluding the namespace). |
|
29 | - * |
|
30 | - * @param string $tableName |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function getDaoClassName(string $tableName) : string; |
|
27 | + /** |
|
28 | + * Returns the name of the DAO class from the table name (excluding the namespace). |
|
29 | + * |
|
30 | + * @param string $tableName |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function getDaoClassName(string $tableName) : string; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
37 | - * |
|
38 | - * @param string $tableName |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getBaseDaoClassName(string $tableName) : string; |
|
35 | + /** |
|
36 | + * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
37 | + * |
|
38 | + * @param string $tableName |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getBaseDaoClassName(string $tableName) : string; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Returns the class name for the DAO factory. |
|
45 | - * |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function getDaoFactoryClassName() : string; |
|
43 | + /** |
|
44 | + * Returns the class name for the DAO factory. |
|
45 | + * |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function getDaoFactoryClassName() : string; |
|
49 | 49 | } |
@@ -7,181 +7,181 @@ |
||
7 | 7 | |
8 | 8 | class DefaultNamingStrategy implements NamingStrategyInterface |
9 | 9 | { |
10 | - private $beanPrefix = ''; |
|
11 | - private $beanSuffix = ''; |
|
12 | - private $baseBeanPrefix = 'Abstract'; |
|
13 | - private $baseBeanSuffix = ''; |
|
14 | - private $daoPrefix = ''; |
|
15 | - private $daoSuffix = 'Dao'; |
|
16 | - private $baseDaoPrefix = 'Abstract'; |
|
17 | - private $baseDaoSuffix = 'Dao'; |
|
18 | - |
|
19 | - /** |
|
20 | - * Sets the string prefix to any bean class name. |
|
21 | - * |
|
22 | - * @param string $beanPrefix |
|
23 | - */ |
|
24 | - public function setBeanPrefix(string $beanPrefix) |
|
25 | - { |
|
26 | - $this->beanPrefix = $beanPrefix; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Sets the string suffix to any bean class name. |
|
31 | - * |
|
32 | - * @param string $beanSuffix |
|
33 | - */ |
|
34 | - public function setBeanSuffix(string $beanSuffix) |
|
35 | - { |
|
36 | - $this->beanSuffix = $beanSuffix; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Sets the string prefix to any base bean class name. |
|
41 | - * |
|
42 | - * @param string $baseBeanPrefix |
|
43 | - */ |
|
44 | - public function setBaseBeanPrefix(string $baseBeanPrefix) |
|
45 | - { |
|
46 | - $this->baseBeanPrefix = $baseBeanPrefix; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Sets the string suffix to any base bean class name. |
|
51 | - * |
|
52 | - * @param string $baseBeanSuffix |
|
53 | - */ |
|
54 | - public function setBaseBeanSuffix(string $baseBeanSuffix) |
|
55 | - { |
|
56 | - $this->baseBeanSuffix = $baseBeanSuffix; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Sets the string prefix to any DAO class name. |
|
61 | - * |
|
62 | - * @param string $daoPrefix |
|
63 | - */ |
|
64 | - public function setDaoPrefix(string $daoPrefix) |
|
65 | - { |
|
66 | - $this->daoPrefix = $daoPrefix; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Sets the string suffix to any DAO class name. |
|
71 | - * |
|
72 | - * @param string $daoSuffix |
|
73 | - */ |
|
74 | - public function setDaoSuffix(string $daoSuffix) |
|
75 | - { |
|
76 | - $this->daoSuffix = $daoSuffix; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Sets the string prefix to any base DAO class name. |
|
81 | - * |
|
82 | - * @param string $baseDaoPrefix |
|
83 | - */ |
|
84 | - public function setBaseDaoPrefix(string $baseDaoPrefix) |
|
85 | - { |
|
86 | - $this->baseDaoPrefix = $baseDaoPrefix; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets the string suffix to any base DAO class name. |
|
91 | - * |
|
92 | - * @param string $baseDaoSuffix |
|
93 | - */ |
|
94 | - public function setBaseDaoSuffix(string $baseDaoSuffix) |
|
95 | - { |
|
96 | - $this->baseDaoSuffix = $baseDaoSuffix; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Returns the bean class name from the table name (excluding the namespace). |
|
102 | - * |
|
103 | - * @param string $tableName |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function getBeanClassName(string $tableName): string |
|
107 | - { |
|
108 | - return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Returns the base bean class name from the table name (excluding the namespace). |
|
113 | - * |
|
114 | - * @param string $tableName |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public function getBaseBeanClassName(string $tableName): string |
|
118 | - { |
|
119 | - return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the name of the DAO class from the table name (excluding the namespace). |
|
124 | - * |
|
125 | - * @param string $tableName |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - public function getDaoClassName(string $tableName): string |
|
129 | - { |
|
130 | - return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
135 | - * |
|
136 | - * @param string $tableName |
|
137 | - * @return string |
|
138 | - */ |
|
139 | - public function getBaseDaoClassName(string $tableName): string |
|
140 | - { |
|
141 | - return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Tries to put string to the singular form (if it is plural) and camel case form. |
|
146 | - * We assume the table names are in english. |
|
147 | - * |
|
148 | - * @param $str string |
|
149 | - * |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - private static function toSingularCamelCase(string $str): string |
|
153 | - { |
|
154 | - $tokens = preg_split("/[_ ]+/", $str); |
|
155 | - $tokens = array_map([Inflector::class, 'singularize'], $tokens); |
|
156 | - |
|
157 | - $str = ''; |
|
158 | - foreach ($tokens as $token) { |
|
159 | - $str .= ucfirst(Inflector::singularize($token)); |
|
160 | - } |
|
161 | - |
|
162 | - return $str; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Put the first letter of the string in lower case. |
|
167 | - * Very useful to transform a class name into a variable name. |
|
168 | - * |
|
169 | - * @param $str string |
|
170 | - * |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - private static function toVariableName($str) |
|
174 | - { |
|
175 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns the class name for the DAO factory. |
|
180 | - * |
|
181 | - * @return string |
|
182 | - */ |
|
183 | - public function getDaoFactoryClassName(): string |
|
184 | - { |
|
185 | - return 'DaoFactory'; |
|
186 | - } |
|
10 | + private $beanPrefix = ''; |
|
11 | + private $beanSuffix = ''; |
|
12 | + private $baseBeanPrefix = 'Abstract'; |
|
13 | + private $baseBeanSuffix = ''; |
|
14 | + private $daoPrefix = ''; |
|
15 | + private $daoSuffix = 'Dao'; |
|
16 | + private $baseDaoPrefix = 'Abstract'; |
|
17 | + private $baseDaoSuffix = 'Dao'; |
|
18 | + |
|
19 | + /** |
|
20 | + * Sets the string prefix to any bean class name. |
|
21 | + * |
|
22 | + * @param string $beanPrefix |
|
23 | + */ |
|
24 | + public function setBeanPrefix(string $beanPrefix) |
|
25 | + { |
|
26 | + $this->beanPrefix = $beanPrefix; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Sets the string suffix to any bean class name. |
|
31 | + * |
|
32 | + * @param string $beanSuffix |
|
33 | + */ |
|
34 | + public function setBeanSuffix(string $beanSuffix) |
|
35 | + { |
|
36 | + $this->beanSuffix = $beanSuffix; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Sets the string prefix to any base bean class name. |
|
41 | + * |
|
42 | + * @param string $baseBeanPrefix |
|
43 | + */ |
|
44 | + public function setBaseBeanPrefix(string $baseBeanPrefix) |
|
45 | + { |
|
46 | + $this->baseBeanPrefix = $baseBeanPrefix; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Sets the string suffix to any base bean class name. |
|
51 | + * |
|
52 | + * @param string $baseBeanSuffix |
|
53 | + */ |
|
54 | + public function setBaseBeanSuffix(string $baseBeanSuffix) |
|
55 | + { |
|
56 | + $this->baseBeanSuffix = $baseBeanSuffix; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Sets the string prefix to any DAO class name. |
|
61 | + * |
|
62 | + * @param string $daoPrefix |
|
63 | + */ |
|
64 | + public function setDaoPrefix(string $daoPrefix) |
|
65 | + { |
|
66 | + $this->daoPrefix = $daoPrefix; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Sets the string suffix to any DAO class name. |
|
71 | + * |
|
72 | + * @param string $daoSuffix |
|
73 | + */ |
|
74 | + public function setDaoSuffix(string $daoSuffix) |
|
75 | + { |
|
76 | + $this->daoSuffix = $daoSuffix; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Sets the string prefix to any base DAO class name. |
|
81 | + * |
|
82 | + * @param string $baseDaoPrefix |
|
83 | + */ |
|
84 | + public function setBaseDaoPrefix(string $baseDaoPrefix) |
|
85 | + { |
|
86 | + $this->baseDaoPrefix = $baseDaoPrefix; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets the string suffix to any base DAO class name. |
|
91 | + * |
|
92 | + * @param string $baseDaoSuffix |
|
93 | + */ |
|
94 | + public function setBaseDaoSuffix(string $baseDaoSuffix) |
|
95 | + { |
|
96 | + $this->baseDaoSuffix = $baseDaoSuffix; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Returns the bean class name from the table name (excluding the namespace). |
|
102 | + * |
|
103 | + * @param string $tableName |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function getBeanClassName(string $tableName): string |
|
107 | + { |
|
108 | + return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Returns the base bean class name from the table name (excluding the namespace). |
|
113 | + * |
|
114 | + * @param string $tableName |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public function getBaseBeanClassName(string $tableName): string |
|
118 | + { |
|
119 | + return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the name of the DAO class from the table name (excluding the namespace). |
|
124 | + * |
|
125 | + * @param string $tableName |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + public function getDaoClassName(string $tableName): string |
|
129 | + { |
|
130 | + return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns the name of the base DAO class from the table name (excluding the namespace). |
|
135 | + * |
|
136 | + * @param string $tableName |
|
137 | + * @return string |
|
138 | + */ |
|
139 | + public function getBaseDaoClassName(string $tableName): string |
|
140 | + { |
|
141 | + return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Tries to put string to the singular form (if it is plural) and camel case form. |
|
146 | + * We assume the table names are in english. |
|
147 | + * |
|
148 | + * @param $str string |
|
149 | + * |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + private static function toSingularCamelCase(string $str): string |
|
153 | + { |
|
154 | + $tokens = preg_split("/[_ ]+/", $str); |
|
155 | + $tokens = array_map([Inflector::class, 'singularize'], $tokens); |
|
156 | + |
|
157 | + $str = ''; |
|
158 | + foreach ($tokens as $token) { |
|
159 | + $str .= ucfirst(Inflector::singularize($token)); |
|
160 | + } |
|
161 | + |
|
162 | + return $str; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Put the first letter of the string in lower case. |
|
167 | + * Very useful to transform a class name into a variable name. |
|
168 | + * |
|
169 | + * @param $str string |
|
170 | + * |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + private static function toVariableName($str) |
|
174 | + { |
|
175 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns the class name for the DAO factory. |
|
180 | + * |
|
181 | + * @return string |
|
182 | + */ |
|
183 | + public function getDaoFactoryClassName(): string |
|
184 | + { |
|
185 | + return 'DaoFactory'; |
|
186 | + } |
|
187 | 187 | } |
@@ -10,9 +10,9 @@ |
||
10 | 10 | */ |
11 | 11 | interface GeneratorListenerInterface |
12 | 12 | { |
13 | - /** |
|
14 | - * @param ConfigurationInterface $configuration |
|
15 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
16 | - */ |
|
17 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors) : void; |
|
13 | + /** |
|
14 | + * @param ConfigurationInterface $configuration |
|
15 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
16 | + */ |
|
17 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors) : void; |
|
18 | 18 | } |
@@ -18,157 +18,157 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class TDBMDaoGenerator |
20 | 20 | { |
21 | - /** |
|
22 | - * @var Schema |
|
23 | - */ |
|
24 | - private $schema; |
|
25 | - |
|
26 | - /** |
|
27 | - * The root directory of the project. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - private $rootPath; |
|
32 | - |
|
33 | - /** |
|
34 | - * Name of composer file. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $composerFile; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var TDBMSchemaAnalyzer |
|
42 | - */ |
|
43 | - private $tdbmSchemaAnalyzer; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - private $eventDispatcher; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var NamingStrategyInterface |
|
52 | - */ |
|
53 | - private $namingStrategy; |
|
54 | - /** |
|
55 | - * @var ConfigurationInterface |
|
56 | - */ |
|
57 | - private $configuration; |
|
58 | - |
|
59 | - /** |
|
60 | - * Constructor. |
|
61 | - * |
|
62 | - * @param ConfigurationInterface $configuration |
|
63 | - * @param Schema $schema |
|
64 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
65 | - */ |
|
66 | - public function __construct(ConfigurationInterface $configuration, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
67 | - { |
|
68 | - $this->configuration = $configuration; |
|
69 | - $this->schema = $schema; |
|
70 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
71 | - $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
72 | - $this->composerFile = 'composer.json'; |
|
73 | - $this->namingStrategy = $configuration->getNamingStrategy(); |
|
74 | - $this->eventDispatcher = $configuration->getGeneratorEventDispatcher(); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Generates all the daos and beans. |
|
79 | - * |
|
80 | - * @throws TDBMException |
|
81 | - */ |
|
82 | - public function generateAllDaosAndBeans(): void |
|
83 | - { |
|
84 | - $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.$this->composerFile); |
|
85 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
86 | - |
|
87 | - $tableList = $this->schema->getTables(); |
|
88 | - |
|
89 | - // Remove all beans and daos from junction tables |
|
90 | - $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
|
91 | - $junctionTableNames = array_map(function (Table $table) { |
|
92 | - return $table->getName(); |
|
93 | - }, $junctionTables); |
|
94 | - |
|
95 | - $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
96 | - return !in_array($table->getName(), $junctionTableNames); |
|
97 | - }); |
|
98 | - |
|
99 | - $beanDescriptors = []; |
|
100 | - |
|
101 | - foreach ($tableList as $table) { |
|
102 | - $beanDescriptors[] = $this->generateDaoAndBean($table, $classNameMapper); |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - $this->generateFactory($tableList, $classNameMapper); |
|
107 | - |
|
108 | - // Let's call the list of listeners |
|
109 | - $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Generates in one method call the daos and the beans for one table. |
|
114 | - * |
|
115 | - * @param Table $table |
|
116 | - * @param ClassNameMapper $classNameMapper |
|
117 | - * |
|
118 | - * @return BeanDescriptor |
|
119 | - * @throws TDBMException |
|
120 | - */ |
|
121 | - private function generateDaoAndBean(Table $table, ClassNameMapper $classNameMapper) : BeanDescriptor |
|
122 | - { |
|
123 | - // TODO: $storeInUtc is NOT USED. |
|
124 | - $tableName = $table->getName(); |
|
125 | - $daoName = $this->namingStrategy->getDaoClassName($tableName); |
|
126 | - $beanName = $this->namingStrategy->getBeanClassName($tableName); |
|
127 | - $baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
128 | - $baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName); |
|
129 | - |
|
130 | - $beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy); |
|
131 | - $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $classNameMapper); |
|
132 | - $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $classNameMapper); |
|
133 | - return $beanDescriptor; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
138 | - * |
|
139 | - * @param BeanDescriptor $beanDescriptor |
|
140 | - * @param string $className The name of the class |
|
141 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
142 | - * @param Table $table The table |
|
143 | - * @param ClassNameMapper $classNameMapper |
|
144 | - * |
|
145 | - * @throws TDBMException |
|
146 | - */ |
|
147 | - public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, ClassNameMapper $classNameMapper) |
|
148 | - { |
|
149 | - $beannamespace = $this->configuration->getBeanNamespace(); |
|
150 | - $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
151 | - |
|
152 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\Generated\\'.$baseClassName); |
|
153 | - if (empty($possibleBaseFileNames)) { |
|
154 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
155 | - } |
|
156 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
157 | - |
|
158 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
159 | - file_put_contents($possibleBaseFileName, $str); |
|
160 | - @chmod($possibleBaseFileName, 0664); |
|
161 | - |
|
162 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
163 | - if (empty($possibleFileNames)) { |
|
164 | - // @codeCoverageIgnoreStart |
|
165 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
166 | - // @codeCoverageIgnoreEnd |
|
167 | - } |
|
168 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
169 | - if (!file_exists($possibleFileName)) { |
|
170 | - $tableName = $table->getName(); |
|
171 | - $str = "<?php |
|
21 | + /** |
|
22 | + * @var Schema |
|
23 | + */ |
|
24 | + private $schema; |
|
25 | + |
|
26 | + /** |
|
27 | + * The root directory of the project. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + private $rootPath; |
|
32 | + |
|
33 | + /** |
|
34 | + * Name of composer file. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $composerFile; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var TDBMSchemaAnalyzer |
|
42 | + */ |
|
43 | + private $tdbmSchemaAnalyzer; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + private $eventDispatcher; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var NamingStrategyInterface |
|
52 | + */ |
|
53 | + private $namingStrategy; |
|
54 | + /** |
|
55 | + * @var ConfigurationInterface |
|
56 | + */ |
|
57 | + private $configuration; |
|
58 | + |
|
59 | + /** |
|
60 | + * Constructor. |
|
61 | + * |
|
62 | + * @param ConfigurationInterface $configuration |
|
63 | + * @param Schema $schema |
|
64 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
65 | + */ |
|
66 | + public function __construct(ConfigurationInterface $configuration, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
67 | + { |
|
68 | + $this->configuration = $configuration; |
|
69 | + $this->schema = $schema; |
|
70 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
71 | + $this->rootPath = __DIR__.'/../../../../../../../../'; |
|
72 | + $this->composerFile = 'composer.json'; |
|
73 | + $this->namingStrategy = $configuration->getNamingStrategy(); |
|
74 | + $this->eventDispatcher = $configuration->getGeneratorEventDispatcher(); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Generates all the daos and beans. |
|
79 | + * |
|
80 | + * @throws TDBMException |
|
81 | + */ |
|
82 | + public function generateAllDaosAndBeans(): void |
|
83 | + { |
|
84 | + $classNameMapper = ClassNameMapper::createFromComposerFile($this->rootPath.$this->composerFile); |
|
85 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
86 | + |
|
87 | + $tableList = $this->schema->getTables(); |
|
88 | + |
|
89 | + // Remove all beans and daos from junction tables |
|
90 | + $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
|
91 | + $junctionTableNames = array_map(function (Table $table) { |
|
92 | + return $table->getName(); |
|
93 | + }, $junctionTables); |
|
94 | + |
|
95 | + $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
96 | + return !in_array($table->getName(), $junctionTableNames); |
|
97 | + }); |
|
98 | + |
|
99 | + $beanDescriptors = []; |
|
100 | + |
|
101 | + foreach ($tableList as $table) { |
|
102 | + $beanDescriptors[] = $this->generateDaoAndBean($table, $classNameMapper); |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + $this->generateFactory($tableList, $classNameMapper); |
|
107 | + |
|
108 | + // Let's call the list of listeners |
|
109 | + $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Generates in one method call the daos and the beans for one table. |
|
114 | + * |
|
115 | + * @param Table $table |
|
116 | + * @param ClassNameMapper $classNameMapper |
|
117 | + * |
|
118 | + * @return BeanDescriptor |
|
119 | + * @throws TDBMException |
|
120 | + */ |
|
121 | + private function generateDaoAndBean(Table $table, ClassNameMapper $classNameMapper) : BeanDescriptor |
|
122 | + { |
|
123 | + // TODO: $storeInUtc is NOT USED. |
|
124 | + $tableName = $table->getName(); |
|
125 | + $daoName = $this->namingStrategy->getDaoClassName($tableName); |
|
126 | + $beanName = $this->namingStrategy->getBeanClassName($tableName); |
|
127 | + $baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
128 | + $baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName); |
|
129 | + |
|
130 | + $beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy); |
|
131 | + $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table, $classNameMapper); |
|
132 | + $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table, $classNameMapper); |
|
133 | + return $beanDescriptor; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
138 | + * |
|
139 | + * @param BeanDescriptor $beanDescriptor |
|
140 | + * @param string $className The name of the class |
|
141 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
142 | + * @param Table $table The table |
|
143 | + * @param ClassNameMapper $classNameMapper |
|
144 | + * |
|
145 | + * @throws TDBMException |
|
146 | + */ |
|
147 | + public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table, ClassNameMapper $classNameMapper) |
|
148 | + { |
|
149 | + $beannamespace = $this->configuration->getBeanNamespace(); |
|
150 | + $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
151 | + |
|
152 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\Generated\\'.$baseClassName); |
|
153 | + if (empty($possibleBaseFileNames)) { |
|
154 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$baseClassName.'" is not autoloadable.'); |
|
155 | + } |
|
156 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
157 | + |
|
158 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
159 | + file_put_contents($possibleBaseFileName, $str); |
|
160 | + @chmod($possibleBaseFileName, 0664); |
|
161 | + |
|
162 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($beannamespace.'\\'.$className); |
|
163 | + if (empty($possibleFileNames)) { |
|
164 | + // @codeCoverageIgnoreStart |
|
165 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$beannamespace.'\\'.$className.'" is not autoloadable.'); |
|
166 | + // @codeCoverageIgnoreEnd |
|
167 | + } |
|
168 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
169 | + if (!file_exists($possibleFileName)) { |
|
170 | + $tableName = $table->getName(); |
|
171 | + $str = "<?php |
|
172 | 172 | /* |
173 | 173 | * This file has been automatically generated by TDBM. |
174 | 174 | * You can edit this file as it will not be overwritten. |
@@ -185,76 +185,76 @@ discard block |
||
185 | 185 | { |
186 | 186 | } |
187 | 187 | "; |
188 | - $this->ensureDirectoryExist($possibleFileName); |
|
189 | - file_put_contents($possibleFileName, $str); |
|
190 | - @chmod($possibleFileName, 0664); |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Tries to find a @defaultSort annotation in one of the columns. |
|
196 | - * |
|
197 | - * @param Table $table |
|
198 | - * |
|
199 | - * @return array First item: column name, Second item: column order (asc/desc) |
|
200 | - */ |
|
201 | - private function getDefaultSortColumnFromAnnotation(Table $table) |
|
202 | - { |
|
203 | - $defaultSort = null; |
|
204 | - $defaultSortDirection = null; |
|
205 | - foreach ($table->getColumns() as $column) { |
|
206 | - $comments = $column->getComment(); |
|
207 | - $matches = []; |
|
208 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
209 | - $defaultSort = $column->getName(); |
|
210 | - if (count($matches) === 3) { |
|
211 | - $defaultSortDirection = $matches[2]; |
|
212 | - } else { |
|
213 | - $defaultSortDirection = 'ASC'; |
|
214 | - } |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - return [$defaultSort, $defaultSortDirection]; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
223 | - * |
|
224 | - * @param BeanDescriptor $beanDescriptor |
|
225 | - * @param string $className The name of the class |
|
226 | - * @param string $baseClassName |
|
227 | - * @param string $beanClassName |
|
228 | - * @param Table $table |
|
229 | - * @param ClassNameMapper $classNameMapper |
|
230 | - * |
|
231 | - * @throws TDBMException |
|
232 | - */ |
|
233 | - private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table, ClassNameMapper $classNameMapper) |
|
234 | - { |
|
235 | - $daonamespace = $this->configuration->getDaoNamespace(); |
|
236 | - $beannamespace = $this->configuration->getBeanNamespace(); |
|
237 | - $tableName = $table->getName(); |
|
238 | - $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
239 | - |
|
240 | - list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
241 | - |
|
242 | - // FIXME: lowercase tables with _ in the name should work! |
|
243 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
244 | - |
|
245 | - $beanClassWithoutNameSpace = $beanClassName; |
|
246 | - $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
247 | - |
|
248 | - list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
249 | - |
|
250 | - $usedBeans[] = $beanClassName; |
|
251 | - // Let's suppress duplicates in used beans (if any) |
|
252 | - $usedBeans = array_flip(array_flip($usedBeans)); |
|
253 | - $useStatements = array_map(function ($usedBean) { |
|
254 | - return "use $usedBean;\n"; |
|
255 | - }, $usedBeans); |
|
256 | - |
|
257 | - $str = "<?php |
|
188 | + $this->ensureDirectoryExist($possibleFileName); |
|
189 | + file_put_contents($possibleFileName, $str); |
|
190 | + @chmod($possibleFileName, 0664); |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Tries to find a @defaultSort annotation in one of the columns. |
|
196 | + * |
|
197 | + * @param Table $table |
|
198 | + * |
|
199 | + * @return array First item: column name, Second item: column order (asc/desc) |
|
200 | + */ |
|
201 | + private function getDefaultSortColumnFromAnnotation(Table $table) |
|
202 | + { |
|
203 | + $defaultSort = null; |
|
204 | + $defaultSortDirection = null; |
|
205 | + foreach ($table->getColumns() as $column) { |
|
206 | + $comments = $column->getComment(); |
|
207 | + $matches = []; |
|
208 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
209 | + $defaultSort = $column->getName(); |
|
210 | + if (count($matches) === 3) { |
|
211 | + $defaultSortDirection = $matches[2]; |
|
212 | + } else { |
|
213 | + $defaultSortDirection = 'ASC'; |
|
214 | + } |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + return [$defaultSort, $defaultSortDirection]; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
223 | + * |
|
224 | + * @param BeanDescriptor $beanDescriptor |
|
225 | + * @param string $className The name of the class |
|
226 | + * @param string $baseClassName |
|
227 | + * @param string $beanClassName |
|
228 | + * @param Table $table |
|
229 | + * @param ClassNameMapper $classNameMapper |
|
230 | + * |
|
231 | + * @throws TDBMException |
|
232 | + */ |
|
233 | + private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table, ClassNameMapper $classNameMapper) |
|
234 | + { |
|
235 | + $daonamespace = $this->configuration->getDaoNamespace(); |
|
236 | + $beannamespace = $this->configuration->getBeanNamespace(); |
|
237 | + $tableName = $table->getName(); |
|
238 | + $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
239 | + |
|
240 | + list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
241 | + |
|
242 | + // FIXME: lowercase tables with _ in the name should work! |
|
243 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
244 | + |
|
245 | + $beanClassWithoutNameSpace = $beanClassName; |
|
246 | + $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
247 | + |
|
248 | + list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
249 | + |
|
250 | + $usedBeans[] = $beanClassName; |
|
251 | + // Let's suppress duplicates in used beans (if any) |
|
252 | + $usedBeans = array_flip(array_flip($usedBeans)); |
|
253 | + $useStatements = array_map(function ($usedBean) { |
|
254 | + return "use $usedBean;\n"; |
|
255 | + }, $usedBeans); |
|
256 | + |
|
257 | + $str = "<?php |
|
258 | 258 | |
259 | 259 | /* |
260 | 260 | * This file has been automatically generated by TDBM. |
@@ -330,10 +330,10 @@ discard block |
||
330 | 330 | } |
331 | 331 | "; |
332 | 332 | |
333 | - if (count($primaryKeyColumns) === 1) { |
|
334 | - $primaryKeyColumn = $primaryKeyColumns[0]; |
|
335 | - $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType()); |
|
336 | - $str .= " |
|
333 | + if (count($primaryKeyColumns) === 1) { |
|
334 | + $primaryKeyColumn = $primaryKeyColumns[0]; |
|
335 | + $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType()); |
|
336 | + $str .= " |
|
337 | 337 | /** |
338 | 338 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
339 | 339 | * If the primary key does not exist, an exception is thrown. |
@@ -348,8 +348,8 @@ discard block |
||
348 | 348 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
349 | 349 | } |
350 | 350 | "; |
351 | - } |
|
352 | - $str .= " |
|
351 | + } |
|
352 | + $str .= " |
|
353 | 353 | /** |
354 | 354 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
355 | 355 | * |
@@ -449,33 +449,33 @@ discard block |
||
449 | 449 | } |
450 | 450 | "; |
451 | 451 | |
452 | - $str .= $findByDaoCode; |
|
453 | - $str .= '} |
|
452 | + $str .= $findByDaoCode; |
|
453 | + $str .= '} |
|
454 | 454 | '; |
455 | 455 | |
456 | - $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\Generated\\'.$baseClassName); |
|
457 | - if (empty($possibleBaseFileNames)) { |
|
458 | - // @codeCoverageIgnoreStart |
|
459 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daonamespace.'\\Generated\\'.$baseClassName.'" is not autoloadable.'); |
|
460 | - // @codeCoverageIgnoreEnd |
|
461 | - } |
|
462 | - $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
463 | - |
|
464 | - $this->ensureDirectoryExist($possibleBaseFileName); |
|
465 | - file_put_contents($possibleBaseFileName, $str); |
|
466 | - @chmod($possibleBaseFileName, 0664); |
|
467 | - |
|
468 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
469 | - if (empty($possibleFileNames)) { |
|
470 | - // @codeCoverageIgnoreStart |
|
471 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daonamespace.'\\'.$className.'" is not autoloadable.'); |
|
472 | - // @codeCoverageIgnoreEnd |
|
473 | - } |
|
474 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
475 | - |
|
476 | - // Now, let's generate the "editable" class |
|
477 | - if (!file_exists($possibleFileName)) { |
|
478 | - $str = "<?php |
|
456 | + $possibleBaseFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\Generated\\'.$baseClassName); |
|
457 | + if (empty($possibleBaseFileNames)) { |
|
458 | + // @codeCoverageIgnoreStart |
|
459 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daonamespace.'\\Generated\\'.$baseClassName.'" is not autoloadable.'); |
|
460 | + // @codeCoverageIgnoreEnd |
|
461 | + } |
|
462 | + $possibleBaseFileName = $this->rootPath.$possibleBaseFileNames[0]; |
|
463 | + |
|
464 | + $this->ensureDirectoryExist($possibleBaseFileName); |
|
465 | + file_put_contents($possibleBaseFileName, $str); |
|
466 | + @chmod($possibleBaseFileName, 0664); |
|
467 | + |
|
468 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daonamespace.'\\'.$className); |
|
469 | + if (empty($possibleFileNames)) { |
|
470 | + // @codeCoverageIgnoreStart |
|
471 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daonamespace.'\\'.$className.'" is not autoloadable.'); |
|
472 | + // @codeCoverageIgnoreEnd |
|
473 | + } |
|
474 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
475 | + |
|
476 | + // Now, let's generate the "editable" class |
|
477 | + if (!file_exists($possibleFileName)) { |
|
478 | + $str = "<?php |
|
479 | 479 | |
480 | 480 | /* |
481 | 481 | * This file has been automatically generated by TDBM. |
@@ -493,27 +493,27 @@ discard block |
||
493 | 493 | { |
494 | 494 | } |
495 | 495 | "; |
496 | - $this->ensureDirectoryExist($possibleFileName); |
|
497 | - file_put_contents($possibleFileName, $str); |
|
498 | - @chmod($possibleFileName, 0664); |
|
499 | - } |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Generates the factory bean. |
|
504 | - * |
|
505 | - * @param Table[] $tableList |
|
506 | - * @param ClassNameMapper $classNameMapper |
|
507 | - * @throws TDBMException |
|
508 | - */ |
|
509 | - private function generateFactory(array $tableList, ClassNameMapper $classNameMapper) : void |
|
510 | - { |
|
511 | - $daoNamespace = $this->configuration->getDaoNamespace(); |
|
512 | - $daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName(); |
|
513 | - |
|
514 | - // For each table, let's write a property. |
|
515 | - |
|
516 | - $str = "<?php |
|
496 | + $this->ensureDirectoryExist($possibleFileName); |
|
497 | + file_put_contents($possibleFileName, $str); |
|
498 | + @chmod($possibleFileName, 0664); |
|
499 | + } |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Generates the factory bean. |
|
504 | + * |
|
505 | + * @param Table[] $tableList |
|
506 | + * @param ClassNameMapper $classNameMapper |
|
507 | + * @throws TDBMException |
|
508 | + */ |
|
509 | + private function generateFactory(array $tableList, ClassNameMapper $classNameMapper) : void |
|
510 | + { |
|
511 | + $daoNamespace = $this->configuration->getDaoNamespace(); |
|
512 | + $daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName(); |
|
513 | + |
|
514 | + // For each table, let's write a property. |
|
515 | + |
|
516 | + $str = "<?php |
|
517 | 517 | |
518 | 518 | /* |
519 | 519 | * This file has been automatically generated by TDBM. |
@@ -523,13 +523,13 @@ discard block |
||
523 | 523 | namespace {$daoNamespace}\\Generated; |
524 | 524 | |
525 | 525 | "; |
526 | - foreach ($tableList as $table) { |
|
527 | - $tableName = $table->getName(); |
|
528 | - $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
529 | - $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
530 | - } |
|
526 | + foreach ($tableList as $table) { |
|
527 | + $tableName = $table->getName(); |
|
528 | + $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
529 | + $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
530 | + } |
|
531 | 531 | |
532 | - $str .= " |
|
532 | + $str .= " |
|
533 | 533 | /** |
534 | 534 | * The $daoFactoryClassName provides an easy access to all DAOs generated by TDBM. |
535 | 535 | * |
@@ -538,12 +538,12 @@ discard block |
||
538 | 538 | { |
539 | 539 | "; |
540 | 540 | |
541 | - foreach ($tableList as $table) { |
|
542 | - $tableName = $table->getName(); |
|
543 | - $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
544 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
541 | + foreach ($tableList as $table) { |
|
542 | + $tableName = $table->getName(); |
|
543 | + $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
544 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
545 | 545 | |
546 | - $str .= ' /** |
|
546 | + $str .= ' /** |
|
547 | 547 | * @var '.$daoClassName.' |
548 | 548 | */ |
549 | 549 | private $'.$daoInstanceName.'; |
@@ -567,139 +567,139 @@ discard block |
||
567 | 567 | { |
568 | 568 | $this->'.$daoInstanceName.' = $'.$daoInstanceName.'; |
569 | 569 | }'; |
570 | - } |
|
570 | + } |
|
571 | 571 | |
572 | - $str .= ' |
|
572 | + $str .= ' |
|
573 | 573 | } |
574 | 574 | '; |
575 | 575 | |
576 | - $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
577 | - if (empty($possibleFileNames)) { |
|
578 | - throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
579 | - } |
|
580 | - $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
581 | - |
|
582 | - $this->ensureDirectoryExist($possibleFileName); |
|
583 | - file_put_contents($possibleFileName, $str); |
|
584 | - @chmod($possibleFileName, 0664); |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
589 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
590 | - * |
|
591 | - * @param $str string |
|
592 | - * |
|
593 | - * @return string |
|
594 | - */ |
|
595 | - public static function toCamelCase($str) |
|
596 | - { |
|
597 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
598 | - while (true) { |
|
599 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
600 | - break; |
|
601 | - } |
|
602 | - |
|
603 | - $pos = strpos($str, '_'); |
|
604 | - if ($pos === false) { |
|
605 | - $pos = strpos($str, ' '); |
|
606 | - } |
|
607 | - $before = substr($str, 0, $pos); |
|
608 | - $after = substr($str, $pos + 1); |
|
609 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
610 | - } |
|
611 | - |
|
612 | - return $str; |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Tries to put string to the singular form (if it is plural). |
|
617 | - * We assume the table names are in english. |
|
618 | - * |
|
619 | - * @param $str string |
|
620 | - * |
|
621 | - * @return string |
|
622 | - */ |
|
623 | - public static function toSingular($str) |
|
624 | - { |
|
625 | - return Inflector::singularize($str); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Put the first letter of the string in lower case. |
|
630 | - * Very useful to transform a class name into a variable name. |
|
631 | - * |
|
632 | - * @param $str string |
|
633 | - * |
|
634 | - * @return string |
|
635 | - */ |
|
636 | - public static function toVariableName($str) |
|
637 | - { |
|
638 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * Ensures the file passed in parameter can be written in its directory. |
|
643 | - * |
|
644 | - * @param string $fileName |
|
645 | - * |
|
646 | - * @throws TDBMException |
|
647 | - */ |
|
648 | - private function ensureDirectoryExist($fileName) |
|
649 | - { |
|
650 | - $dirName = dirname($fileName); |
|
651 | - if (!file_exists($dirName)) { |
|
652 | - $old = umask(0); |
|
653 | - $result = mkdir($dirName, 0775, true); |
|
654 | - umask($old); |
|
655 | - if ($result === false) { |
|
656 | - throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
657 | - } |
|
658 | - } |
|
659 | - } |
|
660 | - |
|
661 | - /** |
|
662 | - * Absolute path to composer json file. |
|
663 | - * |
|
664 | - * @param string $composerFile |
|
665 | - */ |
|
666 | - public function setComposerFile($composerFile) |
|
667 | - { |
|
668 | - $this->rootPath = dirname($composerFile).'/'; |
|
669 | - $this->composerFile = basename($composerFile); |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
674 | - * |
|
675 | - * @param Type $type The DBAL type |
|
676 | - * |
|
677 | - * @return string The PHP type |
|
678 | - */ |
|
679 | - public static function dbalTypeToPhpType(Type $type) |
|
680 | - { |
|
681 | - $map = [ |
|
682 | - Type::TARRAY => 'array', |
|
683 | - Type::SIMPLE_ARRAY => 'array', |
|
684 | - Type::JSON_ARRAY => 'array', |
|
685 | - Type::BIGINT => 'string', |
|
686 | - Type::BOOLEAN => 'bool', |
|
687 | - Type::DATETIME => '\DateTimeInterface', |
|
688 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
689 | - Type::DATE => '\DateTimeInterface', |
|
690 | - Type::TIME => '\DateTimeInterface', |
|
691 | - Type::DECIMAL => 'float', |
|
692 | - Type::INTEGER => 'int', |
|
693 | - Type::OBJECT => 'string', |
|
694 | - Type::SMALLINT => 'int', |
|
695 | - Type::STRING => 'string', |
|
696 | - Type::TEXT => 'string', |
|
697 | - Type::BINARY => 'string', |
|
698 | - Type::BLOB => 'string', |
|
699 | - Type::FLOAT => 'float', |
|
700 | - Type::GUID => 'string', |
|
701 | - ]; |
|
702 | - |
|
703 | - return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
704 | - } |
|
576 | + $possibleFileNames = $classNameMapper->getPossibleFileNames($daoNamespace.'\\Generated\\'.$daoFactoryClassName); |
|
577 | + if (empty($possibleFileNames)) { |
|
578 | + throw new TDBMException('Sorry, autoload namespace issue. The class "'.$daoNamespace.'\\'.$daoFactoryClassName.'" is not autoloadable.'); |
|
579 | + } |
|
580 | + $possibleFileName = $this->rootPath.$possibleFileNames[0]; |
|
581 | + |
|
582 | + $this->ensureDirectoryExist($possibleFileName); |
|
583 | + file_put_contents($possibleFileName, $str); |
|
584 | + @chmod($possibleFileName, 0664); |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
589 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
590 | + * |
|
591 | + * @param $str string |
|
592 | + * |
|
593 | + * @return string |
|
594 | + */ |
|
595 | + public static function toCamelCase($str) |
|
596 | + { |
|
597 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
598 | + while (true) { |
|
599 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
600 | + break; |
|
601 | + } |
|
602 | + |
|
603 | + $pos = strpos($str, '_'); |
|
604 | + if ($pos === false) { |
|
605 | + $pos = strpos($str, ' '); |
|
606 | + } |
|
607 | + $before = substr($str, 0, $pos); |
|
608 | + $after = substr($str, $pos + 1); |
|
609 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
610 | + } |
|
611 | + |
|
612 | + return $str; |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Tries to put string to the singular form (if it is plural). |
|
617 | + * We assume the table names are in english. |
|
618 | + * |
|
619 | + * @param $str string |
|
620 | + * |
|
621 | + * @return string |
|
622 | + */ |
|
623 | + public static function toSingular($str) |
|
624 | + { |
|
625 | + return Inflector::singularize($str); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Put the first letter of the string in lower case. |
|
630 | + * Very useful to transform a class name into a variable name. |
|
631 | + * |
|
632 | + * @param $str string |
|
633 | + * |
|
634 | + * @return string |
|
635 | + */ |
|
636 | + public static function toVariableName($str) |
|
637 | + { |
|
638 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * Ensures the file passed in parameter can be written in its directory. |
|
643 | + * |
|
644 | + * @param string $fileName |
|
645 | + * |
|
646 | + * @throws TDBMException |
|
647 | + */ |
|
648 | + private function ensureDirectoryExist($fileName) |
|
649 | + { |
|
650 | + $dirName = dirname($fileName); |
|
651 | + if (!file_exists($dirName)) { |
|
652 | + $old = umask(0); |
|
653 | + $result = mkdir($dirName, 0775, true); |
|
654 | + umask($old); |
|
655 | + if ($result === false) { |
|
656 | + throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
657 | + } |
|
658 | + } |
|
659 | + } |
|
660 | + |
|
661 | + /** |
|
662 | + * Absolute path to composer json file. |
|
663 | + * |
|
664 | + * @param string $composerFile |
|
665 | + */ |
|
666 | + public function setComposerFile($composerFile) |
|
667 | + { |
|
668 | + $this->rootPath = dirname($composerFile).'/'; |
|
669 | + $this->composerFile = basename($composerFile); |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
674 | + * |
|
675 | + * @param Type $type The DBAL type |
|
676 | + * |
|
677 | + * @return string The PHP type |
|
678 | + */ |
|
679 | + public static function dbalTypeToPhpType(Type $type) |
|
680 | + { |
|
681 | + $map = [ |
|
682 | + Type::TARRAY => 'array', |
|
683 | + Type::SIMPLE_ARRAY => 'array', |
|
684 | + Type::JSON_ARRAY => 'array', |
|
685 | + Type::BIGINT => 'string', |
|
686 | + Type::BOOLEAN => 'bool', |
|
687 | + Type::DATETIME => '\DateTimeInterface', |
|
688 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
689 | + Type::DATE => '\DateTimeInterface', |
|
690 | + Type::TIME => '\DateTimeInterface', |
|
691 | + Type::DECIMAL => 'float', |
|
692 | + Type::INTEGER => 'int', |
|
693 | + Type::OBJECT => 'string', |
|
694 | + Type::SMALLINT => 'int', |
|
695 | + Type::STRING => 'string', |
|
696 | + Type::TEXT => 'string', |
|
697 | + Type::BINARY => 'string', |
|
698 | + Type::BLOB => 'string', |
|
699 | + Type::FLOAT => 'float', |
|
700 | + Type::GUID => 'string', |
|
701 | + ]; |
|
702 | + |
|
703 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
704 | + } |
|
705 | 705 | } |
@@ -17,21 +17,21 @@ |
||
17 | 17 | */ |
18 | 18 | class MoufConfiguration extends Configuration |
19 | 19 | { |
20 | - private $daoFactoryInstanceName = 'daoFactory'; |
|
20 | + private $daoFactoryInstanceName = 'daoFactory'; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function getDaoFactoryInstanceName() : string |
|
26 | - { |
|
27 | - return $this->daoFactoryInstanceName; |
|
28 | - } |
|
22 | + /** |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function getDaoFactoryInstanceName() : string |
|
26 | + { |
|
27 | + return $this->daoFactoryInstanceName; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $daoFactoryInstanceName |
|
32 | - */ |
|
33 | - public function setDaoFactoryInstanceName(string $daoFactoryInstanceName) |
|
34 | - { |
|
35 | - $this->daoFactoryInstanceName = $daoFactoryInstanceName; |
|
36 | - } |
|
30 | + /** |
|
31 | + * @param string $daoFactoryInstanceName |
|
32 | + */ |
|
33 | + public function setDaoFactoryInstanceName(string $daoFactoryInstanceName) |
|
34 | + { |
|
35 | + $this->daoFactoryInstanceName = $daoFactoryInstanceName; |
|
36 | + } |
|
37 | 37 | } |
@@ -33,42 +33,42 @@ |
||
33 | 33 | */ |
34 | 34 | class TDBMObject extends AbstractTDBMObject |
35 | 35 | { |
36 | - public function getProperty($var, $tableName = null) |
|
37 | - { |
|
38 | - return $this->get($var, $tableName); |
|
39 | - } |
|
36 | + public function getProperty($var, $tableName = null) |
|
37 | + { |
|
38 | + return $this->get($var, $tableName); |
|
39 | + } |
|
40 | 40 | |
41 | - public function setProperty($var, $value, $tableName = null) |
|
42 | - { |
|
43 | - $this->set($var, $value, $tableName); |
|
44 | - } |
|
41 | + public function setProperty($var, $value, $tableName = null) |
|
42 | + { |
|
43 | + $this->set($var, $value, $tableName); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Specify data which should be serialized to JSON. |
|
48 | - * |
|
49 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | - * |
|
51 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | - * which is a value of any type other than a resource |
|
53 | - * |
|
54 | - * @since 5.4.0 |
|
55 | - */ |
|
56 | - public function jsonSerialize() |
|
57 | - { |
|
58 | - throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | - } |
|
46 | + /** |
|
47 | + * Specify data which should be serialized to JSON. |
|
48 | + * |
|
49 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
50 | + * |
|
51 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
52 | + * which is a value of any type other than a resource |
|
53 | + * |
|
54 | + * @since 5.4.0 |
|
55 | + */ |
|
56 | + public function jsonSerialize() |
|
57 | + { |
|
58 | + throw new TDBMException('Json serialization is only implemented for generated beans.'); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | - * |
|
64 | - * @return string[] |
|
65 | - */ |
|
66 | - protected function getUsedTables() : array |
|
67 | - { |
|
68 | - $tableNames = array_keys($this->dbRows); |
|
69 | - $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | - $tableNames = array_reverse($tableNames); |
|
61 | + /** |
|
62 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
63 | + * |
|
64 | + * @return string[] |
|
65 | + */ |
|
66 | + protected function getUsedTables() : array |
|
67 | + { |
|
68 | + $tableNames = array_keys($this->dbRows); |
|
69 | + $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames); |
|
70 | + $tableNames = array_reverse($tableNames); |
|
71 | 71 | |
72 | - return $tableNames; |
|
73 | - } |
|
72 | + return $tableNames; |
|
73 | + } |
|
74 | 74 | } |
@@ -31,615 +31,615 @@ |
||
31 | 31 | */ |
32 | 32 | abstract class AbstractTDBMObject implements JsonSerializable |
33 | 33 | { |
34 | - /** |
|
35 | - * The service this object is bound to. |
|
36 | - * |
|
37 | - * @var TDBMService |
|
38 | - */ |
|
39 | - protected $tdbmService; |
|
40 | - |
|
41 | - /** |
|
42 | - * An array of DbRow, indexed by table name. |
|
43 | - * |
|
44 | - * @var DbRow[] |
|
45 | - */ |
|
46 | - protected $dbRows = []; |
|
47 | - |
|
48 | - /** |
|
49 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
50 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
51 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
52 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - private $status; |
|
57 | - |
|
58 | - /** |
|
59 | - * Array storing beans related via many to many relationships (pivot tables). |
|
60 | - * |
|
61 | - * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
62 | - */ |
|
63 | - private $relationships = []; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
67 | - */ |
|
68 | - private $loadedRelationships = []; |
|
69 | - |
|
70 | - /** |
|
71 | - * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
72 | - * |
|
73 | - * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
74 | - */ |
|
75 | - private $manyToOneRelationships = []; |
|
76 | - |
|
77 | - /** |
|
78 | - * Used with $primaryKeys when we want to retrieve an existing object |
|
79 | - * and $primaryKeys=[] if we want a new object. |
|
80 | - * |
|
81 | - * @param string $tableName |
|
82 | - * @param array $primaryKeys |
|
83 | - * @param TDBMService $tdbmService |
|
84 | - * |
|
85 | - * @throws TDBMException |
|
86 | - * @throws TDBMInvalidOperationException |
|
87 | - */ |
|
88 | - public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null) |
|
89 | - { |
|
90 | - // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
91 | - if (!empty($tableName)) { |
|
92 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
93 | - } |
|
94 | - |
|
95 | - if ($tdbmService === null) { |
|
96 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
97 | - } else { |
|
98 | - $this->_attach($tdbmService); |
|
99 | - if (!empty($primaryKeys)) { |
|
100 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
101 | - } else { |
|
102 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
103 | - } |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Alternative constructor called when data is fetched from database via a SELECT. |
|
109 | - * |
|
110 | - * @param array $beanData array<table, array<column, value>> |
|
111 | - * @param TDBMService $tdbmService |
|
112 | - */ |
|
113 | - public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
114 | - { |
|
115 | - $this->tdbmService = $tdbmService; |
|
116 | - |
|
117 | - foreach ($beanData as $table => $columns) { |
|
118 | - $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
119 | - } |
|
120 | - |
|
121 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Alternative constructor called when bean is lazily loaded. |
|
126 | - * |
|
127 | - * @param string $tableName |
|
128 | - * @param array $primaryKeys |
|
129 | - * @param TDBMService $tdbmService |
|
130 | - */ |
|
131 | - public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
132 | - { |
|
133 | - $this->tdbmService = $tdbmService; |
|
134 | - |
|
135 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
136 | - |
|
137 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
138 | - } |
|
139 | - |
|
140 | - public function _attach(TDBMService $tdbmService) |
|
141 | - { |
|
142 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
143 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
144 | - } |
|
145 | - $this->tdbmService = $tdbmService; |
|
146 | - |
|
147 | - // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
148 | - $tableNames = $this->getUsedTables(); |
|
149 | - |
|
150 | - $newDbRows = []; |
|
151 | - |
|
152 | - foreach ($tableNames as $table) { |
|
153 | - if (!isset($this->dbRows[$table])) { |
|
154 | - $this->registerTable($table); |
|
155 | - } |
|
156 | - $newDbRows[$table] = $this->dbRows[$table]; |
|
157 | - } |
|
158 | - $this->dbRows = $newDbRows; |
|
159 | - |
|
160 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
161 | - foreach ($this->dbRows as $dbRow) { |
|
162 | - $dbRow->_attach($tdbmService); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Sets the state of the TDBM Object |
|
168 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
169 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
170 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
171 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
172 | - * |
|
173 | - * @param string $state |
|
174 | - */ |
|
175 | - public function _setStatus($state) |
|
176 | - { |
|
177 | - $this->status = $state; |
|
178 | - |
|
179 | - // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
180 | - foreach ($this->dbRows as $dbRow) { |
|
181 | - $dbRow->_setStatus($state); |
|
182 | - } |
|
183 | - |
|
184 | - if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
185 | - $this->onDelete(); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
191 | - * or throws an error. |
|
192 | - * |
|
193 | - * @param string $tableName |
|
194 | - * |
|
195 | - * @return string |
|
196 | - */ |
|
197 | - private function checkTableName($tableName = null) |
|
198 | - { |
|
199 | - if ($tableName === null) { |
|
200 | - if (count($this->dbRows) > 1) { |
|
201 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
202 | - } elseif (count($this->dbRows) === 1) { |
|
203 | - $tableName = array_keys($this->dbRows)[0]; |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - return $tableName; |
|
208 | - } |
|
209 | - |
|
210 | - protected function get($var, $tableName = null) |
|
211 | - { |
|
212 | - $tableName = $this->checkTableName($tableName); |
|
213 | - |
|
214 | - if (!isset($this->dbRows[$tableName])) { |
|
215 | - return; |
|
216 | - } |
|
217 | - |
|
218 | - return $this->dbRows[$tableName]->get($var); |
|
219 | - } |
|
220 | - |
|
221 | - protected function set($var, $value, $tableName = null) |
|
222 | - { |
|
223 | - if ($tableName === null) { |
|
224 | - if (count($this->dbRows) > 1) { |
|
225 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
226 | - } elseif (count($this->dbRows) === 1) { |
|
227 | - $tableName = array_keys($this->dbRows)[0]; |
|
228 | - } else { |
|
229 | - throw new TDBMException('Please specify a table for this object.'); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - if (!isset($this->dbRows[$tableName])) { |
|
234 | - $this->registerTable($tableName); |
|
235 | - } |
|
236 | - |
|
237 | - $this->dbRows[$tableName]->set($var, $value); |
|
238 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
239 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
240 | - } |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @param string $foreignKeyName |
|
245 | - * @param AbstractTDBMObject $bean |
|
246 | - */ |
|
247 | - protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
248 | - { |
|
249 | - if ($tableName === null) { |
|
250 | - if (count($this->dbRows) > 1) { |
|
251 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
252 | - } elseif (count($this->dbRows) === 1) { |
|
253 | - $tableName = array_keys($this->dbRows)[0]; |
|
254 | - } else { |
|
255 | - throw new TDBMException('Please specify a table for this object.'); |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - if (!isset($this->dbRows[$tableName])) { |
|
260 | - $this->registerTable($tableName); |
|
261 | - } |
|
262 | - |
|
263 | - $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
264 | - if ($oldLinkedBean !== null) { |
|
265 | - $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
266 | - } |
|
267 | - |
|
268 | - $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
269 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
270 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
271 | - } |
|
272 | - |
|
273 | - if ($bean !== null) { |
|
274 | - $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - protected function getRef($foreignKeyName, $tableName = null) |
|
284 | - { |
|
285 | - $tableName = $this->checkTableName($tableName); |
|
286 | - |
|
287 | - if (!isset($this->dbRows[$tableName])) { |
|
288 | - return; |
|
289 | - } |
|
290 | - |
|
291 | - return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Adds a many to many relationship to this bean. |
|
296 | - * |
|
297 | - * @param string $pivotTableName |
|
298 | - * @param AbstractTDBMObject $remoteBean |
|
299 | - */ |
|
300 | - protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
301 | - { |
|
302 | - $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Returns true if there is a relationship to this bean. |
|
307 | - * |
|
308 | - * @param string $pivotTableName |
|
309 | - * @param AbstractTDBMObject $remoteBean |
|
310 | - * |
|
311 | - * @return bool |
|
312 | - */ |
|
313 | - protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
314 | - { |
|
315 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
316 | - |
|
317 | - if ($storage->contains($remoteBean)) { |
|
318 | - if ($storage[$remoteBean]['status'] !== 'delete') { |
|
319 | - return true; |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - return false; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Internal TDBM method. Removes a many to many relationship from this bean. |
|
328 | - * |
|
329 | - * @param string $pivotTableName |
|
330 | - * @param AbstractTDBMObject $remoteBean |
|
331 | - */ |
|
332 | - public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
333 | - { |
|
334 | - if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
335 | - unset($this->relationships[$pivotTableName][$remoteBean]); |
|
336 | - unset($remoteBean->relationships[$pivotTableName][$this]); |
|
337 | - } else { |
|
338 | - $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Sets many to many relationships for this bean. |
|
344 | - * Adds new relationships and removes unused ones. |
|
345 | - * |
|
346 | - * @param $pivotTableName |
|
347 | - * @param array $remoteBeans |
|
348 | - */ |
|
349 | - protected function setRelationships($pivotTableName, array $remoteBeans) |
|
350 | - { |
|
351 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
352 | - |
|
353 | - foreach ($storage as $oldRemoteBean) { |
|
354 | - if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
355 | - // $oldRemoteBean must be removed |
|
356 | - $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
357 | - } |
|
358 | - } |
|
359 | - |
|
360 | - foreach ($remoteBeans as $remoteBean) { |
|
361 | - if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
362 | - // $remoteBean must be added |
|
363 | - $this->addRelationship($pivotTableName, $remoteBean); |
|
364 | - } |
|
365 | - } |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Returns the list of objects linked to this bean via $pivotTableName. |
|
370 | - * |
|
371 | - * @param $pivotTableName |
|
372 | - * |
|
373 | - * @return \SplObjectStorage |
|
374 | - */ |
|
375 | - private function retrieveRelationshipsStorage($pivotTableName) |
|
376 | - { |
|
377 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
378 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
379 | - return $storage; |
|
380 | - } |
|
381 | - |
|
382 | - $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
383 | - $this->loadedRelationships[$pivotTableName] = true; |
|
384 | - |
|
385 | - foreach ($beans as $bean) { |
|
386 | - if (isset($storage[$bean])) { |
|
387 | - $oldStatus = $storage[$bean]['status']; |
|
388 | - if ($oldStatus === 'delete') { |
|
389 | - // Keep deleted things deleted |
|
390 | - continue; |
|
391 | - } |
|
392 | - } |
|
393 | - $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
394 | - } |
|
395 | - |
|
396 | - return $storage; |
|
397 | - } |
|
398 | - |
|
399 | - /** |
|
400 | - * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
401 | - * |
|
402 | - * @param $pivotTableName |
|
403 | - * |
|
404 | - * @return AbstractTDBMObject[] |
|
405 | - */ |
|
406 | - public function _getRelationships($pivotTableName) |
|
407 | - { |
|
408 | - return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
409 | - } |
|
410 | - |
|
411 | - private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
412 | - { |
|
413 | - $beans = []; |
|
414 | - foreach ($storage as $bean) { |
|
415 | - $statusArr = $storage[$bean]; |
|
416 | - if ($statusArr['status'] !== 'delete') { |
|
417 | - $beans[] = $bean; |
|
418 | - } |
|
419 | - } |
|
420 | - |
|
421 | - return $beans; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Declares a relationship between. |
|
426 | - * |
|
427 | - * @param string $pivotTableName |
|
428 | - * @param AbstractTDBMObject $remoteBean |
|
429 | - * @param string $status |
|
430 | - */ |
|
431 | - private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
432 | - { |
|
433 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
434 | - $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
435 | - if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
436 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
437 | - } |
|
438 | - |
|
439 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
440 | - $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
445 | - * |
|
446 | - * @param string $pivotTableName |
|
447 | - * |
|
448 | - * @return \SplObjectStorage |
|
449 | - */ |
|
450 | - private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
451 | - { |
|
452 | - return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
457 | - * |
|
458 | - * @param string $tableName |
|
459 | - * @param string $foreignKeyName |
|
460 | - * |
|
461 | - * @return AlterableResultIterator |
|
462 | - */ |
|
463 | - private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
464 | - { |
|
465 | - $key = $tableName.'___'.$foreignKeyName; |
|
466 | - |
|
467 | - return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * Declares a relationship between this bean and the bean pointing to it. |
|
472 | - * |
|
473 | - * @param string $tableName |
|
474 | - * @param string $foreignKeyName |
|
475 | - * @param AbstractTDBMObject $remoteBean |
|
476 | - */ |
|
477 | - private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
478 | - { |
|
479 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
480 | - $alterableResultIterator->add($remoteBean); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Declares a relationship between this bean and the bean pointing to it. |
|
485 | - * |
|
486 | - * @param string $tableName |
|
487 | - * @param string $foreignKeyName |
|
488 | - * @param AbstractTDBMObject $remoteBean |
|
489 | - */ |
|
490 | - private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
491 | - { |
|
492 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
493 | - $alterableResultIterator->remove($remoteBean); |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Returns the list of objects linked to this bean via a given foreign key. |
|
498 | - * |
|
499 | - * @param string $tableName |
|
500 | - * @param string $foreignKeyName |
|
501 | - * @param string $searchTableName |
|
502 | - * @param array $searchFilter |
|
503 | - * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
504 | - * |
|
505 | - * @return AlterableResultIterator |
|
506 | - */ |
|
507 | - protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
508 | - { |
|
509 | - $key = $tableName.'___'.$foreignKeyName; |
|
510 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
511 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
512 | - return $alterableResultIterator; |
|
513 | - } |
|
514 | - |
|
515 | - $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
516 | - |
|
517 | - $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
518 | - |
|
519 | - return $alterableResultIterator; |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Reverts any changes made to the object and resumes it to its DB state. |
|
524 | - * This can only be called on objects that come from database and that have not been deleted. |
|
525 | - * Otherwise, this will throw an exception. |
|
526 | - * |
|
527 | - * @throws TDBMException |
|
528 | - */ |
|
529 | - public function discardChanges() |
|
530 | - { |
|
531 | - if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
532 | - throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
533 | - } |
|
534 | - |
|
535 | - if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
536 | - throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
537 | - } |
|
538 | - |
|
539 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * Method used internally by TDBM. You should not use it directly. |
|
544 | - * This method returns the status of the TDBMObject. |
|
545 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
546 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
547 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
548 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
549 | - * |
|
550 | - * @return string |
|
551 | - */ |
|
552 | - public function _getStatus() |
|
553 | - { |
|
554 | - return $this->status; |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Override the native php clone function for TDBMObjects. |
|
559 | - */ |
|
560 | - public function __clone() |
|
561 | - { |
|
562 | - // Let's clone the many to many relationships |
|
563 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
564 | - $pivotTableList = array_keys($this->relationships); |
|
565 | - } else { |
|
566 | - $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
567 | - } |
|
568 | - |
|
569 | - foreach ($pivotTableList as $pivotTable) { |
|
570 | - $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
571 | - |
|
572 | - // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
573 | - /*foreach ($storage as $remoteBean) { |
|
34 | + /** |
|
35 | + * The service this object is bound to. |
|
36 | + * |
|
37 | + * @var TDBMService |
|
38 | + */ |
|
39 | + protected $tdbmService; |
|
40 | + |
|
41 | + /** |
|
42 | + * An array of DbRow, indexed by table name. |
|
43 | + * |
|
44 | + * @var DbRow[] |
|
45 | + */ |
|
46 | + protected $dbRows = []; |
|
47 | + |
|
48 | + /** |
|
49 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
50 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
51 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
52 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + private $status; |
|
57 | + |
|
58 | + /** |
|
59 | + * Array storing beans related via many to many relationships (pivot tables). |
|
60 | + * |
|
61 | + * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
62 | + */ |
|
63 | + private $relationships = []; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
67 | + */ |
|
68 | + private $loadedRelationships = []; |
|
69 | + |
|
70 | + /** |
|
71 | + * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
72 | + * |
|
73 | + * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
74 | + */ |
|
75 | + private $manyToOneRelationships = []; |
|
76 | + |
|
77 | + /** |
|
78 | + * Used with $primaryKeys when we want to retrieve an existing object |
|
79 | + * and $primaryKeys=[] if we want a new object. |
|
80 | + * |
|
81 | + * @param string $tableName |
|
82 | + * @param array $primaryKeys |
|
83 | + * @param TDBMService $tdbmService |
|
84 | + * |
|
85 | + * @throws TDBMException |
|
86 | + * @throws TDBMInvalidOperationException |
|
87 | + */ |
|
88 | + public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null) |
|
89 | + { |
|
90 | + // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
91 | + if (!empty($tableName)) { |
|
92 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
93 | + } |
|
94 | + |
|
95 | + if ($tdbmService === null) { |
|
96 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
97 | + } else { |
|
98 | + $this->_attach($tdbmService); |
|
99 | + if (!empty($primaryKeys)) { |
|
100 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
101 | + } else { |
|
102 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
103 | + } |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Alternative constructor called when data is fetched from database via a SELECT. |
|
109 | + * |
|
110 | + * @param array $beanData array<table, array<column, value>> |
|
111 | + * @param TDBMService $tdbmService |
|
112 | + */ |
|
113 | + public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
114 | + { |
|
115 | + $this->tdbmService = $tdbmService; |
|
116 | + |
|
117 | + foreach ($beanData as $table => $columns) { |
|
118 | + $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
119 | + } |
|
120 | + |
|
121 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Alternative constructor called when bean is lazily loaded. |
|
126 | + * |
|
127 | + * @param string $tableName |
|
128 | + * @param array $primaryKeys |
|
129 | + * @param TDBMService $tdbmService |
|
130 | + */ |
|
131 | + public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
132 | + { |
|
133 | + $this->tdbmService = $tdbmService; |
|
134 | + |
|
135 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
136 | + |
|
137 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
138 | + } |
|
139 | + |
|
140 | + public function _attach(TDBMService $tdbmService) |
|
141 | + { |
|
142 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
143 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
144 | + } |
|
145 | + $this->tdbmService = $tdbmService; |
|
146 | + |
|
147 | + // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
148 | + $tableNames = $this->getUsedTables(); |
|
149 | + |
|
150 | + $newDbRows = []; |
|
151 | + |
|
152 | + foreach ($tableNames as $table) { |
|
153 | + if (!isset($this->dbRows[$table])) { |
|
154 | + $this->registerTable($table); |
|
155 | + } |
|
156 | + $newDbRows[$table] = $this->dbRows[$table]; |
|
157 | + } |
|
158 | + $this->dbRows = $newDbRows; |
|
159 | + |
|
160 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
161 | + foreach ($this->dbRows as $dbRow) { |
|
162 | + $dbRow->_attach($tdbmService); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Sets the state of the TDBM Object |
|
168 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
169 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
170 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
171 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
172 | + * |
|
173 | + * @param string $state |
|
174 | + */ |
|
175 | + public function _setStatus($state) |
|
176 | + { |
|
177 | + $this->status = $state; |
|
178 | + |
|
179 | + // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
180 | + foreach ($this->dbRows as $dbRow) { |
|
181 | + $dbRow->_setStatus($state); |
|
182 | + } |
|
183 | + |
|
184 | + if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
185 | + $this->onDelete(); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
191 | + * or throws an error. |
|
192 | + * |
|
193 | + * @param string $tableName |
|
194 | + * |
|
195 | + * @return string |
|
196 | + */ |
|
197 | + private function checkTableName($tableName = null) |
|
198 | + { |
|
199 | + if ($tableName === null) { |
|
200 | + if (count($this->dbRows) > 1) { |
|
201 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
202 | + } elseif (count($this->dbRows) === 1) { |
|
203 | + $tableName = array_keys($this->dbRows)[0]; |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + return $tableName; |
|
208 | + } |
|
209 | + |
|
210 | + protected function get($var, $tableName = null) |
|
211 | + { |
|
212 | + $tableName = $this->checkTableName($tableName); |
|
213 | + |
|
214 | + if (!isset($this->dbRows[$tableName])) { |
|
215 | + return; |
|
216 | + } |
|
217 | + |
|
218 | + return $this->dbRows[$tableName]->get($var); |
|
219 | + } |
|
220 | + |
|
221 | + protected function set($var, $value, $tableName = null) |
|
222 | + { |
|
223 | + if ($tableName === null) { |
|
224 | + if (count($this->dbRows) > 1) { |
|
225 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
226 | + } elseif (count($this->dbRows) === 1) { |
|
227 | + $tableName = array_keys($this->dbRows)[0]; |
|
228 | + } else { |
|
229 | + throw new TDBMException('Please specify a table for this object.'); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + if (!isset($this->dbRows[$tableName])) { |
|
234 | + $this->registerTable($tableName); |
|
235 | + } |
|
236 | + |
|
237 | + $this->dbRows[$tableName]->set($var, $value); |
|
238 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
239 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
240 | + } |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @param string $foreignKeyName |
|
245 | + * @param AbstractTDBMObject $bean |
|
246 | + */ |
|
247 | + protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
248 | + { |
|
249 | + if ($tableName === null) { |
|
250 | + if (count($this->dbRows) > 1) { |
|
251 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
252 | + } elseif (count($this->dbRows) === 1) { |
|
253 | + $tableName = array_keys($this->dbRows)[0]; |
|
254 | + } else { |
|
255 | + throw new TDBMException('Please specify a table for this object.'); |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + if (!isset($this->dbRows[$tableName])) { |
|
260 | + $this->registerTable($tableName); |
|
261 | + } |
|
262 | + |
|
263 | + $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
264 | + if ($oldLinkedBean !== null) { |
|
265 | + $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
266 | + } |
|
267 | + |
|
268 | + $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
269 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
270 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
271 | + } |
|
272 | + |
|
273 | + if ($bean !== null) { |
|
274 | + $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + protected function getRef($foreignKeyName, $tableName = null) |
|
284 | + { |
|
285 | + $tableName = $this->checkTableName($tableName); |
|
286 | + |
|
287 | + if (!isset($this->dbRows[$tableName])) { |
|
288 | + return; |
|
289 | + } |
|
290 | + |
|
291 | + return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Adds a many to many relationship to this bean. |
|
296 | + * |
|
297 | + * @param string $pivotTableName |
|
298 | + * @param AbstractTDBMObject $remoteBean |
|
299 | + */ |
|
300 | + protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
301 | + { |
|
302 | + $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Returns true if there is a relationship to this bean. |
|
307 | + * |
|
308 | + * @param string $pivotTableName |
|
309 | + * @param AbstractTDBMObject $remoteBean |
|
310 | + * |
|
311 | + * @return bool |
|
312 | + */ |
|
313 | + protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
314 | + { |
|
315 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
316 | + |
|
317 | + if ($storage->contains($remoteBean)) { |
|
318 | + if ($storage[$remoteBean]['status'] !== 'delete') { |
|
319 | + return true; |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + return false; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Internal TDBM method. Removes a many to many relationship from this bean. |
|
328 | + * |
|
329 | + * @param string $pivotTableName |
|
330 | + * @param AbstractTDBMObject $remoteBean |
|
331 | + */ |
|
332 | + public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
333 | + { |
|
334 | + if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
335 | + unset($this->relationships[$pivotTableName][$remoteBean]); |
|
336 | + unset($remoteBean->relationships[$pivotTableName][$this]); |
|
337 | + } else { |
|
338 | + $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Sets many to many relationships for this bean. |
|
344 | + * Adds new relationships and removes unused ones. |
|
345 | + * |
|
346 | + * @param $pivotTableName |
|
347 | + * @param array $remoteBeans |
|
348 | + */ |
|
349 | + protected function setRelationships($pivotTableName, array $remoteBeans) |
|
350 | + { |
|
351 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
352 | + |
|
353 | + foreach ($storage as $oldRemoteBean) { |
|
354 | + if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
355 | + // $oldRemoteBean must be removed |
|
356 | + $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
357 | + } |
|
358 | + } |
|
359 | + |
|
360 | + foreach ($remoteBeans as $remoteBean) { |
|
361 | + if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
362 | + // $remoteBean must be added |
|
363 | + $this->addRelationship($pivotTableName, $remoteBean); |
|
364 | + } |
|
365 | + } |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Returns the list of objects linked to this bean via $pivotTableName. |
|
370 | + * |
|
371 | + * @param $pivotTableName |
|
372 | + * |
|
373 | + * @return \SplObjectStorage |
|
374 | + */ |
|
375 | + private function retrieveRelationshipsStorage($pivotTableName) |
|
376 | + { |
|
377 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
378 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
379 | + return $storage; |
|
380 | + } |
|
381 | + |
|
382 | + $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
383 | + $this->loadedRelationships[$pivotTableName] = true; |
|
384 | + |
|
385 | + foreach ($beans as $bean) { |
|
386 | + if (isset($storage[$bean])) { |
|
387 | + $oldStatus = $storage[$bean]['status']; |
|
388 | + if ($oldStatus === 'delete') { |
|
389 | + // Keep deleted things deleted |
|
390 | + continue; |
|
391 | + } |
|
392 | + } |
|
393 | + $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
394 | + } |
|
395 | + |
|
396 | + return $storage; |
|
397 | + } |
|
398 | + |
|
399 | + /** |
|
400 | + * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
401 | + * |
|
402 | + * @param $pivotTableName |
|
403 | + * |
|
404 | + * @return AbstractTDBMObject[] |
|
405 | + */ |
|
406 | + public function _getRelationships($pivotTableName) |
|
407 | + { |
|
408 | + return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
409 | + } |
|
410 | + |
|
411 | + private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
412 | + { |
|
413 | + $beans = []; |
|
414 | + foreach ($storage as $bean) { |
|
415 | + $statusArr = $storage[$bean]; |
|
416 | + if ($statusArr['status'] !== 'delete') { |
|
417 | + $beans[] = $bean; |
|
418 | + } |
|
419 | + } |
|
420 | + |
|
421 | + return $beans; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Declares a relationship between. |
|
426 | + * |
|
427 | + * @param string $pivotTableName |
|
428 | + * @param AbstractTDBMObject $remoteBean |
|
429 | + * @param string $status |
|
430 | + */ |
|
431 | + private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
432 | + { |
|
433 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
434 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
435 | + if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
436 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
437 | + } |
|
438 | + |
|
439 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
440 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
445 | + * |
|
446 | + * @param string $pivotTableName |
|
447 | + * |
|
448 | + * @return \SplObjectStorage |
|
449 | + */ |
|
450 | + private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
451 | + { |
|
452 | + return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
457 | + * |
|
458 | + * @param string $tableName |
|
459 | + * @param string $foreignKeyName |
|
460 | + * |
|
461 | + * @return AlterableResultIterator |
|
462 | + */ |
|
463 | + private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
464 | + { |
|
465 | + $key = $tableName.'___'.$foreignKeyName; |
|
466 | + |
|
467 | + return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * Declares a relationship between this bean and the bean pointing to it. |
|
472 | + * |
|
473 | + * @param string $tableName |
|
474 | + * @param string $foreignKeyName |
|
475 | + * @param AbstractTDBMObject $remoteBean |
|
476 | + */ |
|
477 | + private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
478 | + { |
|
479 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
480 | + $alterableResultIterator->add($remoteBean); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Declares a relationship between this bean and the bean pointing to it. |
|
485 | + * |
|
486 | + * @param string $tableName |
|
487 | + * @param string $foreignKeyName |
|
488 | + * @param AbstractTDBMObject $remoteBean |
|
489 | + */ |
|
490 | + private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
491 | + { |
|
492 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
493 | + $alterableResultIterator->remove($remoteBean); |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Returns the list of objects linked to this bean via a given foreign key. |
|
498 | + * |
|
499 | + * @param string $tableName |
|
500 | + * @param string $foreignKeyName |
|
501 | + * @param string $searchTableName |
|
502 | + * @param array $searchFilter |
|
503 | + * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
504 | + * |
|
505 | + * @return AlterableResultIterator |
|
506 | + */ |
|
507 | + protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
508 | + { |
|
509 | + $key = $tableName.'___'.$foreignKeyName; |
|
510 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
511 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
512 | + return $alterableResultIterator; |
|
513 | + } |
|
514 | + |
|
515 | + $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
516 | + |
|
517 | + $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
518 | + |
|
519 | + return $alterableResultIterator; |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Reverts any changes made to the object and resumes it to its DB state. |
|
524 | + * This can only be called on objects that come from database and that have not been deleted. |
|
525 | + * Otherwise, this will throw an exception. |
|
526 | + * |
|
527 | + * @throws TDBMException |
|
528 | + */ |
|
529 | + public function discardChanges() |
|
530 | + { |
|
531 | + if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
532 | + throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
533 | + } |
|
534 | + |
|
535 | + if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
536 | + throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
537 | + } |
|
538 | + |
|
539 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * Method used internally by TDBM. You should not use it directly. |
|
544 | + * This method returns the status of the TDBMObject. |
|
545 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
546 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
547 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
548 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
549 | + * |
|
550 | + * @return string |
|
551 | + */ |
|
552 | + public function _getStatus() |
|
553 | + { |
|
554 | + return $this->status; |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Override the native php clone function for TDBMObjects. |
|
559 | + */ |
|
560 | + public function __clone() |
|
561 | + { |
|
562 | + // Let's clone the many to many relationships |
|
563 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
564 | + $pivotTableList = array_keys($this->relationships); |
|
565 | + } else { |
|
566 | + $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
567 | + } |
|
568 | + |
|
569 | + foreach ($pivotTableList as $pivotTable) { |
|
570 | + $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
571 | + |
|
572 | + // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
573 | + /*foreach ($storage as $remoteBean) { |
|
574 | 574 | $metadata = $storage[$remoteBean]; |
575 | 575 | |
576 | 576 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
577 | 577 | $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
578 | 578 | }*/ |
579 | - } |
|
580 | - |
|
581 | - // Let's clone each row |
|
582 | - foreach ($this->dbRows as $key => &$dbRow) { |
|
583 | - $dbRow = clone $dbRow; |
|
584 | - $dbRow->setTDBMObject($this); |
|
585 | - } |
|
586 | - |
|
587 | - $this->manyToOneRelationships = []; |
|
588 | - |
|
589 | - // Let's set the status to new (to enter the save function) |
|
590 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Returns raw database rows. |
|
595 | - * |
|
596 | - * @return DbRow[] Key: table name, Value: DbRow object |
|
597 | - */ |
|
598 | - public function _getDbRows() |
|
599 | - { |
|
600 | - return $this->dbRows; |
|
601 | - } |
|
602 | - |
|
603 | - private function registerTable($tableName) |
|
604 | - { |
|
605 | - $dbRow = new DbRow($this, $tableName); |
|
606 | - |
|
607 | - if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
608 | - // Let's get the primary key for the new table |
|
609 | - $anotherDbRow = array_values($this->dbRows)[0]; |
|
610 | - /* @var $anotherDbRow DbRow */ |
|
611 | - $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
612 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
613 | - $dbRow->_setPrimaryKeys($primaryKeys); |
|
614 | - } |
|
615 | - |
|
616 | - $dbRow->_setStatus($this->status); |
|
617 | - |
|
618 | - $this->dbRows[$tableName] = $dbRow; |
|
619 | - // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Internal function: return the list of relationships. |
|
624 | - * |
|
625 | - * @return \SplObjectStorage[] |
|
626 | - */ |
|
627 | - public function _getCachedRelationships() |
|
628 | - { |
|
629 | - return $this->relationships; |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
634 | - * |
|
635 | - * @return string[] |
|
636 | - */ |
|
637 | - abstract protected function getUsedTables() : array; |
|
638 | - |
|
639 | - /** |
|
640 | - * Method called when the bean is removed from database. |
|
641 | - */ |
|
642 | - protected function onDelete() : void |
|
643 | - { |
|
644 | - } |
|
579 | + } |
|
580 | + |
|
581 | + // Let's clone each row |
|
582 | + foreach ($this->dbRows as $key => &$dbRow) { |
|
583 | + $dbRow = clone $dbRow; |
|
584 | + $dbRow->setTDBMObject($this); |
|
585 | + } |
|
586 | + |
|
587 | + $this->manyToOneRelationships = []; |
|
588 | + |
|
589 | + // Let's set the status to new (to enter the save function) |
|
590 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Returns raw database rows. |
|
595 | + * |
|
596 | + * @return DbRow[] Key: table name, Value: DbRow object |
|
597 | + */ |
|
598 | + public function _getDbRows() |
|
599 | + { |
|
600 | + return $this->dbRows; |
|
601 | + } |
|
602 | + |
|
603 | + private function registerTable($tableName) |
|
604 | + { |
|
605 | + $dbRow = new DbRow($this, $tableName); |
|
606 | + |
|
607 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
608 | + // Let's get the primary key for the new table |
|
609 | + $anotherDbRow = array_values($this->dbRows)[0]; |
|
610 | + /* @var $anotherDbRow DbRow */ |
|
611 | + $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
612 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
613 | + $dbRow->_setPrimaryKeys($primaryKeys); |
|
614 | + } |
|
615 | + |
|
616 | + $dbRow->_setStatus($this->status); |
|
617 | + |
|
618 | + $this->dbRows[$tableName] = $dbRow; |
|
619 | + // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Internal function: return the list of relationships. |
|
624 | + * |
|
625 | + * @return \SplObjectStorage[] |
|
626 | + */ |
|
627 | + public function _getCachedRelationships() |
|
628 | + { |
|
629 | + return $this->relationships; |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
634 | + * |
|
635 | + * @return string[] |
|
636 | + */ |
|
637 | + abstract protected function getUsedTables() : array; |
|
638 | + |
|
639 | + /** |
|
640 | + * Method called when the bean is removed from database. |
|
641 | + */ |
|
642 | + protected function onDelete() : void |
|
643 | + { |
|
644 | + } |
|
645 | 645 | } |