@@ -27,170 +27,170 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class DbRow |
29 | 29 | { |
30 | - /** |
|
31 | - * The service this object is bound to. |
|
32 | - * |
|
33 | - * @var TDBMService |
|
34 | - */ |
|
35 | - protected $tdbmService; |
|
36 | - |
|
37 | - /** |
|
38 | - * The object containing this db row. |
|
39 | - * |
|
40 | - * @var AbstractTDBMObject |
|
41 | - */ |
|
42 | - private $object; |
|
43 | - |
|
44 | - /** |
|
45 | - * The name of the table the object if issued from. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $dbTableName; |
|
50 | - |
|
51 | - /** |
|
52 | - * The array of columns returned from database. |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private $dbRow = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * @var AbstractTDBMObject[] |
|
60 | - */ |
|
61 | - private $references = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - private $status; |
|
72 | - |
|
73 | - /** |
|
74 | - * The values of the primary key. |
|
75 | - * This is set when the object is in "loaded" state. |
|
76 | - * |
|
77 | - * @var array An array of column => value |
|
78 | - */ |
|
79 | - private $primaryKeys; |
|
80 | - |
|
81 | - /** |
|
82 | - * You should never call the constructor directly. Instead, you should use the |
|
83 | - * TDBMService class that will create TDBMObjects for you. |
|
84 | - * |
|
85 | - * Used with id!=false when we want to retrieve an existing object |
|
86 | - * and id==false if we want a new object |
|
87 | - * |
|
88 | - * @param AbstractTDBMObject $object The object containing this db row |
|
89 | - * @param string $table_name |
|
90 | - * @param array $primaryKeys |
|
91 | - * @param TDBMService $tdbmService |
|
92 | - * |
|
93 | - * @throws TDBMException |
|
94 | - * @throws TDBMInvalidOperationException |
|
95 | - */ |
|
96 | - public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | - { |
|
98 | - $this->object = $object; |
|
99 | - $this->dbTableName = $table_name; |
|
100 | - |
|
101 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | - |
|
103 | - if ($tdbmService === null) { |
|
104 | - if (!empty($primaryKeys)) { |
|
105 | - throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | - } |
|
107 | - } else { |
|
108 | - $this->tdbmService = $tdbmService; |
|
109 | - |
|
110 | - if (!empty($primaryKeys)) { |
|
111 | - $this->_setPrimaryKeys($primaryKeys); |
|
112 | - if (!empty($dbRow)) { |
|
113 | - $this->dbRow = $dbRow; |
|
114 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | - } else { |
|
116 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | - } |
|
118 | - $tdbmService->_addToCache($this); |
|
119 | - } else { |
|
120 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - public function _attach(TDBMService $tdbmService) |
|
127 | - { |
|
128 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | - } |
|
131 | - $this->tdbmService = $tdbmService; |
|
132 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the state of the TDBM Object |
|
138 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | - * |
|
143 | - * @param string $state |
|
144 | - */ |
|
145 | - public function _setStatus($state) |
|
146 | - { |
|
147 | - $this->status = $state; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | - * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | - * |
|
154 | - * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | - * cannot be found). |
|
156 | - */ |
|
157 | - public function _dbLoadIfNotLoaded() |
|
158 | - { |
|
159 | - if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | - $connection = $this->tdbmService->getConnection(); |
|
161 | - |
|
162 | - /// buildFilterFromFilterBag($filter_bag) |
|
163 | - list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | - |
|
165 | - $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | - $result = $connection->executeQuery($sql, $parameters); |
|
167 | - |
|
168 | - if ($result->rowCount() === 0) { |
|
169 | - throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | - } |
|
171 | - |
|
172 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | - |
|
174 | - $this->dbRow = []; |
|
175 | - $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | - |
|
177 | - foreach ($row as $key => $value) { |
|
178 | - $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | - } |
|
180 | - |
|
181 | - $result->closeCursor(); |
|
182 | - |
|
183 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - public function get($var) |
|
188 | - { |
|
189 | - $this->_dbLoadIfNotLoaded(); |
|
190 | - |
|
191 | - // Let's first check if the key exist. |
|
192 | - if (!isset($this->dbRow[$var])) { |
|
193 | - /* |
|
30 | + /** |
|
31 | + * The service this object is bound to. |
|
32 | + * |
|
33 | + * @var TDBMService |
|
34 | + */ |
|
35 | + protected $tdbmService; |
|
36 | + |
|
37 | + /** |
|
38 | + * The object containing this db row. |
|
39 | + * |
|
40 | + * @var AbstractTDBMObject |
|
41 | + */ |
|
42 | + private $object; |
|
43 | + |
|
44 | + /** |
|
45 | + * The name of the table the object if issued from. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $dbTableName; |
|
50 | + |
|
51 | + /** |
|
52 | + * The array of columns returned from database. |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private $dbRow = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * @var AbstractTDBMObject[] |
|
60 | + */ |
|
61 | + private $references = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + private $status; |
|
72 | + |
|
73 | + /** |
|
74 | + * The values of the primary key. |
|
75 | + * This is set when the object is in "loaded" state. |
|
76 | + * |
|
77 | + * @var array An array of column => value |
|
78 | + */ |
|
79 | + private $primaryKeys; |
|
80 | + |
|
81 | + /** |
|
82 | + * You should never call the constructor directly. Instead, you should use the |
|
83 | + * TDBMService class that will create TDBMObjects for you. |
|
84 | + * |
|
85 | + * Used with id!=false when we want to retrieve an existing object |
|
86 | + * and id==false if we want a new object |
|
87 | + * |
|
88 | + * @param AbstractTDBMObject $object The object containing this db row |
|
89 | + * @param string $table_name |
|
90 | + * @param array $primaryKeys |
|
91 | + * @param TDBMService $tdbmService |
|
92 | + * |
|
93 | + * @throws TDBMException |
|
94 | + * @throws TDBMInvalidOperationException |
|
95 | + */ |
|
96 | + public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | + { |
|
98 | + $this->object = $object; |
|
99 | + $this->dbTableName = $table_name; |
|
100 | + |
|
101 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | + |
|
103 | + if ($tdbmService === null) { |
|
104 | + if (!empty($primaryKeys)) { |
|
105 | + throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | + } |
|
107 | + } else { |
|
108 | + $this->tdbmService = $tdbmService; |
|
109 | + |
|
110 | + if (!empty($primaryKeys)) { |
|
111 | + $this->_setPrimaryKeys($primaryKeys); |
|
112 | + if (!empty($dbRow)) { |
|
113 | + $this->dbRow = $dbRow; |
|
114 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | + } else { |
|
116 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | + } |
|
118 | + $tdbmService->_addToCache($this); |
|
119 | + } else { |
|
120 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + public function _attach(TDBMService $tdbmService) |
|
127 | + { |
|
128 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | + } |
|
131 | + $this->tdbmService = $tdbmService; |
|
132 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the state of the TDBM Object |
|
138 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | + * |
|
143 | + * @param string $state |
|
144 | + */ |
|
145 | + public function _setStatus($state) |
|
146 | + { |
|
147 | + $this->status = $state; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | + * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | + * |
|
154 | + * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | + * cannot be found). |
|
156 | + */ |
|
157 | + public function _dbLoadIfNotLoaded() |
|
158 | + { |
|
159 | + if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | + $connection = $this->tdbmService->getConnection(); |
|
161 | + |
|
162 | + /// buildFilterFromFilterBag($filter_bag) |
|
163 | + list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | + |
|
165 | + $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | + $result = $connection->executeQuery($sql, $parameters); |
|
167 | + |
|
168 | + if ($result->rowCount() === 0) { |
|
169 | + throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | + } |
|
171 | + |
|
172 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | + |
|
174 | + $this->dbRow = []; |
|
175 | + $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | + |
|
177 | + foreach ($row as $key => $value) { |
|
178 | + $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | + } |
|
180 | + |
|
181 | + $result->closeCursor(); |
|
182 | + |
|
183 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + public function get($var) |
|
188 | + { |
|
189 | + $this->_dbLoadIfNotLoaded(); |
|
190 | + |
|
191 | + // Let's first check if the key exist. |
|
192 | + if (!isset($this->dbRow[$var])) { |
|
193 | + /* |
|
194 | 194 | // Unable to find column.... this is an error if the object has been retrieved from database. |
195 | 195 | // If it's a new object, well, that may not be an error after all! |
196 | 196 | // Let's check if the column does exist in the table |
@@ -210,39 +210,39 @@ discard block |
||
210 | 210 | $str = "Could not find column \"$var\" in table \"$this->dbTableName\". Maybe you meant one of those columns: '".implode("', '",$result_array)."'"; |
211 | 211 | |
212 | 212 | throw new TDBMException($str);*/ |
213 | - return; |
|
214 | - } |
|
215 | - |
|
216 | - $value = $this->dbRow[$var]; |
|
217 | - if ($value instanceof \DateTime) { |
|
218 | - if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | - return \DateTimeImmutable::createFromMutable($value); |
|
220 | - } else { |
|
221 | - return new \DateTimeImmutable($value->format('c')); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - return $this->dbRow[$var]; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns true if a column is set, false otherwise. |
|
230 | - * |
|
231 | - * @param string $var |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - /*public function has($var) { |
|
213 | + return; |
|
214 | + } |
|
215 | + |
|
216 | + $value = $this->dbRow[$var]; |
|
217 | + if ($value instanceof \DateTime) { |
|
218 | + if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | + return \DateTimeImmutable::createFromMutable($value); |
|
220 | + } else { |
|
221 | + return new \DateTimeImmutable($value->format('c')); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + return $this->dbRow[$var]; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns true if a column is set, false otherwise. |
|
230 | + * |
|
231 | + * @param string $var |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + /*public function has($var) { |
|
236 | 236 | $this->_dbLoadIfNotLoaded(); |
237 | 237 | |
238 | 238 | return isset($this->dbRow[$var]); |
239 | 239 | }*/ |
240 | 240 | |
241 | - public function set($var, $value) |
|
242 | - { |
|
243 | - $this->_dbLoadIfNotLoaded(); |
|
241 | + public function set($var, $value) |
|
242 | + { |
|
243 | + $this->_dbLoadIfNotLoaded(); |
|
244 | 244 | |
245 | - /* |
|
245 | + /* |
|
246 | 246 | // Ok, let's start by checking the column type |
247 | 247 | $type = $this->db_connection->getColumnType($this->dbTableName, $var); |
248 | 248 | |
@@ -252,198 +252,198 @@ discard block |
||
252 | 252 | } |
253 | 253 | */ |
254 | 254 | |
255 | - /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
255 | + /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
256 | 256 | throw new TDBMException("Error! Changing primary key value is forbidden.");*/ |
257 | - $this->dbRow[$var] = $value; |
|
258 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param string $foreignKeyName |
|
266 | - * @param AbstractTDBMObject $bean |
|
267 | - */ |
|
268 | - public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | - { |
|
270 | - $this->references[$foreignKeyName] = $bean; |
|
271 | - |
|
272 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - public function getRef($foreignKeyName) |
|
284 | - { |
|
285 | - if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | - return $this->references[$foreignKeyName]; |
|
287 | - } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | - // If the object is new and has no property, then it has to be empty. |
|
289 | - return; |
|
290 | - } else { |
|
291 | - $this->_dbLoadIfNotLoaded(); |
|
292 | - |
|
293 | - // Let's match the name of the columns to the primary key values |
|
294 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | - |
|
296 | - $values = []; |
|
297 | - foreach ($fk->getLocalColumns() as $column) { |
|
298 | - if (!isset($this->dbRow[$column])) { |
|
299 | - return; |
|
300 | - } |
|
301 | - $values[] = $this->dbRow[$column]; |
|
302 | - } |
|
303 | - |
|
304 | - $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | - |
|
306 | - return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns the name of the table this object comes from. |
|
312 | - * |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function _getDbTableName() |
|
316 | - { |
|
317 | - return $this->dbTableName; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Method used internally by TDBM. You should not use it directly. |
|
322 | - * This method returns the status of the TDBMObject. |
|
323 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | - * |
|
328 | - * @return string |
|
329 | - */ |
|
330 | - public function _getStatus() |
|
331 | - { |
|
332 | - return $this->status; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Override the native php clone function for TDBMObjects. |
|
337 | - */ |
|
338 | - public function __clone() |
|
339 | - { |
|
340 | - // Let's load the row (before we lose the ID!) |
|
341 | - $this->_dbLoadIfNotLoaded(); |
|
342 | - |
|
343 | - //Let's set the status to detached |
|
344 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | - |
|
346 | - $this->primaryKeys = []; |
|
347 | - |
|
348 | - //Now unset the PK from the row |
|
349 | - if ($this->tdbmService) { |
|
350 | - $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | - foreach ($pk_array as $pk) { |
|
352 | - $this->dbRow[$pk] = null; |
|
353 | - } |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns raw database row. |
|
359 | - * |
|
360 | - * @return array |
|
361 | - * |
|
362 | - * @throws TDBMMissingReferenceException |
|
363 | - */ |
|
364 | - public function _getDbRow() |
|
365 | - { |
|
366 | - // Let's merge $dbRow and $references |
|
367 | - $dbRow = $this->dbRow; |
|
368 | - |
|
369 | - foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | - // Let's match the name of the columns to the primary key values |
|
371 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | - $localColumns = $fk->getLocalColumns(); |
|
373 | - |
|
374 | - if ($reference !== null) { |
|
375 | - $refDbRows = $reference->_getDbRows(); |
|
376 | - $firstRefDbRow = reset($refDbRows); |
|
377 | - if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | - throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | - } |
|
380 | - $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | - $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | - } |
|
384 | - } else { |
|
385 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | - $dbRow[$localColumns[$i]] = null; |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - return $dbRow; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Returns references array. |
|
396 | - * |
|
397 | - * @return AbstractTDBMObject[] |
|
398 | - */ |
|
399 | - public function _getReferences() |
|
400 | - { |
|
401 | - return $this->references; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Returns the values of the primary key. |
|
406 | - * This is set when the object is in "loaded" state. |
|
407 | - * |
|
408 | - * @return array |
|
409 | - */ |
|
410 | - public function _getPrimaryKeys() |
|
411 | - { |
|
412 | - return $this->primaryKeys; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Sets the values of the primary key. |
|
417 | - * This is set when the object is in "loaded" state. |
|
418 | - * |
|
419 | - * @param array $primaryKeys |
|
420 | - */ |
|
421 | - public function _setPrimaryKeys(array $primaryKeys) |
|
422 | - { |
|
423 | - $this->primaryKeys = $primaryKeys; |
|
424 | - foreach ($this->primaryKeys as $column => $value) { |
|
425 | - $this->dbRow[$column] = $value; |
|
426 | - } |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Returns the TDBMObject this bean is associated to. |
|
431 | - * |
|
432 | - * @return AbstractTDBMObject |
|
433 | - */ |
|
434 | - public function getTDBMObject() |
|
435 | - { |
|
436 | - return $this->object; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Sets the TDBMObject this bean is associated to. |
|
441 | - * Only used when cloning. |
|
442 | - * |
|
443 | - * @param AbstractTDBMObject $object |
|
444 | - */ |
|
445 | - public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | - { |
|
447 | - $this->object = $object; |
|
448 | - } |
|
257 | + $this->dbRow[$var] = $value; |
|
258 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param string $foreignKeyName |
|
266 | + * @param AbstractTDBMObject $bean |
|
267 | + */ |
|
268 | + public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | + { |
|
270 | + $this->references[$foreignKeyName] = $bean; |
|
271 | + |
|
272 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + public function getRef($foreignKeyName) |
|
284 | + { |
|
285 | + if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | + return $this->references[$foreignKeyName]; |
|
287 | + } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | + // If the object is new and has no property, then it has to be empty. |
|
289 | + return; |
|
290 | + } else { |
|
291 | + $this->_dbLoadIfNotLoaded(); |
|
292 | + |
|
293 | + // Let's match the name of the columns to the primary key values |
|
294 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | + |
|
296 | + $values = []; |
|
297 | + foreach ($fk->getLocalColumns() as $column) { |
|
298 | + if (!isset($this->dbRow[$column])) { |
|
299 | + return; |
|
300 | + } |
|
301 | + $values[] = $this->dbRow[$column]; |
|
302 | + } |
|
303 | + |
|
304 | + $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | + |
|
306 | + return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns the name of the table this object comes from. |
|
312 | + * |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function _getDbTableName() |
|
316 | + { |
|
317 | + return $this->dbTableName; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Method used internally by TDBM. You should not use it directly. |
|
322 | + * This method returns the status of the TDBMObject. |
|
323 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | + * |
|
328 | + * @return string |
|
329 | + */ |
|
330 | + public function _getStatus() |
|
331 | + { |
|
332 | + return $this->status; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Override the native php clone function for TDBMObjects. |
|
337 | + */ |
|
338 | + public function __clone() |
|
339 | + { |
|
340 | + // Let's load the row (before we lose the ID!) |
|
341 | + $this->_dbLoadIfNotLoaded(); |
|
342 | + |
|
343 | + //Let's set the status to detached |
|
344 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | + |
|
346 | + $this->primaryKeys = []; |
|
347 | + |
|
348 | + //Now unset the PK from the row |
|
349 | + if ($this->tdbmService) { |
|
350 | + $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | + foreach ($pk_array as $pk) { |
|
352 | + $this->dbRow[$pk] = null; |
|
353 | + } |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns raw database row. |
|
359 | + * |
|
360 | + * @return array |
|
361 | + * |
|
362 | + * @throws TDBMMissingReferenceException |
|
363 | + */ |
|
364 | + public function _getDbRow() |
|
365 | + { |
|
366 | + // Let's merge $dbRow and $references |
|
367 | + $dbRow = $this->dbRow; |
|
368 | + |
|
369 | + foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | + // Let's match the name of the columns to the primary key values |
|
371 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | + $localColumns = $fk->getLocalColumns(); |
|
373 | + |
|
374 | + if ($reference !== null) { |
|
375 | + $refDbRows = $reference->_getDbRows(); |
|
376 | + $firstRefDbRow = reset($refDbRows); |
|
377 | + if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | + throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | + } |
|
380 | + $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | + $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | + } |
|
384 | + } else { |
|
385 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | + $dbRow[$localColumns[$i]] = null; |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + return $dbRow; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Returns references array. |
|
396 | + * |
|
397 | + * @return AbstractTDBMObject[] |
|
398 | + */ |
|
399 | + public function _getReferences() |
|
400 | + { |
|
401 | + return $this->references; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Returns the values of the primary key. |
|
406 | + * This is set when the object is in "loaded" state. |
|
407 | + * |
|
408 | + * @return array |
|
409 | + */ |
|
410 | + public function _getPrimaryKeys() |
|
411 | + { |
|
412 | + return $this->primaryKeys; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Sets the values of the primary key. |
|
417 | + * This is set when the object is in "loaded" state. |
|
418 | + * |
|
419 | + * @param array $primaryKeys |
|
420 | + */ |
|
421 | + public function _setPrimaryKeys(array $primaryKeys) |
|
422 | + { |
|
423 | + $this->primaryKeys = $primaryKeys; |
|
424 | + foreach ($this->primaryKeys as $column => $value) { |
|
425 | + $this->dbRow[$column] = $value; |
|
426 | + } |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Returns the TDBMObject this bean is associated to. |
|
431 | + * |
|
432 | + * @return AbstractTDBMObject |
|
433 | + */ |
|
434 | + public function getTDBMObject() |
|
435 | + { |
|
436 | + return $this->object; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Sets the TDBMObject this bean is associated to. |
|
441 | + * Only used when cloning. |
|
442 | + * |
|
443 | + * @param AbstractTDBMObject $object |
|
444 | + */ |
|
445 | + public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | + { |
|
447 | + $this->object = $object; |
|
448 | + } |
|
449 | 449 | } |
@@ -27,11 +27,11 @@ |
||
27 | 27 | */ |
28 | 28 | final class TDBMObjectStateEnum |
29 | 29 | { |
30 | - const STATE_DETACHED = 'detached'; |
|
31 | - const STATE_NEW = 'new'; |
|
32 | - const STATE_SAVING = 'saving'; |
|
33 | - const STATE_NOT_LOADED = 'not loaded'; |
|
34 | - const STATE_LOADED = 'loaded'; |
|
35 | - const STATE_DIRTY = 'dirty'; |
|
36 | - const STATE_DELETED = 'deleted'; |
|
30 | + const STATE_DETACHED = 'detached'; |
|
31 | + const STATE_NEW = 'new'; |
|
32 | + const STATE_SAVING = 'saving'; |
|
33 | + const STATE_NOT_LOADED = 'not loaded'; |
|
34 | + const STATE_LOADED = 'loaded'; |
|
35 | + const STATE_DIRTY = 'dirty'; |
|
36 | + const STATE_DELETED = 'deleted'; |
|
37 | 37 | } |
@@ -8,7 +8,7 @@ |
||
8 | 8 | <input type="hidden" id="selfedit" name="selfedit" value="<?php echo plainstring_to_htmlprotected($this->selfedit) ?>" /> |
9 | 9 | |
10 | 10 | <?php if (!$this->autoloadDetected) { |
11 | - ?> |
|
11 | + ?> |
|
12 | 12 | <div class="alert">Warning! TDBM could not detect the autoload section of your composer.json file. |
13 | 13 | Unless you are developing your own autoload system, you should configure <strong>composer.json</strong> to <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank">define a source directory and a root namespace using PSR-0</a>.</div> |
14 | 14 | <?php |
@@ -11,136 +11,136 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
13 | 13 | { |
14 | - /** |
|
15 | - * @var Column |
|
16 | - */ |
|
17 | - private $column; |
|
18 | - /** |
|
19 | - * @var NamingStrategyInterface |
|
20 | - */ |
|
21 | - private $namingStrategy; |
|
22 | - |
|
23 | - /** |
|
24 | - * ScalarBeanPropertyDescriptor constructor. |
|
25 | - * @param Table $table |
|
26 | - * @param Column $column |
|
27 | - * @param NamingStrategyInterface $namingStrategy |
|
28 | - */ |
|
29 | - public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
30 | - { |
|
31 | - parent::__construct($table); |
|
32 | - $this->table = $table; |
|
33 | - $this->column = $column; |
|
34 | - $this->namingStrategy = $namingStrategy; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
39 | - * |
|
40 | - * @return ForeignKeyConstraint|null |
|
41 | - */ |
|
42 | - public function getForeignKey() |
|
43 | - { |
|
44 | - return false; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns the param annotation for this property (useful for constructor). |
|
49 | - * |
|
50 | - * @return string |
|
51 | - */ |
|
52 | - public function getParamAnnotation() |
|
53 | - { |
|
54 | - $className = $this->getClassName(); |
|
55 | - $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
56 | - |
|
57 | - $str = ' * @param %s %s'; |
|
58 | - |
|
59 | - return sprintf($str, $paramType, $this->getVariableName()); |
|
60 | - } |
|
61 | - |
|
62 | - public function getUpperCamelCaseName() |
|
63 | - { |
|
64 | - return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
69 | - * |
|
70 | - * @return null|string |
|
71 | - */ |
|
72 | - public function getClassName() |
|
73 | - { |
|
74 | - return; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
79 | - * |
|
80 | - * @return bool |
|
81 | - */ |
|
82 | - public function isCompulsory() |
|
83 | - { |
|
84 | - return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Returns true if the property has a default value. |
|
89 | - * |
|
90 | - * @return bool |
|
91 | - */ |
|
92 | - public function hasDefault() |
|
93 | - { |
|
94 | - return $this->column->getDefault() !== null; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns the code that assigns a value to its default value. |
|
99 | - * |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function assignToDefaultCode() |
|
103 | - { |
|
104 | - $str = ' $this->%s(%s);'; |
|
105 | - |
|
106 | - $default = $this->column->getDefault(); |
|
107 | - |
|
108 | - if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
109 | - $defaultCode = 'new \DateTimeImmutable()'; |
|
110 | - } else { |
|
111 | - $defaultCode = var_export($this->column->getDefault(), true); |
|
112 | - } |
|
113 | - |
|
114 | - return sprintf($str, $this->getSetterName(), $defaultCode); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Returns true if the property is the primary key. |
|
119 | - * |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public function isPrimaryKey() |
|
123 | - { |
|
124 | - return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns the PHP code for getters and setters. |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public function getGetterSetterCode() |
|
133 | - { |
|
134 | - $type = $this->column->getType(); |
|
135 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
136 | - |
|
137 | - $columnGetterName = $this->getGetterName(); |
|
138 | - $columnSetterName = $this->getSetterName(); |
|
139 | - |
|
140 | - // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
141 | - $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
142 | - |
|
143 | - $getterAndSetterCode = ' /** |
|
14 | + /** |
|
15 | + * @var Column |
|
16 | + */ |
|
17 | + private $column; |
|
18 | + /** |
|
19 | + * @var NamingStrategyInterface |
|
20 | + */ |
|
21 | + private $namingStrategy; |
|
22 | + |
|
23 | + /** |
|
24 | + * ScalarBeanPropertyDescriptor constructor. |
|
25 | + * @param Table $table |
|
26 | + * @param Column $column |
|
27 | + * @param NamingStrategyInterface $namingStrategy |
|
28 | + */ |
|
29 | + public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
30 | + { |
|
31 | + parent::__construct($table); |
|
32 | + $this->table = $table; |
|
33 | + $this->column = $column; |
|
34 | + $this->namingStrategy = $namingStrategy; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
39 | + * |
|
40 | + * @return ForeignKeyConstraint|null |
|
41 | + */ |
|
42 | + public function getForeignKey() |
|
43 | + { |
|
44 | + return false; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns the param annotation for this property (useful for constructor). |
|
49 | + * |
|
50 | + * @return string |
|
51 | + */ |
|
52 | + public function getParamAnnotation() |
|
53 | + { |
|
54 | + $className = $this->getClassName(); |
|
55 | + $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
56 | + |
|
57 | + $str = ' * @param %s %s'; |
|
58 | + |
|
59 | + return sprintf($str, $paramType, $this->getVariableName()); |
|
60 | + } |
|
61 | + |
|
62 | + public function getUpperCamelCaseName() |
|
63 | + { |
|
64 | + return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
69 | + * |
|
70 | + * @return null|string |
|
71 | + */ |
|
72 | + public function getClassName() |
|
73 | + { |
|
74 | + return; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
79 | + * |
|
80 | + * @return bool |
|
81 | + */ |
|
82 | + public function isCompulsory() |
|
83 | + { |
|
84 | + return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Returns true if the property has a default value. |
|
89 | + * |
|
90 | + * @return bool |
|
91 | + */ |
|
92 | + public function hasDefault() |
|
93 | + { |
|
94 | + return $this->column->getDefault() !== null; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns the code that assigns a value to its default value. |
|
99 | + * |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function assignToDefaultCode() |
|
103 | + { |
|
104 | + $str = ' $this->%s(%s);'; |
|
105 | + |
|
106 | + $default = $this->column->getDefault(); |
|
107 | + |
|
108 | + if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
109 | + $defaultCode = 'new \DateTimeImmutable()'; |
|
110 | + } else { |
|
111 | + $defaultCode = var_export($this->column->getDefault(), true); |
|
112 | + } |
|
113 | + |
|
114 | + return sprintf($str, $this->getSetterName(), $defaultCode); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Returns true if the property is the primary key. |
|
119 | + * |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public function isPrimaryKey() |
|
123 | + { |
|
124 | + return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns the PHP code for getters and setters. |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public function getGetterSetterCode() |
|
133 | + { |
|
134 | + $type = $this->column->getType(); |
|
135 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
136 | + |
|
137 | + $columnGetterName = $this->getGetterName(); |
|
138 | + $columnSetterName = $this->getSetterName(); |
|
139 | + |
|
140 | + // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
141 | + $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
142 | + |
|
143 | + $getterAndSetterCode = ' /** |
|
144 | 144 | * The getter for the "%s" column. |
145 | 145 | * |
146 | 146 | * @return %s |
@@ -162,54 +162,54 @@ discard block |
||
162 | 162 | |
163 | 163 | '; |
164 | 164 | |
165 | - return sprintf($getterAndSetterCode, |
|
166 | - // Getter |
|
167 | - $this->column->getName(), |
|
168 | - $normalizedType.($isNullable ? '|null' : ''), |
|
169 | - $columnGetterName, |
|
170 | - ($isNullable ? '?' : ''), |
|
171 | - $normalizedType, |
|
172 | - var_export($this->column->getName(), true), |
|
173 | - var_export($this->table->getName(), true), |
|
174 | - // Setter |
|
175 | - $this->column->getName(), |
|
176 | - $normalizedType, |
|
177 | - $this->column->getName(), |
|
178 | - $columnSetterName, |
|
179 | - $this->column->getNotnull() ? '' : '?', |
|
180 | - $normalizedType, |
|
181 | - //$castTo, |
|
182 | - $this->column->getName(), |
|
183 | - var_export($this->column->getName(), true), |
|
184 | - $this->column->getName(), |
|
185 | - var_export($this->table->getName(), true) |
|
186 | - ); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Returns the part of code useful when doing json serialization. |
|
191 | - * |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - public function getJsonSerializeCode() |
|
195 | - { |
|
196 | - $type = $this->column->getType(); |
|
197 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
198 | - |
|
199 | - if ($normalizedType == '\\DateTimeInterface') { |
|
200 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
201 | - } else { |
|
202 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Returns the column name. |
|
208 | - * |
|
209 | - * @return string |
|
210 | - */ |
|
211 | - public function getColumnName() |
|
212 | - { |
|
213 | - return $this->column->getName(); |
|
214 | - } |
|
165 | + return sprintf($getterAndSetterCode, |
|
166 | + // Getter |
|
167 | + $this->column->getName(), |
|
168 | + $normalizedType.($isNullable ? '|null' : ''), |
|
169 | + $columnGetterName, |
|
170 | + ($isNullable ? '?' : ''), |
|
171 | + $normalizedType, |
|
172 | + var_export($this->column->getName(), true), |
|
173 | + var_export($this->table->getName(), true), |
|
174 | + // Setter |
|
175 | + $this->column->getName(), |
|
176 | + $normalizedType, |
|
177 | + $this->column->getName(), |
|
178 | + $columnSetterName, |
|
179 | + $this->column->getNotnull() ? '' : '?', |
|
180 | + $normalizedType, |
|
181 | + //$castTo, |
|
182 | + $this->column->getName(), |
|
183 | + var_export($this->column->getName(), true), |
|
184 | + $this->column->getName(), |
|
185 | + var_export($this->table->getName(), true) |
|
186 | + ); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Returns the part of code useful when doing json serialization. |
|
191 | + * |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + public function getJsonSerializeCode() |
|
195 | + { |
|
196 | + $type = $this->column->getType(); |
|
197 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
198 | + |
|
199 | + if ($normalizedType == '\\DateTimeInterface') { |
|
200 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
201 | + } else { |
|
202 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Returns the column name. |
|
208 | + * |
|
209 | + * @return string |
|
210 | + */ |
|
211 | + public function getColumnName() |
|
212 | + { |
|
213 | + return $this->column->getName(); |
|
214 | + } |
|
215 | 215 | } |
@@ -7,105 +7,105 @@ discard block |
||
7 | 7 | |
8 | 8 | class PivotTableMethodsDescriptor implements MethodDescriptorInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Table |
|
12 | - */ |
|
13 | - private $pivotTable; |
|
14 | - |
|
15 | - private $useAlternateName = false; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var ForeignKeyConstraint |
|
19 | - */ |
|
20 | - private $localFk; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ForeignKeyConstraint |
|
24 | - */ |
|
25 | - private $remoteFk; |
|
26 | - /** |
|
27 | - * @var NamingStrategyInterface |
|
28 | - */ |
|
29 | - private $namingStrategy; |
|
30 | - |
|
31 | - /** |
|
32 | - * @param Table $pivotTable The pivot table |
|
33 | - * @param ForeignKeyConstraint $localFk |
|
34 | - * @param ForeignKeyConstraint $remoteFk |
|
35 | - * @param NamingStrategyInterface $namingStrategy |
|
36 | - */ |
|
37 | - public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
38 | - { |
|
39 | - $this->pivotTable = $pivotTable; |
|
40 | - $this->localFk = $localFk; |
|
41 | - $this->remoteFk = $remoteFk; |
|
42 | - $this->namingStrategy = $namingStrategy; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Requests the use of an alternative name for this method. |
|
47 | - */ |
|
48 | - public function useAlternativeName() |
|
49 | - { |
|
50 | - $this->useAlternateName = true; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Returns the name of the method to be generated. |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - public function getName() : string |
|
59 | - { |
|
60 | - if (!$this->useAlternateName) { |
|
61 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
62 | - } else { |
|
63 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Returns the plural name. |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - private function getPluralName() : string |
|
73 | - { |
|
74 | - if (!$this->useAlternateName) { |
|
75 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
76 | - } else { |
|
77 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
78 | - } |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Returns the singular name. |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - private function getSingularName() : string |
|
87 | - { |
|
88 | - if (!$this->useAlternateName) { |
|
89 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
90 | - } else { |
|
91 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Returns the code of the method. |
|
97 | - * |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - public function getCode() : string |
|
101 | - { |
|
102 | - $singularName = $this->getSingularName(); |
|
103 | - $pluralName = $this->getPluralName(); |
|
104 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
105 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
106 | - $pluralVariableName = $variableName.'s'; |
|
107 | - |
|
108 | - $str = ' /** |
|
10 | + /** |
|
11 | + * @var Table |
|
12 | + */ |
|
13 | + private $pivotTable; |
|
14 | + |
|
15 | + private $useAlternateName = false; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var ForeignKeyConstraint |
|
19 | + */ |
|
20 | + private $localFk; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ForeignKeyConstraint |
|
24 | + */ |
|
25 | + private $remoteFk; |
|
26 | + /** |
|
27 | + * @var NamingStrategyInterface |
|
28 | + */ |
|
29 | + private $namingStrategy; |
|
30 | + |
|
31 | + /** |
|
32 | + * @param Table $pivotTable The pivot table |
|
33 | + * @param ForeignKeyConstraint $localFk |
|
34 | + * @param ForeignKeyConstraint $remoteFk |
|
35 | + * @param NamingStrategyInterface $namingStrategy |
|
36 | + */ |
|
37 | + public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
38 | + { |
|
39 | + $this->pivotTable = $pivotTable; |
|
40 | + $this->localFk = $localFk; |
|
41 | + $this->remoteFk = $remoteFk; |
|
42 | + $this->namingStrategy = $namingStrategy; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Requests the use of an alternative name for this method. |
|
47 | + */ |
|
48 | + public function useAlternativeName() |
|
49 | + { |
|
50 | + $this->useAlternateName = true; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Returns the name of the method to be generated. |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + public function getName() : string |
|
59 | + { |
|
60 | + if (!$this->useAlternateName) { |
|
61 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
62 | + } else { |
|
63 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Returns the plural name. |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + private function getPluralName() : string |
|
73 | + { |
|
74 | + if (!$this->useAlternateName) { |
|
75 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
76 | + } else { |
|
77 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
78 | + } |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Returns the singular name. |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + private function getSingularName() : string |
|
87 | + { |
|
88 | + if (!$this->useAlternateName) { |
|
89 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
90 | + } else { |
|
91 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Returns the code of the method. |
|
97 | + * |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + public function getCode() : string |
|
101 | + { |
|
102 | + $singularName = $this->getSingularName(); |
|
103 | + $pluralName = $this->getPluralName(); |
|
104 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
105 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
106 | + $pluralVariableName = $variableName.'s'; |
|
107 | + |
|
108 | + $str = ' /** |
|
109 | 109 | * Returns the list of %s associated to this bean via the %s pivot table. |
110 | 110 | * |
111 | 111 | * @return %s[] |
@@ -116,9 +116,9 @@ discard block |
||
116 | 116 | } |
117 | 117 | '; |
118 | 118 | |
119 | - $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
119 | + $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
120 | 120 | |
121 | - $str = ' /** |
|
121 | + $str = ' /** |
|
122 | 122 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
123 | 123 | * |
124 | 124 | * @param %s %s |
@@ -129,9 +129,9 @@ discard block |
||
129 | 129 | } |
130 | 130 | '; |
131 | 131 | |
132 | - $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
132 | + $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
133 | 133 | |
134 | - $str = ' /** |
|
134 | + $str = ' /** |
|
135 | 135 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
136 | 136 | * |
137 | 137 | * @param %s %s |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | } |
143 | 143 | '; |
144 | 144 | |
145 | - $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
145 | + $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
146 | 146 | |
147 | - $str = ' /** |
|
147 | + $str = ' /** |
|
148 | 148 | * Returns whether this bean is associated with %s via the %s pivot table. |
149 | 149 | * |
150 | 150 | * @param %s %s |
@@ -156,9 +156,9 @@ discard block |
||
156 | 156 | } |
157 | 157 | '; |
158 | 158 | |
159 | - $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
159 | + $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
160 | 160 | |
161 | - $str = ' /** |
|
161 | + $str = ' /** |
|
162 | 162 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
163 | 163 | * Exiting relationships will be removed and replaced by the provided relationships. |
164 | 164 | * |
@@ -170,38 +170,38 @@ discard block |
||
170 | 170 | } |
171 | 171 | '; |
172 | 172 | |
173 | - $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
174 | - |
|
175 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
176 | - |
|
177 | - return $code; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Returns an array of classes that needs a "use" for this method. |
|
182 | - * |
|
183 | - * @return string[] |
|
184 | - */ |
|
185 | - public function getUsedClasses() : array |
|
186 | - { |
|
187 | - return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Returns the code to past in jsonSerialize. |
|
192 | - * |
|
193 | - * @return string |
|
194 | - */ |
|
195 | - public function getJsonSerializeCode() : string |
|
196 | - { |
|
197 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
198 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
199 | - |
|
200 | - return ' if (!$stopRecursion) { |
|
173 | + $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
174 | + |
|
175 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
176 | + |
|
177 | + return $code; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Returns an array of classes that needs a "use" for this method. |
|
182 | + * |
|
183 | + * @return string[] |
|
184 | + */ |
|
185 | + public function getUsedClasses() : array |
|
186 | + { |
|
187 | + return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Returns the code to past in jsonSerialize. |
|
192 | + * |
|
193 | + * @return string |
|
194 | + */ |
|
195 | + public function getJsonSerializeCode() : string |
|
196 | + { |
|
197 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
198 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
199 | + |
|
200 | + return ' if (!$stopRecursion) { |
|
201 | 201 | $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
202 | 202 | return '.$variableName.'->jsonSerialize(true); |
203 | 203 | }, $this->'.$this->getName().'()); |
204 | 204 | } |
205 | 205 | '; |
206 | - } |
|
206 | + } |
|
207 | 207 | } |
@@ -7,28 +7,28 @@ |
||
7 | 7 | |
8 | 8 | class GeneratorEventDispatcher implements GeneratorListenerInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * @var GeneratorListenerInterface[] |
|
12 | - */ |
|
13 | - private $listeners; |
|
10 | + /** |
|
11 | + * @var GeneratorListenerInterface[] |
|
12 | + */ |
|
13 | + private $listeners; |
|
14 | 14 | |
15 | - /** |
|
16 | - * GeneratorEventDispatcher constructor. |
|
17 | - * @param GeneratorListenerInterface[] $listeners |
|
18 | - */ |
|
19 | - public function __construct(array $listeners) |
|
20 | - { |
|
21 | - $this->listeners = $listeners; |
|
22 | - } |
|
15 | + /** |
|
16 | + * GeneratorEventDispatcher constructor. |
|
17 | + * @param GeneratorListenerInterface[] $listeners |
|
18 | + */ |
|
19 | + public function __construct(array $listeners) |
|
20 | + { |
|
21 | + $this->listeners = $listeners; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param ConfigurationInterface $configuration |
|
26 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
27 | - */ |
|
28 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
29 | - { |
|
30 | - foreach ($this->listeners as $listener) { |
|
31 | - $listener->onGenerate($configuration, $beanDescriptors); |
|
32 | - } |
|
33 | - } |
|
24 | + /** |
|
25 | + * @param ConfigurationInterface $configuration |
|
26 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
27 | + */ |
|
28 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
29 | + { |
|
30 | + foreach ($this->listeners as $listener) { |
|
31 | + $listener->onGenerate($configuration, $beanDescriptors); |
|
32 | + } |
|
33 | + } |
|
34 | 34 | } |
@@ -12,71 +12,71 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var ForeignKeyConstraint |
|
17 | - */ |
|
18 | - private $fk; |
|
19 | - |
|
20 | - private $useAlternateName = false; |
|
21 | - /** |
|
22 | - * @var Table |
|
23 | - */ |
|
24 | - private $mainTable; |
|
25 | - /** |
|
26 | - * @var NamingStrategyInterface |
|
27 | - */ |
|
28 | - private $namingStrategy; |
|
29 | - |
|
30 | - /** |
|
31 | - * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
32 | - * @param Table $mainTable The main table that is pointed to |
|
33 | - * @param NamingStrategyInterface $namingStrategy |
|
34 | - */ |
|
35 | - public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
36 | - { |
|
37 | - $this->fk = $fk; |
|
38 | - $this->mainTable = $mainTable; |
|
39 | - $this->namingStrategy = $namingStrategy; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Returns the name of the method to be generated. |
|
44 | - * |
|
45 | - * @return string |
|
46 | - */ |
|
47 | - public function getName() : string |
|
48 | - { |
|
49 | - if (!$this->useAlternateName) { |
|
50 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
51 | - } else { |
|
52 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
53 | - |
|
54 | - $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
55 | - |
|
56 | - $methodName .= implode('And', $camelizedColumns); |
|
57 | - |
|
58 | - return $methodName; |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Requests the use of an alternative name for this method. |
|
64 | - */ |
|
65 | - public function useAlternativeName() |
|
66 | - { |
|
67 | - $this->useAlternateName = true; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the code of the method. |
|
72 | - * |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function getCode() : string |
|
76 | - { |
|
77 | - $code = ''; |
|
78 | - |
|
79 | - $getterCode = ' /** |
|
15 | + /** |
|
16 | + * @var ForeignKeyConstraint |
|
17 | + */ |
|
18 | + private $fk; |
|
19 | + |
|
20 | + private $useAlternateName = false; |
|
21 | + /** |
|
22 | + * @var Table |
|
23 | + */ |
|
24 | + private $mainTable; |
|
25 | + /** |
|
26 | + * @var NamingStrategyInterface |
|
27 | + */ |
|
28 | + private $namingStrategy; |
|
29 | + |
|
30 | + /** |
|
31 | + * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
32 | + * @param Table $mainTable The main table that is pointed to |
|
33 | + * @param NamingStrategyInterface $namingStrategy |
|
34 | + */ |
|
35 | + public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
36 | + { |
|
37 | + $this->fk = $fk; |
|
38 | + $this->mainTable = $mainTable; |
|
39 | + $this->namingStrategy = $namingStrategy; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Returns the name of the method to be generated. |
|
44 | + * |
|
45 | + * @return string |
|
46 | + */ |
|
47 | + public function getName() : string |
|
48 | + { |
|
49 | + if (!$this->useAlternateName) { |
|
50 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
51 | + } else { |
|
52 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
53 | + |
|
54 | + $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
55 | + |
|
56 | + $methodName .= implode('And', $camelizedColumns); |
|
57 | + |
|
58 | + return $methodName; |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Requests the use of an alternative name for this method. |
|
64 | + */ |
|
65 | + public function useAlternativeName() |
|
66 | + { |
|
67 | + $this->useAlternateName = true; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the code of the method. |
|
72 | + * |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function getCode() : string |
|
76 | + { |
|
77 | + $code = ''; |
|
78 | + |
|
79 | + $getterCode = ' /** |
|
80 | 80 | * Returns the list of %s pointing to this bean via the %s column. |
81 | 81 | * |
82 | 82 | * @return %s[]|AlterableResultIterator |
@@ -88,55 +88,55 @@ discard block |
||
88 | 88 | |
89 | 89 | '; |
90 | 90 | |
91 | - $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
92 | - $code .= sprintf($getterCode, |
|
93 | - $beanClass, |
|
94 | - implode(', ', $this->fk->getColumns()), |
|
95 | - $beanClass, |
|
96 | - $this->getName(), |
|
97 | - var_export($this->fk->getLocalTableName(), true), |
|
98 | - var_export($this->fk->getName(), true), |
|
99 | - var_export($this->fk->getLocalTableName(), true), |
|
100 | - $this->getFilters($this->fk) |
|
101 | - ); |
|
102 | - |
|
103 | - return $code; |
|
104 | - } |
|
105 | - |
|
106 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
107 | - { |
|
108 | - $counter = 0; |
|
109 | - $parameters = []; |
|
110 | - |
|
111 | - $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
112 | - |
|
113 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
114 | - $pkColumn = $pkColumns[$counter]; |
|
115 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
116 | - ++$counter; |
|
117 | - } |
|
118 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
119 | - |
|
120 | - return $parametersCode; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns an array of classes that needs a "use" for this method. |
|
125 | - * |
|
126 | - * @return string[] |
|
127 | - */ |
|
128 | - public function getUsedClasses() : array |
|
129 | - { |
|
130 | - return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns the code to past in jsonSerialize. |
|
135 | - * |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function getJsonSerializeCode() : string |
|
139 | - { |
|
140 | - return ''; |
|
141 | - } |
|
91 | + $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
92 | + $code .= sprintf($getterCode, |
|
93 | + $beanClass, |
|
94 | + implode(', ', $this->fk->getColumns()), |
|
95 | + $beanClass, |
|
96 | + $this->getName(), |
|
97 | + var_export($this->fk->getLocalTableName(), true), |
|
98 | + var_export($this->fk->getName(), true), |
|
99 | + var_export($this->fk->getLocalTableName(), true), |
|
100 | + $this->getFilters($this->fk) |
|
101 | + ); |
|
102 | + |
|
103 | + return $code; |
|
104 | + } |
|
105 | + |
|
106 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
107 | + { |
|
108 | + $counter = 0; |
|
109 | + $parameters = []; |
|
110 | + |
|
111 | + $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
112 | + |
|
113 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
114 | + $pkColumn = $pkColumns[$counter]; |
|
115 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
116 | + ++$counter; |
|
117 | + } |
|
118 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
119 | + |
|
120 | + return $parametersCode; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns an array of classes that needs a "use" for this method. |
|
125 | + * |
|
126 | + * @return string[] |
|
127 | + */ |
|
128 | + public function getUsedClasses() : array |
|
129 | + { |
|
130 | + return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns the code to past in jsonSerialize. |
|
135 | + * |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function getJsonSerializeCode() : string |
|
139 | + { |
|
140 | + return ''; |
|
141 | + } |
|
142 | 142 | } |
@@ -11,12 +11,12 @@ |
||
11 | 11 | class VoidListener implements GeneratorListenerInterface |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @param ConfigurationInterface $configuration |
|
16 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
17 | - */ |
|
18 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
19 | - { |
|
20 | - // Let's do nothing. |
|
21 | - } |
|
14 | + /** |
|
15 | + * @param ConfigurationInterface $configuration |
|
16 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
17 | + */ |
|
18 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
19 | + { |
|
20 | + // Let's do nothing. |
|
21 | + } |
|
22 | 22 | } |
@@ -12,169 +12,169 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class ObjectBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
14 | 14 | { |
15 | - /** |
|
16 | - * @var ForeignKeyConstraint |
|
17 | - */ |
|
18 | - private $foreignKey; |
|
19 | - |
|
20 | - /** |
|
21 | - * @var SchemaAnalyzer |
|
22 | - */ |
|
23 | - private $schemaAnalyzer; |
|
24 | - /** |
|
25 | - * @var NamingStrategyInterface |
|
26 | - */ |
|
27 | - private $namingStrategy; |
|
28 | - |
|
29 | - /** |
|
30 | - * ObjectBeanPropertyDescriptor constructor. |
|
31 | - * @param Table $table |
|
32 | - * @param ForeignKeyConstraint $foreignKey |
|
33 | - * @param SchemaAnalyzer $schemaAnalyzer |
|
34 | - * @param NamingStrategyInterface $namingStrategy |
|
35 | - */ |
|
36 | - public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
37 | - { |
|
38 | - parent::__construct($table); |
|
39 | - $this->foreignKey = $foreignKey; |
|
40 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
41 | - $this->namingStrategy = $namingStrategy; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Returns the foreignkey the column is part of, if any. null otherwise. |
|
46 | - * |
|
47 | - * @return ForeignKeyConstraint|null |
|
48 | - */ |
|
49 | - public function getForeignKey() |
|
50 | - { |
|
51 | - return $this->foreignKey; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
56 | - * |
|
57 | - * @return null|string |
|
58 | - */ |
|
59 | - public function getClassName() |
|
60 | - { |
|
61 | - return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Returns the param annotation for this property (useful for constructor). |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getParamAnnotation() |
|
70 | - { |
|
71 | - $str = ' * @param %s %s'; |
|
72 | - |
|
73 | - return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
74 | - } |
|
75 | - |
|
76 | - public function getUpperCamelCaseName() |
|
77 | - { |
|
78 | - // First, are there many column or only one? |
|
79 | - // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
80 | - if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
81 | - $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
82 | - if ($this->alternativeName) { |
|
83 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
84 | - |
|
85 | - $name .= 'By'.implode('And', $camelizedColumns); |
|
86 | - } |
|
87 | - } else { |
|
88 | - $column = $this->foreignKey->getLocalColumns()[0]; |
|
89 | - // Let's remove any _id or id_. |
|
90 | - if (strpos(strtolower($column), 'id_') === 0) { |
|
91 | - $column = substr($column, 3); |
|
92 | - } |
|
93 | - if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
94 | - $column = substr($column, 0, strlen($column) - 3); |
|
95 | - } |
|
96 | - $name = TDBMDaoGenerator::toCamelCase($column); |
|
97 | - if ($this->alternativeName) { |
|
98 | - $name .= 'Object'; |
|
99 | - } |
|
100 | - } |
|
101 | - |
|
102 | - return $name; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - public function isCompulsory() |
|
111 | - { |
|
112 | - // Are all columns nullable? |
|
113 | - $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
114 | - |
|
115 | - foreach ($localColumnNames as $name) { |
|
116 | - $column = $this->table->getColumn($name); |
|
117 | - if ($column->getNotnull()) { |
|
118 | - return true; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return false; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns true if the property has a default value. |
|
127 | - * |
|
128 | - * @return bool |
|
129 | - */ |
|
130 | - public function hasDefault() |
|
131 | - { |
|
132 | - return false; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Returns the code that assigns a value to its default value. |
|
137 | - * |
|
138 | - * @return string |
|
139 | - * |
|
140 | - * @throws \TDBMException |
|
141 | - */ |
|
142 | - public function assignToDefaultCode() |
|
143 | - { |
|
144 | - throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Returns true if the property is the primary key. |
|
149 | - * |
|
150 | - * @return bool |
|
151 | - */ |
|
152 | - public function isPrimaryKey() |
|
153 | - { |
|
154 | - $fkColumns = $this->foreignKey->getLocalColumns(); |
|
155 | - sort($fkColumns); |
|
156 | - |
|
157 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
158 | - sort($pkColumns); |
|
159 | - |
|
160 | - return $fkColumns == $pkColumns; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Returns the PHP code for getters and setters. |
|
165 | - * |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - public function getGetterSetterCode() |
|
169 | - { |
|
170 | - $tableName = $this->table->getName(); |
|
171 | - $getterName = $this->getGetterName(); |
|
172 | - $setterName = $this->getSetterName(); |
|
173 | - $isNullable = !$this->isCompulsory(); |
|
174 | - |
|
175 | - $referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
176 | - |
|
177 | - $str = ' /** |
|
15 | + /** |
|
16 | + * @var ForeignKeyConstraint |
|
17 | + */ |
|
18 | + private $foreignKey; |
|
19 | + |
|
20 | + /** |
|
21 | + * @var SchemaAnalyzer |
|
22 | + */ |
|
23 | + private $schemaAnalyzer; |
|
24 | + /** |
|
25 | + * @var NamingStrategyInterface |
|
26 | + */ |
|
27 | + private $namingStrategy; |
|
28 | + |
|
29 | + /** |
|
30 | + * ObjectBeanPropertyDescriptor constructor. |
|
31 | + * @param Table $table |
|
32 | + * @param ForeignKeyConstraint $foreignKey |
|
33 | + * @param SchemaAnalyzer $schemaAnalyzer |
|
34 | + * @param NamingStrategyInterface $namingStrategy |
|
35 | + */ |
|
36 | + public function __construct(Table $table, ForeignKeyConstraint $foreignKey, SchemaAnalyzer $schemaAnalyzer, NamingStrategyInterface $namingStrategy) |
|
37 | + { |
|
38 | + parent::__construct($table); |
|
39 | + $this->foreignKey = $foreignKey; |
|
40 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
41 | + $this->namingStrategy = $namingStrategy; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Returns the foreignkey the column is part of, if any. null otherwise. |
|
46 | + * |
|
47 | + * @return ForeignKeyConstraint|null |
|
48 | + */ |
|
49 | + public function getForeignKey() |
|
50 | + { |
|
51 | + return $this->foreignKey; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
56 | + * |
|
57 | + * @return null|string |
|
58 | + */ |
|
59 | + public function getClassName() |
|
60 | + { |
|
61 | + return $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Returns the param annotation for this property (useful for constructor). |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getParamAnnotation() |
|
70 | + { |
|
71 | + $str = ' * @param %s %s'; |
|
72 | + |
|
73 | + return sprintf($str, $this->getClassName(), $this->getVariableName()); |
|
74 | + } |
|
75 | + |
|
76 | + public function getUpperCamelCaseName() |
|
77 | + { |
|
78 | + // First, are there many column or only one? |
|
79 | + // If one column, we name the setter after it. Otherwise, we name the setter after the table name |
|
80 | + if (count($this->foreignKey->getLocalColumns()) > 1) { |
|
81 | + $name = TDBMDaoGenerator::toSingular(TDBMDaoGenerator::toCamelCase($this->foreignKey->getForeignTableName())); |
|
82 | + if ($this->alternativeName) { |
|
83 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $this->foreignKey->getLocalColumns()); |
|
84 | + |
|
85 | + $name .= 'By'.implode('And', $camelizedColumns); |
|
86 | + } |
|
87 | + } else { |
|
88 | + $column = $this->foreignKey->getLocalColumns()[0]; |
|
89 | + // Let's remove any _id or id_. |
|
90 | + if (strpos(strtolower($column), 'id_') === 0) { |
|
91 | + $column = substr($column, 3); |
|
92 | + } |
|
93 | + if (strrpos(strtolower($column), '_id') === strlen($column) - 3) { |
|
94 | + $column = substr($column, 0, strlen($column) - 3); |
|
95 | + } |
|
96 | + $name = TDBMDaoGenerator::toCamelCase($column); |
|
97 | + if ($this->alternativeName) { |
|
98 | + $name .= 'Object'; |
|
99 | + } |
|
100 | + } |
|
101 | + |
|
102 | + return $name; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + public function isCompulsory() |
|
111 | + { |
|
112 | + // Are all columns nullable? |
|
113 | + $localColumnNames = $this->foreignKey->getLocalColumns(); |
|
114 | + |
|
115 | + foreach ($localColumnNames as $name) { |
|
116 | + $column = $this->table->getColumn($name); |
|
117 | + if ($column->getNotnull()) { |
|
118 | + return true; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return false; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns true if the property has a default value. |
|
127 | + * |
|
128 | + * @return bool |
|
129 | + */ |
|
130 | + public function hasDefault() |
|
131 | + { |
|
132 | + return false; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Returns the code that assigns a value to its default value. |
|
137 | + * |
|
138 | + * @return string |
|
139 | + * |
|
140 | + * @throws \TDBMException |
|
141 | + */ |
|
142 | + public function assignToDefaultCode() |
|
143 | + { |
|
144 | + throw new \TDBMException('Foreign key based properties cannot be assigned a default value.'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Returns true if the property is the primary key. |
|
149 | + * |
|
150 | + * @return bool |
|
151 | + */ |
|
152 | + public function isPrimaryKey() |
|
153 | + { |
|
154 | + $fkColumns = $this->foreignKey->getLocalColumns(); |
|
155 | + sort($fkColumns); |
|
156 | + |
|
157 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
158 | + sort($pkColumns); |
|
159 | + |
|
160 | + return $fkColumns == $pkColumns; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Returns the PHP code for getters and setters. |
|
165 | + * |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + public function getGetterSetterCode() |
|
169 | + { |
|
170 | + $tableName = $this->table->getName(); |
|
171 | + $getterName = $this->getGetterName(); |
|
172 | + $setterName = $this->getSetterName(); |
|
173 | + $isNullable = !$this->isCompulsory(); |
|
174 | + |
|
175 | + $referencedBeanName = $this->namingStrategy->getBeanClassName($this->foreignKey->getForeignTableName()); |
|
176 | + |
|
177 | + $str = ' /** |
|
178 | 178 | * Returns the '.$referencedBeanName.' object bound to this object via the '.implode(' and ', $this->foreignKey->getLocalColumns()).' column. |
179 | 179 | * |
180 | 180 | * @return '.$referencedBeanName.($isNullable?'|null':'').' |
@@ -196,20 +196,20 @@ discard block |
||
196 | 196 | |
197 | 197 | '; |
198 | 198 | |
199 | - return $str; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns the part of code useful when doing json serialization. |
|
204 | - * |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - public function getJsonSerializeCode() |
|
208 | - { |
|
209 | - return ' if (!$stopRecursion) { |
|
199 | + return $str; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns the part of code useful when doing json serialization. |
|
204 | + * |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + public function getJsonSerializeCode() |
|
208 | + { |
|
209 | + return ' if (!$stopRecursion) { |
|
210 | 210 | $object = $this->'.$this->getGetterName().'(); |
211 | 211 | $array['.var_export($this->getLowerCamelCaseName(), true).'] = $object ? $object->jsonSerialize(true) : null; |
212 | 212 | } |
213 | 213 | '; |
214 | - } |
|
214 | + } |
|
215 | 215 | } |