@@ -11,125 +11,125 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
13 | 13 | { |
14 | - /** |
|
15 | - * @var Column |
|
16 | - */ |
|
17 | - private $column; |
|
18 | - |
|
19 | - public function __construct(Table $table, Column $column) |
|
20 | - { |
|
21 | - parent::__construct($table); |
|
22 | - $this->table = $table; |
|
23 | - $this->column = $column; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
28 | - * |
|
29 | - * @return ForeignKeyConstraint|null |
|
30 | - */ |
|
31 | - public function getForeignKey() |
|
32 | - { |
|
33 | - return false; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Returns the param annotation for this property (useful for constructor). |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function getParamAnnotation() |
|
42 | - { |
|
43 | - $className = $this->getClassName(); |
|
44 | - $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
45 | - |
|
46 | - $str = ' * @param %s %s'; |
|
47 | - |
|
48 | - return sprintf($str, $paramType, $this->getVariableName()); |
|
49 | - } |
|
50 | - |
|
51 | - public function getUpperCamelCaseName() |
|
52 | - { |
|
53 | - return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
58 | - * |
|
59 | - * @return null|string |
|
60 | - */ |
|
61 | - public function getClassName() |
|
62 | - { |
|
63 | - return; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
68 | - * |
|
69 | - * @return bool |
|
70 | - */ |
|
71 | - public function isCompulsory() |
|
72 | - { |
|
73 | - return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns true if the property has a default value. |
|
78 | - * |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - public function hasDefault() |
|
82 | - { |
|
83 | - return $this->column->getDefault() !== null; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the code that assigns a value to its default value. |
|
88 | - * |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function assignToDefaultCode() |
|
92 | - { |
|
93 | - $str = ' $this->%s(%s);'; |
|
94 | - |
|
95 | - $default = $this->column->getDefault(); |
|
96 | - |
|
97 | - if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
98 | - $defaultCode = 'new \DateTimeImmutable()'; |
|
99 | - } else { |
|
100 | - $defaultCode = var_export($this->column->getDefault(), true); |
|
101 | - } |
|
102 | - |
|
103 | - return sprintf($str, $this->getSetterName(), $defaultCode); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Returns true if the property is the primary key. |
|
108 | - * |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - public function isPrimaryKey() |
|
112 | - { |
|
113 | - return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns the PHP code for getters and setters. |
|
118 | - * |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function getGetterSetterCode() |
|
122 | - { |
|
123 | - $type = $this->column->getType(); |
|
124 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
125 | - |
|
126 | - $columnGetterName = $this->getGetterName(); |
|
127 | - $columnSetterName = $this->getSetterName(); |
|
128 | - |
|
129 | - // A column type can be forced if it is not nullable and not auto-incrmentable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
130 | - $canForceGetterReturnType = $this->column->getNotnull() && !$this->column->getAutoincrement(); |
|
131 | - |
|
132 | - $getterAndSetterCode = ' /** |
|
14 | + /** |
|
15 | + * @var Column |
|
16 | + */ |
|
17 | + private $column; |
|
18 | + |
|
19 | + public function __construct(Table $table, Column $column) |
|
20 | + { |
|
21 | + parent::__construct($table); |
|
22 | + $this->table = $table; |
|
23 | + $this->column = $column; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
28 | + * |
|
29 | + * @return ForeignKeyConstraint|null |
|
30 | + */ |
|
31 | + public function getForeignKey() |
|
32 | + { |
|
33 | + return false; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Returns the param annotation for this property (useful for constructor). |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function getParamAnnotation() |
|
42 | + { |
|
43 | + $className = $this->getClassName(); |
|
44 | + $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
45 | + |
|
46 | + $str = ' * @param %s %s'; |
|
47 | + |
|
48 | + return sprintf($str, $paramType, $this->getVariableName()); |
|
49 | + } |
|
50 | + |
|
51 | + public function getUpperCamelCaseName() |
|
52 | + { |
|
53 | + return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
58 | + * |
|
59 | + * @return null|string |
|
60 | + */ |
|
61 | + public function getClassName() |
|
62 | + { |
|
63 | + return; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
68 | + * |
|
69 | + * @return bool |
|
70 | + */ |
|
71 | + public function isCompulsory() |
|
72 | + { |
|
73 | + return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns true if the property has a default value. |
|
78 | + * |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + public function hasDefault() |
|
82 | + { |
|
83 | + return $this->column->getDefault() !== null; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the code that assigns a value to its default value. |
|
88 | + * |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function assignToDefaultCode() |
|
92 | + { |
|
93 | + $str = ' $this->%s(%s);'; |
|
94 | + |
|
95 | + $default = $this->column->getDefault(); |
|
96 | + |
|
97 | + if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
98 | + $defaultCode = 'new \DateTimeImmutable()'; |
|
99 | + } else { |
|
100 | + $defaultCode = var_export($this->column->getDefault(), true); |
|
101 | + } |
|
102 | + |
|
103 | + return sprintf($str, $this->getSetterName(), $defaultCode); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Returns true if the property is the primary key. |
|
108 | + * |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + public function isPrimaryKey() |
|
112 | + { |
|
113 | + return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns the PHP code for getters and setters. |
|
118 | + * |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function getGetterSetterCode() |
|
122 | + { |
|
123 | + $type = $this->column->getType(); |
|
124 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
125 | + |
|
126 | + $columnGetterName = $this->getGetterName(); |
|
127 | + $columnSetterName = $this->getSetterName(); |
|
128 | + |
|
129 | + // A column type can be forced if it is not nullable and not auto-incrmentable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
130 | + $canForceGetterReturnType = $this->column->getNotnull() && !$this->column->getAutoincrement(); |
|
131 | + |
|
132 | + $getterAndSetterCode = ' /** |
|
133 | 133 | * The getter for the "%s" column. |
134 | 134 | * |
135 | 135 | * @return %s |
@@ -151,52 +151,52 @@ discard block |
||
151 | 151 | |
152 | 152 | '; |
153 | 153 | |
154 | - return sprintf($getterAndSetterCode, |
|
155 | - // Getter |
|
156 | - $this->column->getName(), |
|
157 | - $normalizedType.($canForceGetterReturnType ? '' : '|null'), |
|
158 | - $columnGetterName, |
|
159 | - ($canForceGetterReturnType ? ' : '.$normalizedType : ''), |
|
160 | - var_export($this->column->getName(), true), |
|
161 | - var_export($this->table->getName(), true), |
|
162 | - // Setter |
|
163 | - $this->column->getName(), |
|
164 | - $normalizedType, |
|
165 | - $this->column->getName(), |
|
166 | - $columnSetterName, |
|
167 | - $normalizedType, |
|
168 | - //$castTo, |
|
169 | - $this->column->getName().($this->column->getNotnull() ? '' : ' = null'), |
|
170 | - var_export($this->column->getName(), true), |
|
171 | - $this->column->getName(), |
|
172 | - var_export($this->table->getName(), true) |
|
173 | - ); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Returns the part of code useful when doing json serialization. |
|
178 | - * |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function getJsonSerializeCode() |
|
182 | - { |
|
183 | - $type = $this->column->getType(); |
|
184 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
185 | - |
|
186 | - if ($normalizedType == '\\DateTimeInterface') { |
|
187 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
188 | - } else { |
|
189 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Returns the column name. |
|
195 | - * |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - public function getColumnName() |
|
199 | - { |
|
200 | - return $this->column->getName(); |
|
201 | - } |
|
154 | + return sprintf($getterAndSetterCode, |
|
155 | + // Getter |
|
156 | + $this->column->getName(), |
|
157 | + $normalizedType.($canForceGetterReturnType ? '' : '|null'), |
|
158 | + $columnGetterName, |
|
159 | + ($canForceGetterReturnType ? ' : '.$normalizedType : ''), |
|
160 | + var_export($this->column->getName(), true), |
|
161 | + var_export($this->table->getName(), true), |
|
162 | + // Setter |
|
163 | + $this->column->getName(), |
|
164 | + $normalizedType, |
|
165 | + $this->column->getName(), |
|
166 | + $columnSetterName, |
|
167 | + $normalizedType, |
|
168 | + //$castTo, |
|
169 | + $this->column->getName().($this->column->getNotnull() ? '' : ' = null'), |
|
170 | + var_export($this->column->getName(), true), |
|
171 | + $this->column->getName(), |
|
172 | + var_export($this->table->getName(), true) |
|
173 | + ); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Returns the part of code useful when doing json serialization. |
|
178 | + * |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function getJsonSerializeCode() |
|
182 | + { |
|
183 | + $type = $this->column->getType(); |
|
184 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
185 | + |
|
186 | + if ($normalizedType == '\\DateTimeInterface') { |
|
187 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
188 | + } else { |
|
189 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Returns the column name. |
|
195 | + * |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + public function getColumnName() |
|
199 | + { |
|
200 | + return $this->column->getName(); |
|
201 | + } |
|
202 | 202 | } |
@@ -17,228 +17,228 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class BeanDescriptor |
19 | 19 | { |
20 | - /** |
|
21 | - * @var Table |
|
22 | - */ |
|
23 | - private $table; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var SchemaAnalyzer |
|
27 | - */ |
|
28 | - private $schemaAnalyzer; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var Schema |
|
32 | - */ |
|
33 | - private $schema; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var AbstractBeanPropertyDescriptor[] |
|
37 | - */ |
|
38 | - private $beanPropertyDescriptors = []; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var TDBMSchemaAnalyzer |
|
42 | - */ |
|
43 | - private $tdbmSchemaAnalyzer; |
|
44 | - |
|
45 | - public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
46 | - { |
|
47 | - $this->table = $table; |
|
48 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
49 | - $this->schema = $schema; |
|
50 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
51 | - $this->initBeanPropertyDescriptors(); |
|
52 | - } |
|
53 | - |
|
54 | - private function initBeanPropertyDescriptors() |
|
55 | - { |
|
56 | - $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
61 | - * |
|
62 | - * @param Table $table |
|
63 | - * @param Column $column |
|
64 | - * |
|
65 | - * @return ForeignKeyConstraint|null |
|
66 | - */ |
|
67 | - private function isPartOfForeignKey(Table $table, Column $column) |
|
68 | - { |
|
69 | - $localColumnName = $column->getName(); |
|
70 | - foreach ($table->getForeignKeys() as $foreignKey) { |
|
71 | - foreach ($foreignKey->getColumns() as $columnName) { |
|
72 | - if ($columnName === $localColumnName) { |
|
73 | - return $foreignKey; |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - return; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return AbstractBeanPropertyDescriptor[] |
|
83 | - */ |
|
84 | - public function getBeanPropertyDescriptors() |
|
85 | - { |
|
86 | - return $this->beanPropertyDescriptors; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
91 | - * |
|
92 | - * @return AbstractBeanPropertyDescriptor[] |
|
93 | - */ |
|
94 | - public function getConstructorProperties() |
|
95 | - { |
|
96 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
97 | - return $property->isCompulsory(); |
|
98 | - }); |
|
99 | - |
|
100 | - return $constructorProperties; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Returns the list of columns that have default values for a given table. |
|
105 | - * |
|
106 | - * @return AbstractBeanPropertyDescriptor[] |
|
107 | - */ |
|
108 | - public function getPropertiesWithDefault() |
|
109 | - { |
|
110 | - $properties = $this->getPropertiesForTable($this->table); |
|
111 | - $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
112 | - return $property->hasDefault(); |
|
113 | - }); |
|
114 | - |
|
115 | - return $defaultProperties; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Returns the list of properties exposed as getters and setters in this class. |
|
120 | - * |
|
121 | - * @return AbstractBeanPropertyDescriptor[] |
|
122 | - */ |
|
123 | - public function getExposedProperties() |
|
124 | - { |
|
125 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
126 | - return $property->getTable()->getName() == $this->table->getName(); |
|
127 | - }); |
|
128 | - |
|
129 | - return $exposedProperties; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns the list of properties for this table (including parent tables). |
|
134 | - * |
|
135 | - * @param Table $table |
|
136 | - * |
|
137 | - * @return AbstractBeanPropertyDescriptor[] |
|
138 | - */ |
|
139 | - private function getProperties(Table $table) |
|
140 | - { |
|
141 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
142 | - if ($parentRelationship) { |
|
143 | - $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
144 | - $properties = $this->getProperties($parentTable); |
|
145 | - // we merge properties by overriding property names. |
|
146 | - $localProperties = $this->getPropertiesForTable($table); |
|
147 | - foreach ($localProperties as $name => $property) { |
|
148 | - // We do not override properties if this is a primary key! |
|
149 | - if ($property->isPrimaryKey()) { |
|
150 | - continue; |
|
151 | - } |
|
152 | - $properties[$name] = $property; |
|
153 | - } |
|
154 | - } else { |
|
155 | - $properties = $this->getPropertiesForTable($table); |
|
156 | - } |
|
157 | - |
|
158 | - return $properties; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Returns the list of properties for this table (ignoring parent tables). |
|
163 | - * |
|
164 | - * @param Table $table |
|
165 | - * |
|
166 | - * @return AbstractBeanPropertyDescriptor[] |
|
167 | - */ |
|
168 | - private function getPropertiesForTable(Table $table) |
|
169 | - { |
|
170 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
171 | - if ($parentRelationship) { |
|
172 | - $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
173 | - } else { |
|
174 | - $ignoreColumns = []; |
|
175 | - } |
|
176 | - |
|
177 | - $beanPropertyDescriptors = []; |
|
178 | - |
|
179 | - foreach ($table->getColumns() as $column) { |
|
180 | - if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
181 | - continue; |
|
182 | - } |
|
183 | - |
|
184 | - $fk = $this->isPartOfForeignKey($table, $column); |
|
185 | - if ($fk !== null) { |
|
186 | - // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
187 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
188 | - if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
189 | - continue 2; |
|
190 | - } |
|
191 | - } |
|
192 | - // Check that this property is not an inheritance relationship |
|
193 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
194 | - if ($parentRelationship === $fk) { |
|
195 | - continue; |
|
196 | - } |
|
197 | - |
|
198 | - $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
199 | - } else { |
|
200 | - $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - // Now, let's get the name of all properties and let's check there is no duplicate. |
|
205 | - /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
206 | - $names = []; |
|
207 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
208 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
209 | - if (isset($names[$name])) { |
|
210 | - $names[$name]->useAlternativeName(); |
|
211 | - $beanDescriptor->useAlternativeName(); |
|
212 | - } else { |
|
213 | - $names[$name] = $beanDescriptor; |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - // Final check (throw exceptions if problem arises) |
|
218 | - $names = []; |
|
219 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | - if (isset($names[$name])) { |
|
222 | - throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
223 | - } else { |
|
224 | - $names[$name] = $beanDescriptor; |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - // Last step, let's rebuild the list with a map: |
|
229 | - $beanPropertyDescriptorsMap = []; |
|
230 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
231 | - $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
232 | - } |
|
233 | - |
|
234 | - return $beanPropertyDescriptorsMap; |
|
235 | - } |
|
236 | - |
|
237 | - public function generateBeanConstructor() |
|
238 | - { |
|
239 | - $constructorProperties = $this->getConstructorProperties(); |
|
240 | - |
|
241 | - $constructorCode = ' /** |
|
20 | + /** |
|
21 | + * @var Table |
|
22 | + */ |
|
23 | + private $table; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var SchemaAnalyzer |
|
27 | + */ |
|
28 | + private $schemaAnalyzer; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var Schema |
|
32 | + */ |
|
33 | + private $schema; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var AbstractBeanPropertyDescriptor[] |
|
37 | + */ |
|
38 | + private $beanPropertyDescriptors = []; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var TDBMSchemaAnalyzer |
|
42 | + */ |
|
43 | + private $tdbmSchemaAnalyzer; |
|
44 | + |
|
45 | + public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
46 | + { |
|
47 | + $this->table = $table; |
|
48 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
49 | + $this->schema = $schema; |
|
50 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
51 | + $this->initBeanPropertyDescriptors(); |
|
52 | + } |
|
53 | + |
|
54 | + private function initBeanPropertyDescriptors() |
|
55 | + { |
|
56 | + $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
61 | + * |
|
62 | + * @param Table $table |
|
63 | + * @param Column $column |
|
64 | + * |
|
65 | + * @return ForeignKeyConstraint|null |
|
66 | + */ |
|
67 | + private function isPartOfForeignKey(Table $table, Column $column) |
|
68 | + { |
|
69 | + $localColumnName = $column->getName(); |
|
70 | + foreach ($table->getForeignKeys() as $foreignKey) { |
|
71 | + foreach ($foreignKey->getColumns() as $columnName) { |
|
72 | + if ($columnName === $localColumnName) { |
|
73 | + return $foreignKey; |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + return; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return AbstractBeanPropertyDescriptor[] |
|
83 | + */ |
|
84 | + public function getBeanPropertyDescriptors() |
|
85 | + { |
|
86 | + return $this->beanPropertyDescriptors; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
91 | + * |
|
92 | + * @return AbstractBeanPropertyDescriptor[] |
|
93 | + */ |
|
94 | + public function getConstructorProperties() |
|
95 | + { |
|
96 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
97 | + return $property->isCompulsory(); |
|
98 | + }); |
|
99 | + |
|
100 | + return $constructorProperties; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Returns the list of columns that have default values for a given table. |
|
105 | + * |
|
106 | + * @return AbstractBeanPropertyDescriptor[] |
|
107 | + */ |
|
108 | + public function getPropertiesWithDefault() |
|
109 | + { |
|
110 | + $properties = $this->getPropertiesForTable($this->table); |
|
111 | + $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
112 | + return $property->hasDefault(); |
|
113 | + }); |
|
114 | + |
|
115 | + return $defaultProperties; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Returns the list of properties exposed as getters and setters in this class. |
|
120 | + * |
|
121 | + * @return AbstractBeanPropertyDescriptor[] |
|
122 | + */ |
|
123 | + public function getExposedProperties() |
|
124 | + { |
|
125 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
126 | + return $property->getTable()->getName() == $this->table->getName(); |
|
127 | + }); |
|
128 | + |
|
129 | + return $exposedProperties; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns the list of properties for this table (including parent tables). |
|
134 | + * |
|
135 | + * @param Table $table |
|
136 | + * |
|
137 | + * @return AbstractBeanPropertyDescriptor[] |
|
138 | + */ |
|
139 | + private function getProperties(Table $table) |
|
140 | + { |
|
141 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
142 | + if ($parentRelationship) { |
|
143 | + $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
144 | + $properties = $this->getProperties($parentTable); |
|
145 | + // we merge properties by overriding property names. |
|
146 | + $localProperties = $this->getPropertiesForTable($table); |
|
147 | + foreach ($localProperties as $name => $property) { |
|
148 | + // We do not override properties if this is a primary key! |
|
149 | + if ($property->isPrimaryKey()) { |
|
150 | + continue; |
|
151 | + } |
|
152 | + $properties[$name] = $property; |
|
153 | + } |
|
154 | + } else { |
|
155 | + $properties = $this->getPropertiesForTable($table); |
|
156 | + } |
|
157 | + |
|
158 | + return $properties; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Returns the list of properties for this table (ignoring parent tables). |
|
163 | + * |
|
164 | + * @param Table $table |
|
165 | + * |
|
166 | + * @return AbstractBeanPropertyDescriptor[] |
|
167 | + */ |
|
168 | + private function getPropertiesForTable(Table $table) |
|
169 | + { |
|
170 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
171 | + if ($parentRelationship) { |
|
172 | + $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
173 | + } else { |
|
174 | + $ignoreColumns = []; |
|
175 | + } |
|
176 | + |
|
177 | + $beanPropertyDescriptors = []; |
|
178 | + |
|
179 | + foreach ($table->getColumns() as $column) { |
|
180 | + if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
181 | + continue; |
|
182 | + } |
|
183 | + |
|
184 | + $fk = $this->isPartOfForeignKey($table, $column); |
|
185 | + if ($fk !== null) { |
|
186 | + // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
187 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
188 | + if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
189 | + continue 2; |
|
190 | + } |
|
191 | + } |
|
192 | + // Check that this property is not an inheritance relationship |
|
193 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
194 | + if ($parentRelationship === $fk) { |
|
195 | + continue; |
|
196 | + } |
|
197 | + |
|
198 | + $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
199 | + } else { |
|
200 | + $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + // Now, let's get the name of all properties and let's check there is no duplicate. |
|
205 | + /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
206 | + $names = []; |
|
207 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
208 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
209 | + if (isset($names[$name])) { |
|
210 | + $names[$name]->useAlternativeName(); |
|
211 | + $beanDescriptor->useAlternativeName(); |
|
212 | + } else { |
|
213 | + $names[$name] = $beanDescriptor; |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + // Final check (throw exceptions if problem arises) |
|
218 | + $names = []; |
|
219 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
220 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
221 | + if (isset($names[$name])) { |
|
222 | + throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
223 | + } else { |
|
224 | + $names[$name] = $beanDescriptor; |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + // Last step, let's rebuild the list with a map: |
|
229 | + $beanPropertyDescriptorsMap = []; |
|
230 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
231 | + $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
232 | + } |
|
233 | + |
|
234 | + return $beanPropertyDescriptorsMap; |
|
235 | + } |
|
236 | + |
|
237 | + public function generateBeanConstructor() |
|
238 | + { |
|
239 | + $constructorProperties = $this->getConstructorProperties(); |
|
240 | + |
|
241 | + $constructorCode = ' /** |
|
242 | 242 | * The constructor takes all compulsory arguments. |
243 | 243 | * |
244 | 244 | %s |
@@ -249,69 +249,69 @@ discard block |
||
249 | 249 | } |
250 | 250 | '; |
251 | 251 | |
252 | - $paramAnnotations = []; |
|
253 | - $arguments = []; |
|
254 | - $assigns = []; |
|
255 | - $parentConstructorArguments = []; |
|
256 | - |
|
257 | - foreach ($constructorProperties as $property) { |
|
258 | - $className = $property->getClassName(); |
|
259 | - if ($className) { |
|
260 | - $arguments[] = $className.' '.$property->getVariableName(); |
|
261 | - } else { |
|
262 | - $arguments[] = $property->getVariableName(); |
|
263 | - } |
|
264 | - $paramAnnotations[] = $property->getParamAnnotation(); |
|
265 | - if ($property->getTable()->getName() === $this->table->getName()) { |
|
266 | - $assigns[] = $property->getConstructorAssignCode(); |
|
267 | - } else { |
|
268 | - $parentConstructorArguments[] = $property->getVariableName(); |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
273 | - |
|
274 | - foreach ($this->getPropertiesWithDefault() as $property) { |
|
275 | - $assigns[] = $property->assignToDefaultCode(); |
|
276 | - } |
|
277 | - |
|
278 | - return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns)); |
|
279 | - } |
|
280 | - |
|
281 | - public function generateDirectForeignKeysCode() |
|
282 | - { |
|
283 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
284 | - |
|
285 | - $fksByTable = []; |
|
286 | - |
|
287 | - foreach ($fks as $fk) { |
|
288 | - $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
289 | - } |
|
290 | - |
|
291 | - /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
292 | - $fksByMethodName = []; |
|
293 | - |
|
294 | - foreach ($fksByTable as $tableName => $fksForTable) { |
|
295 | - if (count($fksForTable) > 1) { |
|
296 | - foreach ($fksForTable as $fk) { |
|
297 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
298 | - |
|
299 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
300 | - |
|
301 | - $methodName .= implode('And', $camelizedColumns); |
|
302 | - |
|
303 | - $fksByMethodName[$methodName] = $fk; |
|
304 | - } |
|
305 | - } else { |
|
306 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
307 | - $fksByMethodName[$methodName] = $fksForTable[0]; |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - $code = ''; |
|
312 | - |
|
313 | - foreach ($fksByMethodName as $methodName => $fk) { |
|
314 | - $getterCode = ' /** |
|
252 | + $paramAnnotations = []; |
|
253 | + $arguments = []; |
|
254 | + $assigns = []; |
|
255 | + $parentConstructorArguments = []; |
|
256 | + |
|
257 | + foreach ($constructorProperties as $property) { |
|
258 | + $className = $property->getClassName(); |
|
259 | + if ($className) { |
|
260 | + $arguments[] = $className.' '.$property->getVariableName(); |
|
261 | + } else { |
|
262 | + $arguments[] = $property->getVariableName(); |
|
263 | + } |
|
264 | + $paramAnnotations[] = $property->getParamAnnotation(); |
|
265 | + if ($property->getTable()->getName() === $this->table->getName()) { |
|
266 | + $assigns[] = $property->getConstructorAssignCode(); |
|
267 | + } else { |
|
268 | + $parentConstructorArguments[] = $property->getVariableName(); |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
273 | + |
|
274 | + foreach ($this->getPropertiesWithDefault() as $property) { |
|
275 | + $assigns[] = $property->assignToDefaultCode(); |
|
276 | + } |
|
277 | + |
|
278 | + return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns)); |
|
279 | + } |
|
280 | + |
|
281 | + public function generateDirectForeignKeysCode() |
|
282 | + { |
|
283 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
284 | + |
|
285 | + $fksByTable = []; |
|
286 | + |
|
287 | + foreach ($fks as $fk) { |
|
288 | + $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
289 | + } |
|
290 | + |
|
291 | + /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
292 | + $fksByMethodName = []; |
|
293 | + |
|
294 | + foreach ($fksByTable as $tableName => $fksForTable) { |
|
295 | + if (count($fksForTable) > 1) { |
|
296 | + foreach ($fksForTable as $fk) { |
|
297 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
298 | + |
|
299 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
300 | + |
|
301 | + $methodName .= implode('And', $camelizedColumns); |
|
302 | + |
|
303 | + $fksByMethodName[$methodName] = $fk; |
|
304 | + } |
|
305 | + } else { |
|
306 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
307 | + $fksByMethodName[$methodName] = $fksForTable[0]; |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + $code = ''; |
|
312 | + |
|
313 | + foreach ($fksByMethodName as $methodName => $fk) { |
|
314 | + $getterCode = ' /** |
|
315 | 315 | * Returns the list of %s pointing to this bean via the %s column. |
316 | 316 | * |
317 | 317 | * @return %s[]|AlterableResultIterator |
@@ -323,107 +323,107 @@ discard block |
||
323 | 323 | |
324 | 324 | '; |
325 | 325 | |
326 | - $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
327 | - $code .= sprintf($getterCode, |
|
328 | - $beanClass, |
|
329 | - implode(', ', $fk->getColumns()), |
|
330 | - $beanClass, |
|
331 | - $methodName, |
|
332 | - var_export($fk->getLocalTableName(), true), |
|
333 | - var_export($fk->getName(), true), |
|
334 | - var_export($fk->getLocalTableName(), true), |
|
335 | - $this->getFilters($fk) |
|
336 | - ); |
|
337 | - } |
|
338 | - |
|
339 | - return $code; |
|
340 | - } |
|
341 | - |
|
342 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
343 | - { |
|
344 | - $counter = 0; |
|
345 | - $parameters = []; |
|
346 | - |
|
347 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
348 | - |
|
349 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
350 | - $pkColumn = $pkColumns[$counter]; |
|
351 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
352 | - ++$counter; |
|
353 | - } |
|
354 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
355 | - |
|
356 | - return $parametersCode; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Generate code section about pivot tables. |
|
361 | - * |
|
362 | - * @return string |
|
363 | - */ |
|
364 | - public function generatePivotTableCode() |
|
365 | - { |
|
366 | - $finalDescs = $this->getPivotTableDescriptors(); |
|
367 | - |
|
368 | - $code = ''; |
|
369 | - |
|
370 | - foreach ($finalDescs as $desc) { |
|
371 | - $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
372 | - } |
|
373 | - |
|
374 | - return $code; |
|
375 | - } |
|
376 | - |
|
377 | - private function getPivotTableDescriptors() |
|
378 | - { |
|
379 | - $descs = []; |
|
380 | - foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
381 | - // There are exactly 2 FKs since this is a pivot table. |
|
382 | - $fks = array_values($table->getForeignKeys()); |
|
383 | - |
|
384 | - if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
385 | - $localFK = $fks[0]; |
|
386 | - $remoteFK = $fks[1]; |
|
387 | - } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
388 | - $localFK = $fks[1]; |
|
389 | - $remoteFK = $fks[0]; |
|
390 | - } else { |
|
391 | - continue; |
|
392 | - } |
|
393 | - |
|
394 | - $descs[$remoteFK->getForeignTableName()][] = [ |
|
395 | - 'table' => $table, |
|
396 | - 'localFK' => $localFK, |
|
397 | - 'remoteFK' => $remoteFK, |
|
398 | - ]; |
|
399 | - } |
|
400 | - |
|
401 | - $finalDescs = []; |
|
402 | - foreach ($descs as $descArray) { |
|
403 | - if (count($descArray) > 1) { |
|
404 | - foreach ($descArray as $desc) { |
|
405 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
406 | - $finalDescs[] = $desc; |
|
407 | - } |
|
408 | - } else { |
|
409 | - $desc = $descArray[0]; |
|
410 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
411 | - $finalDescs[] = $desc; |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - return $finalDescs; |
|
416 | - } |
|
417 | - |
|
418 | - public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
419 | - { |
|
420 | - $singularName = TDBMDaoGenerator::toSingular($name); |
|
421 | - $pluralName = $name; |
|
422 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
423 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
424 | - $pluralVariableName = $variableName.'s'; |
|
425 | - |
|
426 | - $str = ' /** |
|
326 | + $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
327 | + $code .= sprintf($getterCode, |
|
328 | + $beanClass, |
|
329 | + implode(', ', $fk->getColumns()), |
|
330 | + $beanClass, |
|
331 | + $methodName, |
|
332 | + var_export($fk->getLocalTableName(), true), |
|
333 | + var_export($fk->getName(), true), |
|
334 | + var_export($fk->getLocalTableName(), true), |
|
335 | + $this->getFilters($fk) |
|
336 | + ); |
|
337 | + } |
|
338 | + |
|
339 | + return $code; |
|
340 | + } |
|
341 | + |
|
342 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
343 | + { |
|
344 | + $counter = 0; |
|
345 | + $parameters = []; |
|
346 | + |
|
347 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
348 | + |
|
349 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
350 | + $pkColumn = $pkColumns[$counter]; |
|
351 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
352 | + ++$counter; |
|
353 | + } |
|
354 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
355 | + |
|
356 | + return $parametersCode; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Generate code section about pivot tables. |
|
361 | + * |
|
362 | + * @return string |
|
363 | + */ |
|
364 | + public function generatePivotTableCode() |
|
365 | + { |
|
366 | + $finalDescs = $this->getPivotTableDescriptors(); |
|
367 | + |
|
368 | + $code = ''; |
|
369 | + |
|
370 | + foreach ($finalDescs as $desc) { |
|
371 | + $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
372 | + } |
|
373 | + |
|
374 | + return $code; |
|
375 | + } |
|
376 | + |
|
377 | + private function getPivotTableDescriptors() |
|
378 | + { |
|
379 | + $descs = []; |
|
380 | + foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
381 | + // There are exactly 2 FKs since this is a pivot table. |
|
382 | + $fks = array_values($table->getForeignKeys()); |
|
383 | + |
|
384 | + if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
385 | + $localFK = $fks[0]; |
|
386 | + $remoteFK = $fks[1]; |
|
387 | + } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
388 | + $localFK = $fks[1]; |
|
389 | + $remoteFK = $fks[0]; |
|
390 | + } else { |
|
391 | + continue; |
|
392 | + } |
|
393 | + |
|
394 | + $descs[$remoteFK->getForeignTableName()][] = [ |
|
395 | + 'table' => $table, |
|
396 | + 'localFK' => $localFK, |
|
397 | + 'remoteFK' => $remoteFK, |
|
398 | + ]; |
|
399 | + } |
|
400 | + |
|
401 | + $finalDescs = []; |
|
402 | + foreach ($descs as $descArray) { |
|
403 | + if (count($descArray) > 1) { |
|
404 | + foreach ($descArray as $desc) { |
|
405 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
406 | + $finalDescs[] = $desc; |
|
407 | + } |
|
408 | + } else { |
|
409 | + $desc = $descArray[0]; |
|
410 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
411 | + $finalDescs[] = $desc; |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + return $finalDescs; |
|
416 | + } |
|
417 | + |
|
418 | + public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
419 | + { |
|
420 | + $singularName = TDBMDaoGenerator::toSingular($name); |
|
421 | + $pluralName = $name; |
|
422 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
423 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
424 | + $pluralVariableName = $variableName.'s'; |
|
425 | + |
|
426 | + $str = ' /** |
|
427 | 427 | * Returns the list of %s associated to this bean via the %s pivot table. |
428 | 428 | * |
429 | 429 | * @return %s[] |
@@ -434,9 +434,9 @@ discard block |
||
434 | 434 | } |
435 | 435 | '; |
436 | 436 | |
437 | - $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
437 | + $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
438 | 438 | |
439 | - $str = ' /** |
|
439 | + $str = ' /** |
|
440 | 440 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
441 | 441 | * |
442 | 442 | * @param %s %s |
@@ -447,9 +447,9 @@ discard block |
||
447 | 447 | } |
448 | 448 | '; |
449 | 449 | |
450 | - $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
450 | + $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
451 | 451 | |
452 | - $str = ' /** |
|
452 | + $str = ' /** |
|
453 | 453 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
454 | 454 | * |
455 | 455 | * @param %s %s |
@@ -460,9 +460,9 @@ discard block |
||
460 | 460 | } |
461 | 461 | '; |
462 | 462 | |
463 | - $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
463 | + $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
464 | 464 | |
465 | - $str = ' /** |
|
465 | + $str = ' /** |
|
466 | 466 | * Returns whether this bean is associated with %s via the %s pivot table. |
467 | 467 | * |
468 | 468 | * @param %s %s |
@@ -474,9 +474,9 @@ discard block |
||
474 | 474 | } |
475 | 475 | '; |
476 | 476 | |
477 | - $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
477 | + $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
478 | 478 | |
479 | - $str = ' /** |
|
479 | + $str = ' /** |
|
480 | 480 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
481 | 481 | * Exiting relationships will be removed and replaced by the provided relationships. |
482 | 482 | * |
@@ -488,24 +488,24 @@ discard block |
||
488 | 488 | } |
489 | 489 | '; |
490 | 490 | |
491 | - $setterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($remoteFK->getLocalTableName(), true), $pluralVariableName); |
|
491 | + $setterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($remoteFK->getLocalTableName(), true), $pluralVariableName); |
|
492 | 492 | |
493 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
493 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
494 | 494 | |
495 | - return $code; |
|
496 | - } |
|
495 | + return $code; |
|
496 | + } |
|
497 | 497 | |
498 | - public function generateJsonSerialize() |
|
499 | - { |
|
500 | - $tableName = $this->table->getName(); |
|
501 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
502 | - if ($parentFk !== null) { |
|
503 | - $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
504 | - } else { |
|
505 | - $initializer = '$array = [];'; |
|
506 | - } |
|
498 | + public function generateJsonSerialize() |
|
499 | + { |
|
500 | + $tableName = $this->table->getName(); |
|
501 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
502 | + if ($parentFk !== null) { |
|
503 | + $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
504 | + } else { |
|
505 | + $initializer = '$array = [];'; |
|
506 | + } |
|
507 | 507 | |
508 | - $str = ' |
|
508 | + $str = ' |
|
509 | 509 | /** |
510 | 510 | * Serializes the object for JSON encoding. |
511 | 511 | * |
@@ -521,97 +521,97 @@ discard block |
||
521 | 521 | } |
522 | 522 | '; |
523 | 523 | |
524 | - $propertiesCode = ''; |
|
525 | - foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
526 | - $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
527 | - } |
|
524 | + $propertiesCode = ''; |
|
525 | + foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
526 | + $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
527 | + } |
|
528 | 528 | |
529 | - // Many to many relationships: |
|
529 | + // Many to many relationships: |
|
530 | 530 | |
531 | - $descs = $this->getPivotTableDescriptors(); |
|
531 | + $descs = $this->getPivotTableDescriptors(); |
|
532 | 532 | |
533 | - $many2manyCode = ''; |
|
533 | + $many2manyCode = ''; |
|
534 | 534 | |
535 | - foreach ($descs as $desc) { |
|
536 | - $remoteFK = $desc['remoteFK']; |
|
537 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
538 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
535 | + foreach ($descs as $desc) { |
|
536 | + $remoteFK = $desc['remoteFK']; |
|
537 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
538 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
539 | 539 | |
540 | - $many2manyCode .= ' if (!$stopRecursion) { |
|
540 | + $many2manyCode .= ' if (!$stopRecursion) { |
|
541 | 541 | $array[\''.lcfirst($desc['name']).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
542 | 542 | return '.$variableName.'->jsonSerialize(true); |
543 | 543 | }, $this->get'.$desc['name'].'()); |
544 | 544 | } |
545 | 545 | '; |
546 | - } |
|
547 | - |
|
548 | - return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
549 | - } |
|
550 | - |
|
551 | - /** |
|
552 | - * Returns as an array the class we need to extend from and the list of use statements. |
|
553 | - * |
|
554 | - * @return array |
|
555 | - */ |
|
556 | - private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
557 | - { |
|
558 | - $classes = []; |
|
559 | - if ($parentFk !== null) { |
|
560 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
561 | - $classes[] = $extends; |
|
562 | - } |
|
563 | - |
|
564 | - foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
565 | - $className = $beanPropertyDescriptor->getClassName(); |
|
566 | - if (null !== $className) { |
|
567 | - $classes[] = $beanPropertyDescriptor->getClassName(); |
|
568 | - } |
|
569 | - } |
|
570 | - |
|
571 | - foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
572 | - /* @var $fk ForeignKeyConstraint */ |
|
573 | - $fk = $descriptor['remoteFK']; |
|
574 | - $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
575 | - } |
|
576 | - |
|
577 | - // Many-to-one relationships |
|
578 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
579 | - foreach ($fks as $fk) { |
|
580 | - $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
581 | - } |
|
582 | - |
|
583 | - $classes = array_unique($classes); |
|
584 | - |
|
585 | - return $classes; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
590 | - * |
|
591 | - * @param string $beannamespace The namespace of the bean |
|
592 | - */ |
|
593 | - public function generatePhpCode($beannamespace) |
|
594 | - { |
|
595 | - $tableName = $this->table->getName(); |
|
596 | - $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
597 | - $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
598 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
599 | - |
|
600 | - $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
601 | - |
|
602 | - $uses = array_map(function ($className) use ($beannamespace) { |
|
603 | - return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
604 | - }, $classes); |
|
605 | - $use = implode('', $uses); |
|
606 | - |
|
607 | - if ($parentFk !== null) { |
|
608 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
609 | - } else { |
|
610 | - $extends = 'AbstractTDBMObject'; |
|
611 | - $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
612 | - } |
|
613 | - |
|
614 | - $str = "<?php |
|
546 | + } |
|
547 | + |
|
548 | + return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
549 | + } |
|
550 | + |
|
551 | + /** |
|
552 | + * Returns as an array the class we need to extend from and the list of use statements. |
|
553 | + * |
|
554 | + * @return array |
|
555 | + */ |
|
556 | + private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
557 | + { |
|
558 | + $classes = []; |
|
559 | + if ($parentFk !== null) { |
|
560 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
561 | + $classes[] = $extends; |
|
562 | + } |
|
563 | + |
|
564 | + foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
565 | + $className = $beanPropertyDescriptor->getClassName(); |
|
566 | + if (null !== $className) { |
|
567 | + $classes[] = $beanPropertyDescriptor->getClassName(); |
|
568 | + } |
|
569 | + } |
|
570 | + |
|
571 | + foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
572 | + /* @var $fk ForeignKeyConstraint */ |
|
573 | + $fk = $descriptor['remoteFK']; |
|
574 | + $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
575 | + } |
|
576 | + |
|
577 | + // Many-to-one relationships |
|
578 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
579 | + foreach ($fks as $fk) { |
|
580 | + $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
581 | + } |
|
582 | + |
|
583 | + $classes = array_unique($classes); |
|
584 | + |
|
585 | + return $classes; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
590 | + * |
|
591 | + * @param string $beannamespace The namespace of the bean |
|
592 | + */ |
|
593 | + public function generatePhpCode($beannamespace) |
|
594 | + { |
|
595 | + $tableName = $this->table->getName(); |
|
596 | + $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
597 | + $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
598 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
599 | + |
|
600 | + $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
601 | + |
|
602 | + $uses = array_map(function ($className) use ($beannamespace) { |
|
603 | + return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
604 | + }, $classes); |
|
605 | + $use = implode('', $uses); |
|
606 | + |
|
607 | + if ($parentFk !== null) { |
|
608 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
609 | + } else { |
|
610 | + $extends = 'AbstractTDBMObject'; |
|
611 | + $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
612 | + } |
|
613 | + |
|
614 | + $str = "<?php |
|
615 | 615 | namespace {$beannamespace}\\Generated; |
616 | 616 | |
617 | 617 | use Mouf\\Database\\TDBM\\ResultIterator; |
@@ -631,126 +631,126 @@ discard block |
||
631 | 631 | { |
632 | 632 | "; |
633 | 633 | |
634 | - $str .= $this->generateBeanConstructor(); |
|
634 | + $str .= $this->generateBeanConstructor(); |
|
635 | 635 | |
636 | - foreach ($this->getExposedProperties() as $property) { |
|
637 | - $str .= $property->getGetterSetterCode(); |
|
638 | - } |
|
636 | + foreach ($this->getExposedProperties() as $property) { |
|
637 | + $str .= $property->getGetterSetterCode(); |
|
638 | + } |
|
639 | 639 | |
640 | - $str .= $this->generateDirectForeignKeysCode(); |
|
641 | - $str .= $this->generatePivotTableCode(); |
|
642 | - $str .= $this->generateJsonSerialize(); |
|
640 | + $str .= $this->generateDirectForeignKeysCode(); |
|
641 | + $str .= $this->generatePivotTableCode(); |
|
642 | + $str .= $this->generateJsonSerialize(); |
|
643 | 643 | |
644 | - $str .= $this->generateGetUsedTablesCode(); |
|
644 | + $str .= $this->generateGetUsedTablesCode(); |
|
645 | 645 | |
646 | - $str .= '} |
|
646 | + $str .= '} |
|
647 | 647 | '; |
648 | 648 | |
649 | - return $str; |
|
650 | - } |
|
651 | - |
|
652 | - /** |
|
653 | - * @param string $beanNamespace |
|
654 | - * @param string $beanClassName |
|
655 | - * |
|
656 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
657 | - */ |
|
658 | - public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
659 | - { |
|
660 | - $code = ''; |
|
661 | - $usedBeans = []; |
|
662 | - foreach ($this->table->getIndexes() as $index) { |
|
663 | - if (!$index->isPrimary()) { |
|
664 | - list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
665 | - $code .= $codeForIndex; |
|
666 | - $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
667 | - } |
|
668 | - } |
|
669 | - |
|
670 | - return [$usedBeans, $code]; |
|
671 | - } |
|
672 | - |
|
673 | - /** |
|
674 | - * @param Index $index |
|
675 | - * @param string $beanNamespace |
|
676 | - * @param string $beanClassName |
|
677 | - * |
|
678 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
679 | - */ |
|
680 | - private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
681 | - { |
|
682 | - $columns = $index->getColumns(); |
|
683 | - $usedBeans = []; |
|
684 | - |
|
685 | - /* |
|
649 | + return $str; |
|
650 | + } |
|
651 | + |
|
652 | + /** |
|
653 | + * @param string $beanNamespace |
|
654 | + * @param string $beanClassName |
|
655 | + * |
|
656 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
657 | + */ |
|
658 | + public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
659 | + { |
|
660 | + $code = ''; |
|
661 | + $usedBeans = []; |
|
662 | + foreach ($this->table->getIndexes() as $index) { |
|
663 | + if (!$index->isPrimary()) { |
|
664 | + list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
665 | + $code .= $codeForIndex; |
|
666 | + $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
667 | + } |
|
668 | + } |
|
669 | + |
|
670 | + return [$usedBeans, $code]; |
|
671 | + } |
|
672 | + |
|
673 | + /** |
|
674 | + * @param Index $index |
|
675 | + * @param string $beanNamespace |
|
676 | + * @param string $beanClassName |
|
677 | + * |
|
678 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
679 | + */ |
|
680 | + private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
681 | + { |
|
682 | + $columns = $index->getColumns(); |
|
683 | + $usedBeans = []; |
|
684 | + |
|
685 | + /* |
|
686 | 686 | * The list of elements building this index (expressed as columns or foreign keys) |
687 | 687 | * @var AbstractBeanPropertyDescriptor[] |
688 | 688 | */ |
689 | - $elements = []; |
|
690 | - |
|
691 | - foreach ($columns as $column) { |
|
692 | - $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
693 | - if ($fk !== null) { |
|
694 | - if (!in_array($fk, $elements)) { |
|
695 | - $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
696 | - } |
|
697 | - } else { |
|
698 | - $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
699 | - } |
|
700 | - } |
|
701 | - |
|
702 | - // If the index is actually only a foreign key, let's bypass it entirely. |
|
703 | - if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
704 | - return [[], '']; |
|
705 | - } |
|
706 | - |
|
707 | - $methodNameComponent = []; |
|
708 | - $functionParameters = []; |
|
709 | - $first = true; |
|
710 | - foreach ($elements as $element) { |
|
711 | - $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
712 | - $functionParameter = $element->getClassName(); |
|
713 | - if ($functionParameter) { |
|
714 | - $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
715 | - $functionParameter .= ' '; |
|
716 | - } |
|
717 | - $functionParameter .= $element->getVariableName(); |
|
718 | - if ($first) { |
|
719 | - $first = false; |
|
720 | - } else { |
|
721 | - $functionParameter .= ' = null'; |
|
722 | - } |
|
723 | - $functionParameters[] = $functionParameter; |
|
724 | - } |
|
725 | - if ($index->isUnique()) { |
|
726 | - $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
727 | - $calledMethod = 'findOne'; |
|
728 | - $returnType = "{$beanClassName}"; |
|
729 | - } else { |
|
730 | - $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
731 | - $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
732 | - $calledMethod = 'find'; |
|
733 | - } |
|
734 | - $functionParametersString = implode(', ', $functionParameters); |
|
735 | - |
|
736 | - $count = 0; |
|
737 | - |
|
738 | - $params = []; |
|
739 | - $filterArrayCode = ''; |
|
740 | - $commentArguments = []; |
|
741 | - foreach ($elements as $element) { |
|
742 | - $params[] = $element->getParamAnnotation(); |
|
743 | - if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
744 | - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
745 | - } else { |
|
746 | - ++$count; |
|
747 | - $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
748 | - } |
|
749 | - $commentArguments[] = substr($element->getVariableName(), 1); |
|
750 | - } |
|
751 | - $paramsString = implode("\n", $params); |
|
752 | - |
|
753 | - $code = " |
|
689 | + $elements = []; |
|
690 | + |
|
691 | + foreach ($columns as $column) { |
|
692 | + $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
693 | + if ($fk !== null) { |
|
694 | + if (!in_array($fk, $elements)) { |
|
695 | + $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
696 | + } |
|
697 | + } else { |
|
698 | + $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
699 | + } |
|
700 | + } |
|
701 | + |
|
702 | + // If the index is actually only a foreign key, let's bypass it entirely. |
|
703 | + if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
704 | + return [[], '']; |
|
705 | + } |
|
706 | + |
|
707 | + $methodNameComponent = []; |
|
708 | + $functionParameters = []; |
|
709 | + $first = true; |
|
710 | + foreach ($elements as $element) { |
|
711 | + $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
712 | + $functionParameter = $element->getClassName(); |
|
713 | + if ($functionParameter) { |
|
714 | + $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
715 | + $functionParameter .= ' '; |
|
716 | + } |
|
717 | + $functionParameter .= $element->getVariableName(); |
|
718 | + if ($first) { |
|
719 | + $first = false; |
|
720 | + } else { |
|
721 | + $functionParameter .= ' = null'; |
|
722 | + } |
|
723 | + $functionParameters[] = $functionParameter; |
|
724 | + } |
|
725 | + if ($index->isUnique()) { |
|
726 | + $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
727 | + $calledMethod = 'findOne'; |
|
728 | + $returnType = "{$beanClassName}"; |
|
729 | + } else { |
|
730 | + $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
731 | + $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
732 | + $calledMethod = 'find'; |
|
733 | + } |
|
734 | + $functionParametersString = implode(', ', $functionParameters); |
|
735 | + |
|
736 | + $count = 0; |
|
737 | + |
|
738 | + $params = []; |
|
739 | + $filterArrayCode = ''; |
|
740 | + $commentArguments = []; |
|
741 | + foreach ($elements as $element) { |
|
742 | + $params[] = $element->getParamAnnotation(); |
|
743 | + if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
744 | + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
745 | + } else { |
|
746 | + ++$count; |
|
747 | + $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
748 | + } |
|
749 | + $commentArguments[] = substr($element->getVariableName(), 1); |
|
750 | + } |
|
751 | + $paramsString = implode("\n", $params); |
|
752 | + |
|
753 | + $code = " |
|
754 | 754 | /** |
755 | 755 | * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).". |
756 | 756 | * |
@@ -768,27 +768,27 @@ discard block |
||
768 | 768 | } |
769 | 769 | "; |
770 | 770 | |
771 | - return [$usedBeans, $code]; |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * Generates the code for the getUsedTable protected method. |
|
776 | - * |
|
777 | - * @return string |
|
778 | - */ |
|
779 | - private function generateGetUsedTablesCode() |
|
780 | - { |
|
781 | - $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
782 | - if ($hasParentRelationship) { |
|
783 | - $code = sprintf(' $tables = parent::getUsedTables(); |
|
771 | + return [$usedBeans, $code]; |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * Generates the code for the getUsedTable protected method. |
|
776 | + * |
|
777 | + * @return string |
|
778 | + */ |
|
779 | + private function generateGetUsedTablesCode() |
|
780 | + { |
|
781 | + $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
782 | + if ($hasParentRelationship) { |
|
783 | + $code = sprintf(' $tables = parent::getUsedTables(); |
|
784 | 784 | $tables[] = %s; |
785 | 785 | |
786 | 786 | return $tables;', var_export($this->table->getName(), true)); |
787 | - } else { |
|
788 | - $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
789 | - } |
|
787 | + } else { |
|
788 | + $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
789 | + } |
|
790 | 790 | |
791 | - return sprintf(' |
|
791 | + return sprintf(' |
|
792 | 792 | /** |
793 | 793 | * Returns an array of used tables by this bean (from parent to child relationship). |
794 | 794 | * |
@@ -799,5 +799,5 @@ discard block |
||
799 | 799 | %s |
800 | 800 | } |
801 | 801 | ', $code); |
802 | - } |
|
802 | + } |
|
803 | 803 | } |