Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like TDBMService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TDBMService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class TDBMService { |
||
44 | |||
45 | const MODE_CURSOR = 1; |
||
46 | const MODE_ARRAY = 2; |
||
47 | |||
48 | /** |
||
49 | * The database connection. |
||
50 | * |
||
51 | * @var Connection |
||
52 | */ |
||
53 | private $connection; |
||
54 | |||
55 | /** |
||
56 | * @var SchemaAnalyzer |
||
57 | */ |
||
58 | private $schemaAnalyzer; |
||
59 | |||
60 | /** |
||
61 | * @var MagicQuery |
||
62 | */ |
||
63 | private $magicQuery; |
||
64 | |||
65 | /** |
||
66 | * @var TDBMSchemaAnalyzer |
||
67 | */ |
||
68 | private $tdbmSchemaAnalyzer; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $cachePrefix; |
||
74 | |||
75 | /** |
||
76 | * The default autosave mode for the objects |
||
77 | * True to automatically save the object. |
||
78 | * If false, the user must explicitly call the save() method to save the object. |
||
79 | * |
||
80 | * @var boolean |
||
81 | */ |
||
82 | //private $autosave_default = false; |
||
|
|||
83 | |||
84 | /** |
||
85 | * Cache of table of primary keys. |
||
86 | * Primary keys are stored by tables, as an array of column. |
||
87 | * For instance $primary_key['my_table'][0] will return the first column of the primary key of table 'my_table'. |
||
88 | * |
||
89 | * @var string[] |
||
90 | */ |
||
91 | private $primaryKeysColumns; |
||
92 | |||
93 | /** |
||
94 | * Service storing objects in memory. |
||
95 | * Access is done by table name and then by primary key. |
||
96 | * If the primary key is split on several columns, access is done by an array of columns, serialized. |
||
97 | * |
||
98 | * @var StandardObjectStorage|WeakrefObjectStorage |
||
99 | */ |
||
100 | private $objectStorage; |
||
101 | |||
102 | /** |
||
103 | * The fetch mode of the result sets returned by `getObjects`. |
||
104 | * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY or TDBMObjectArray::MODE_COMPATIBLE_ARRAY |
||
105 | * |
||
106 | * In 'MODE_ARRAY' mode (default), the result is an array. Use this mode by default (unless the list returned is very big). |
||
107 | * In 'MODE_CURSOR' mode, the result is a Generator which is an iterable collection that can be scanned only once (only one "foreach") on it, |
||
108 | * and it cannot be accessed via key. Use this mode for large datasets processed by batch. |
||
109 | * In 'MODE_COMPATIBLE_ARRAY' mode, the result is an old TDBMObjectArray (used up to TDBM 3.2). |
||
110 | * You can access the array by key, or using foreach, several times. |
||
111 | * |
||
112 | * @var int |
||
113 | */ |
||
114 | private $mode = self::MODE_ARRAY; |
||
115 | |||
116 | /** |
||
117 | * Table of new objects not yet inserted in database or objects modified that must be saved. |
||
118 | * @var \SplObjectStorage of DbRow objects |
||
119 | */ |
||
120 | private $toSaveObjects; |
||
121 | |||
122 | /// The timestamp of the script startup. Useful to stop execution before time limit is reached and display useful error message. |
||
123 | public static $script_start_up_time; |
||
124 | |||
125 | /** |
||
126 | * The content of the cache variable. |
||
127 | * |
||
128 | * @var array<string, mixed> |
||
129 | */ |
||
130 | private $cache; |
||
131 | |||
132 | private $cacheKey = "__TDBM_Cache__"; |
||
133 | |||
134 | /** |
||
135 | * Map associating a table name to a fully qualified Bean class name |
||
136 | * @var array |
||
137 | */ |
||
138 | private $tableToBeanMap = []; |
||
139 | |||
140 | /** |
||
141 | * @var \ReflectionClass[] |
||
142 | */ |
||
143 | private $reflectionClassCache; |
||
144 | |||
145 | /** |
||
146 | * @param Connection $connection The DBAL DB connection to use |
||
147 | * @param Cache|null $cache A cache service to be used |
||
148 | * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
||
149 | * Will be automatically created if not passed. |
||
150 | */ |
||
151 | public function __construct(Connection $connection, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null) { |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Returns the object used to connect to the database. |
||
184 | * |
||
185 | * @return Connection |
||
186 | */ |
||
187 | public function getConnection() { |
||
190 | |||
191 | /** |
||
192 | * Creates a unique cache key for the current connection. |
||
193 | * @return string |
||
194 | */ |
||
195 | private function getConnectionUniqueId() { |
||
198 | |||
199 | /** |
||
200 | * Returns true if the objects will save automatically by default, |
||
201 | * false if an explicit call to save() is required. |
||
202 | * |
||
203 | * The behaviour can be overloaded by setAutoSaveMode on each object. |
||
204 | * |
||
205 | * @return boolean |
||
206 | */ |
||
207 | /*public function getDefaultAutoSaveMode() { |
||
208 | return $this->autosave_default; |
||
209 | }*/ |
||
210 | |||
211 | /** |
||
212 | * Sets the autosave mode: |
||
213 | * true if the object will save automatically, |
||
214 | * false if an explicit call to save() is required. |
||
215 | * |
||
216 | * @Compulsory |
||
217 | * @param boolean $autoSave |
||
218 | */ |
||
219 | /*public function setDefaultAutoSaveMode($autoSave = true) { |
||
220 | $this->autosave_default = $autoSave; |
||
221 | }*/ |
||
222 | |||
223 | /** |
||
224 | * Sets the fetch mode of the result sets returned by `getObjects`. |
||
225 | * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY or TDBMObjectArray::MODE_COMPATIBLE_ARRAY |
||
226 | * |
||
227 | * In 'MODE_ARRAY' mode (default), the result is a ResultIterator object that behaves like an array. Use this mode by default (unless the list returned is very big). |
||
228 | * In 'MODE_CURSOR' mode, the result is a ResultIterator object. If you scan it many times (by calling several time a foreach loop), the query will be run |
||
229 | * several times. In cursor mode, you cannot access the result set by key. Use this mode for large datasets processed by batch. |
||
230 | * |
||
231 | * @param int $mode |
||
232 | */ |
||
233 | public function setFetchMode($mode) { |
||
240 | |||
241 | /** |
||
242 | * Returns a TDBMObject associated from table "$table_name". |
||
243 | * If the $filters parameter is an int/string, the object returned will be the object whose primary key = $filters. |
||
244 | * $filters can also be a set of TDBM_Filters (see the getObjects method for more details). |
||
245 | * |
||
246 | * For instance, if there is a table 'users', with a primary key on column 'user_id' and a column 'user_name', then |
||
247 | * $user = $tdbmService->getObject('users',1); |
||
248 | * echo $user->name; |
||
249 | * will return the name of the user whose user_id is one. |
||
250 | * |
||
251 | * If a table has a primary key over several columns, you should pass to $id an array containing the the value of the various columns. |
||
252 | * For instance: |
||
253 | * $group = $tdbmService->getObject('groups',array(1,2)); |
||
254 | * |
||
255 | * Note that TDBMObject performs caching for you. If you get twice the same object, the reference of the object you will get |
||
256 | * will be the same. |
||
257 | * |
||
258 | * For instance: |
||
259 | * $user1 = $tdbmService->getObject('users',1); |
||
260 | * $user2 = $tdbmService->getObject('users',1); |
||
261 | * $user1->name = 'John Doe'; |
||
262 | * echo $user2->name; |
||
263 | * will return 'John Doe'. |
||
264 | * |
||
265 | * You can use filters instead of passing the primary key. For instance: |
||
266 | * $user = $tdbmService->getObject('users',new EqualFilter('users', 'login', 'jdoe')); |
||
267 | * This will return the user whose login is 'jdoe'. |
||
268 | * Please note that if 2 users have the jdoe login in database, the method will throw a TDBM_DuplicateRowException. |
||
269 | * |
||
270 | * Also, you can specify the return class for the object (provided the return class extends TDBMObject). |
||
271 | * For instance: |
||
272 | * $user = $tdbmService->getObject('users',1,'User'); |
||
273 | * will return an object from the "User" class. The "User" class must extend the "TDBMObject" class. |
||
274 | * Please be sure not to override any method or any property unless you perfectly know what you are doing! |
||
275 | * |
||
276 | * @param string $table_name The name of the table we retrieve an object from. |
||
277 | * @param mixed $filters If the filter is a string/integer, it will be considered as the id of the object (the value of the primary key). Otherwise, it can be a filter bag (see the filterbag parameter of the getObjects method for more details about filter bags) |
||
278 | * @param string $className Optional: The name of the class to instanciate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned. |
||
279 | * @param boolean $lazy_loading If set to true, and if the primary key is passed in parameter of getObject, the object will not be queried in database. It will be queried when you first try to access a column. If at that time the object cannot be found in database, an exception will be thrown. |
||
280 | * @return TDBMObject |
||
281 | */ |
||
282 | /* public function getObject($table_name, $filters, $className = null, $lazy_loading = false) { |
||
283 | |||
284 | if (is_array($filters) || $filters instanceof FilterInterface) { |
||
285 | $isFilterBag = false; |
||
286 | if (is_array($filters)) { |
||
287 | // Is this a multiple primary key or a filter bag? |
||
288 | // Let's have a look at the first item of the array to decide. |
||
289 | foreach ($filters as $filter) { |
||
290 | if (is_array($filter) || $filter instanceof FilterInterface) { |
||
291 | $isFilterBag = true; |
||
292 | } |
||
293 | break; |
||
294 | } |
||
295 | } else { |
||
296 | $isFilterBag = true; |
||
297 | } |
||
298 | |||
299 | if ($isFilterBag == true) { |
||
300 | // If a filter bag was passer in parameter, let's perform a getObjects. |
||
301 | $objects = $this->getObjects($table_name, $filters, null, null, null, $className); |
||
302 | if (count($objects) == 0) { |
||
303 | return null; |
||
304 | } elseif (count($objects) > 1) { |
||
305 | throw new DuplicateRowException("Error while querying an object for table '$table_name': ".count($objects)." rows have been returned, but we should have received at most one."); |
||
306 | } |
||
307 | // Return the first and only object. |
||
308 | if ($objects instanceof \Generator) { |
||
309 | return $objects->current(); |
||
310 | } else { |
||
311 | return $objects[0]; |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | $id = $filters; |
||
316 | if ($this->connection == null) { |
||
317 | throw new TDBMException("Error while calling TdbmService->getObject(): No connection has been established on the database!"); |
||
318 | } |
||
319 | $table_name = $this->connection->toStandardcase($table_name); |
||
320 | |||
321 | // If the ID is null, let's throw an exception |
||
322 | if ($id === null) { |
||
323 | throw new TDBMException("The ID you passed to TdbmService->getObject is null for the object of type '$table_name'. Objects primary keys cannot be null."); |
||
324 | } |
||
325 | |||
326 | // If the primary key is split over many columns, the IDs are passed in an array. Let's serialize this array to store it. |
||
327 | if (is_array($id)) { |
||
328 | $id = serialize($id); |
||
329 | } |
||
330 | |||
331 | if ($className === null) { |
||
332 | if (isset($this->tableToBeanMap[$table_name])) { |
||
333 | $className = $this->tableToBeanMap[$table_name]; |
||
334 | } else { |
||
335 | $className = "Mouf\\Database\\TDBM\\TDBMObject"; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | if ($this->objectStorage->has($table_name, $id)) { |
||
340 | $obj = $this->objectStorage->get($table_name, $id); |
||
341 | if (is_a($obj, $className)) { |
||
342 | return $obj; |
||
343 | } else { |
||
344 | throw new TDBMException("Error! The object with ID '$id' for table '$table_name' has already been retrieved. The type for this object is '".get_class($obj)."'' which is not a subtype of '$className'"); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | if ($className != "Mouf\\Database\\TDBM\\TDBMObject" && !is_subclass_of($className, "Mouf\\Database\\TDBM\\TDBMObject")) { |
||
349 | if (!class_exists($className)) { |
||
350 | throw new TDBMException("Error while calling TDBMService->getObject: The class ".$className." does not exist."); |
||
351 | } else { |
||
352 | throw new TDBMException("Error while calling TDBMService->getObject: The class ".$className." should extend TDBMObject."); |
||
353 | } |
||
354 | } |
||
355 | $obj = new $className($this, $table_name, $id); |
||
356 | |||
357 | if ($lazy_loading == false) { |
||
358 | // If we are not doing lazy loading, let's load the object: |
||
359 | $obj->_dbLoadIfNotLoaded(); |
||
360 | } |
||
361 | |||
362 | $this->objectStorage->set($table_name, $id, $obj); |
||
363 | |||
364 | return $obj; |
||
365 | }*/ |
||
366 | |||
367 | /** |
||
368 | * Removes the given object from database. |
||
369 | * This cannot be called on an object that is not attached to this TDBMService |
||
370 | * (will throw a TDBMInvalidOperationException) |
||
371 | * |
||
372 | * @param AbstractTDBMObject $object the object to delete. |
||
373 | * @throws TDBMException |
||
374 | * @throws TDBMInvalidOperationException |
||
375 | */ |
||
376 | public function delete(AbstractTDBMObject $object) { |
||
414 | |||
415 | /** |
||
416 | * Removes all many to many relationships for this object. |
||
417 | * @param AbstractTDBMObject $object |
||
418 | */ |
||
419 | private function deleteManyToManyRelationships(AbstractTDBMObject $object) { |
||
431 | |||
432 | |||
433 | /** |
||
434 | * This function removes the given object from the database. It will also remove all objects relied to the one given |
||
435 | * by parameter before all. |
||
436 | * |
||
437 | * Notice: if the object has a multiple primary key, the function will not work. |
||
438 | * |
||
439 | * @param AbstractTDBMObject $objToDelete |
||
440 | */ |
||
441 | public function deleteCascade(AbstractTDBMObject $objToDelete) { |
||
445 | |||
446 | /** |
||
447 | * This function is used only in TDBMService (private function) |
||
448 | * It will call deleteCascade function foreach object relied with a foreign key to the object given by parameter |
||
449 | * |
||
450 | * @param TDBMObject $obj |
||
451 | * @return TDBMObjectArray |
||
452 | */ |
||
453 | private function deleteAllConstraintWithThisObject(TDBMObject $obj) { |
||
472 | |||
473 | /** |
||
474 | * This function performs a save() of all the objects that have been modified. |
||
475 | */ |
||
476 | public function completeSave() { |
||
484 | |||
485 | /** |
||
486 | * Returns an array of objects of "table_name" kind filtered from the filter bag. |
||
487 | * |
||
488 | * The getObjects method should be the most used query method in TDBM if you want to query the database for objects. |
||
489 | * (Note: if you want to query the database for an object by its primary key, use the getObject method). |
||
490 | * |
||
491 | * The getObjects method takes in parameter: |
||
492 | * - table_name: the kinf of TDBMObject you want to retrieve. In TDBM, a TDBMObject matches a database row, so the |
||
493 | * $table_name parameter should be the name of an existing table in database. |
||
494 | * - filter_bag: The filter bag is anything that you can use to filter your request. It can be a SQL Where clause, |
||
495 | * a series of TDBM_Filter objects, or even TDBMObjects or TDBMObjectArrays that you will use as filters. |
||
496 | * - order_bag: The order bag is anything that will be used to order the data that is passed back to you. |
||
497 | * A SQL Order by clause can be used as an order bag for instance, or a OrderByColumn object |
||
498 | * - from (optionnal): The offset from which the query should be performed. For instance, if $from=5, the getObjects method |
||
499 | * will return objects from the 6th rows. |
||
500 | * - limit (optionnal): The maximum number of objects to return. Used together with $from, you can implement |
||
501 | * paging mechanisms. |
||
502 | * - hint_path (optionnal): EXPERTS ONLY! The path the request should use if not the most obvious one. This parameter |
||
503 | * should be used only if you perfectly know what you are doing. |
||
504 | * |
||
505 | * The getObjects method will return a TDBMObjectArray. A TDBMObjectArray is an array of TDBMObjects that does behave as |
||
506 | * a single TDBMObject if the array has only one member. Refer to the documentation of TDBMObjectArray and TDBMObject |
||
507 | * to learn more. |
||
508 | * |
||
509 | * More about the filter bag: |
||
510 | * A filter is anything that can change the set of objects returned by getObjects. |
||
511 | * There are many kind of filters in TDBM: |
||
512 | * A filter can be: |
||
513 | * - A SQL WHERE clause: |
||
514 | * The clause is specified without the "WHERE" keyword. For instance: |
||
515 | * $filter = "users.first_name LIKE 'J%'"; |
||
516 | * is a valid filter. |
||
517 | * The only difference with SQL is that when you specify a column name, it should always be fully qualified with |
||
518 | * the table name: "country_name='France'" is not valid, while "countries.country_name='France'" is valid (if |
||
519 | * "countries" is a table and "country_name" a column in that table, sure. |
||
520 | * For instance, |
||
521 | * $french_users = TDBMObject::getObjects("users", "countries.country_name='France'"); |
||
522 | * will return all the users that are French (based on trhe assumption that TDBM can find a way to connect the users |
||
523 | * table to the country table using foreign keys, see the manual for that point). |
||
524 | * - A TDBMObject: |
||
525 | * An object can be used as a filter. For instance, we could get the France object and then find any users related to |
||
526 | * that object using: |
||
527 | * $france = TDBMObject::getObjects("country", "countries.country_name='France'"); |
||
528 | * $french_users = TDBMObject::getObjects("users", $france); |
||
529 | * - A TDBMObjectArray can be used as a filter too. |
||
530 | * For instance: |
||
531 | * $french_groups = TDBMObject::getObjects("groups", $french_users); |
||
532 | * might return all the groups in which french users can be found. |
||
533 | * - Finally, TDBM_xxxFilter instances can be used. |
||
534 | * TDBM provides the developer a set of TDBM_xxxFilters that can be used to model a SQL Where query. |
||
535 | * Using the appropriate filter object, you can model the operations =,<,<=,>,>=,IN,LIKE,AND,OR, IS NULL and NOT |
||
536 | * For instance: |
||
537 | * $french_users = TDBMObject::getObjects("users", new EqualFilter('countries','country_name','France'); |
||
538 | * Refer to the documentation of the appropriate filters for more information. |
||
539 | * |
||
540 | * The nice thing about a filter bag is that it can be any filter, or any array of filters. In that case, filters are |
||
541 | * 'ANDed' together. |
||
542 | * So a request like this is valid: |
||
543 | * $france = TDBMObject::getObjects("country", "countries.country_name='France'"); |
||
544 | * $french_administrators = TDBMObject::getObjects("users", array($france,"role.role_name='Administrators'"); |
||
545 | * This requests would return the users that are both French and administrators. |
||
546 | * |
||
547 | * Finally, if filter_bag is null, the whole table is returned. |
||
548 | * |
||
549 | * More about the order bag: |
||
550 | * The order bag contains anything that can be used to order the data that is passed back to you. |
||
551 | * The order bag can contain two kinds of objects: |
||
552 | * - A SQL ORDER BY clause: |
||
553 | * The clause is specified without the "ORDER BY" keyword. For instance: |
||
554 | * $orderby = "users.last_name ASC, users.first_name ASC"; |
||
555 | * is a valid order bag. |
||
556 | * The only difference with SQL is that when you specify a column name, it should always be fully qualified with |
||
557 | * the table name: "country_name ASC" is not valid, while "countries.country_name ASC" is valid (if |
||
558 | * "countries" is a table and "country_name" a column in that table, sure. |
||
559 | * For instance, |
||
560 | * $french_users = TDBMObject::getObjects("users", null, "countries.country_name ASC"); |
||
561 | * will return all the users sorted by country. |
||
562 | * - A OrderByColumn object |
||
563 | * This object models a single column in a database. |
||
564 | * |
||
565 | * @param string $table_name The name of the table queried |
||
566 | * @param mixed $filter_bag The filter bag (see above for complete description) |
||
567 | * @param mixed $orderby_bag The order bag (see above for complete description) |
||
568 | * @param integer $from The offset |
||
569 | * @param integer $limit The maximum number of rows returned |
||
570 | * @param string $className Optional: The name of the class to instanciate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned. |
||
571 | * @param unknown_type $hint_path Hints to get the path for the query (expert parameter, you should leave it to null). |
||
572 | * @return TDBMObjectArray A TDBMObjectArray containing the resulting objects of the query. |
||
573 | */ |
||
574 | /* public function getObjects($table_name, $filter_bag=null, $orderby_bag=null, $from=null, $limit=null, $className=null, $hint_path=null) { |
||
575 | if ($this->connection == null) { |
||
576 | throw new TDBMException("Error while calling TDBMObject::getObject(): No connection has been established on the database!"); |
||
577 | } |
||
578 | return $this->getObjectsByMode('getObjects', $table_name, $filter_bag, $orderby_bag, $from, $limit, $className, $hint_path); |
||
579 | }*/ |
||
580 | |||
581 | |||
582 | /** |
||
583 | * Takes in input a filter_bag (which can be about anything from a string to an array of TDBMObjects... see above from documentation), |
||
584 | * and gives back a proper Filter object. |
||
585 | * |
||
586 | * @param mixed $filter_bag |
||
587 | * @return array First item: filter string, second item: parameters. |
||
588 | * @throws TDBMException |
||
589 | */ |
||
590 | public function buildFilterFromFilterBag($filter_bag) { |
||
684 | |||
685 | /** |
||
686 | * Takes in input an order_bag (which can be about anything from a string to an array of OrderByColumn objects... see above from documentation), |
||
687 | * and gives back an array of OrderByColumn / OrderBySQLString objects. |
||
688 | * |
||
689 | * @param unknown_type $orderby_bag |
||
690 | * @return array |
||
691 | */ |
||
692 | public function buildOrderArrayFromOrderBag($orderby_bag) { |
||
714 | |||
715 | /** |
||
716 | * @param string $table |
||
717 | * @return string[] |
||
718 | */ |
||
719 | public function getPrimaryKeyColumns($table) { |
||
750 | |||
751 | /** |
||
752 | * This is an internal function, you should not use it in your application. |
||
753 | * This is used internally by TDBM to add an object to the object cache. |
||
754 | * |
||
755 | * @param DbRow $dbRow |
||
756 | */ |
||
757 | public function _addToCache(DbRow $dbRow) { |
||
762 | |||
763 | /** |
||
764 | * This is an internal function, you should not use it in your application. |
||
765 | * This is used internally by TDBM to remove the object from the list of objects that have been |
||
766 | * created/updated but not saved yet. |
||
767 | * |
||
768 | * @param DbRow $myObject |
||
769 | */ |
||
770 | private function removeFromToSaveObjectList(DbRow $myObject) { |
||
773 | |||
774 | /** |
||
775 | * This is an internal function, you should not use it in your application. |
||
776 | * This is used internally by TDBM to add an object to the list of objects that have been |
||
777 | * created/updated but not saved yet. |
||
778 | * |
||
779 | * @param AbstractTDBMObject $myObject |
||
780 | */ |
||
781 | public function _addToToSaveObjectList(DbRow $myObject) { |
||
784 | |||
785 | /** |
||
786 | * Generates all the daos and beans. |
||
787 | * |
||
788 | * @param string $daoFactoryClassName The classe name of the DAO factory |
||
789 | * @param string $daonamespace The namespace for the DAOs, without trailing \ |
||
790 | * @param string $beannamespace The Namespace for the beans, without trailing \ |
||
791 | * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone. |
||
792 | * @return \string[] the list of tables |
||
793 | */ |
||
794 | public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc) { |
||
798 | |||
799 | /** |
||
800 | * @param array<string, string> $tableToBeanMap |
||
801 | */ |
||
802 | public function setTableToBeanMap(array $tableToBeanMap) { |
||
805 | |||
806 | /** |
||
807 | * Saves $object by INSERTing or UPDAT(E)ing it in the database. |
||
808 | * |
||
809 | * @param AbstractTDBMObject $object |
||
810 | * @throws TDBMException |
||
811 | */ |
||
812 | public function save(AbstractTDBMObject $object) { |
||
1009 | |||
1010 | private function persistManyToManyRelationships(AbstractTDBMObject $object) { |
||
1069 | |||
1070 | private function getPivotFilters(AbstractTDBMObject $localBean, AbstractTDBMObject $remoteBean, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) { |
||
1081 | |||
1082 | /** |
||
1083 | * Returns the "values" of the primary key. |
||
1084 | * This returns the primary key from the $primaryKey attribute, not the one stored in the columns. |
||
1085 | * |
||
1086 | * @param AbstractTDBMObject $bean |
||
1087 | * @return array numerically indexed array of values. |
||
1088 | */ |
||
1089 | private function getPrimaryKeyValues(AbstractTDBMObject $bean) { |
||
1094 | |||
1095 | /** |
||
1096 | * Returns a unique hash used to store the object based on its primary key. |
||
1097 | * If the array contains only one value, then the value is returned. |
||
1098 | * Otherwise, a hash representing the array is returned. |
||
1099 | * |
||
1100 | * @param array $primaryKeys An array of columns => values forming the primary key |
||
1101 | * @return string |
||
1102 | */ |
||
1103 | public function getObjectHash(array $primaryKeys) { |
||
1111 | |||
1112 | /** |
||
1113 | * Returns an array of primary keys from the object. |
||
1114 | * The primary keys are extracted from the object columns and not from the primary keys stored in the |
||
1115 | * $primaryKeys variable of the object. |
||
1116 | * |
||
1117 | * @param DbRow $dbRow |
||
1118 | * @return array Returns an array of column => value |
||
1119 | */ |
||
1120 | public function getPrimaryKeysForObjectFromDbRow(DbRow $dbRow) { |
||
1125 | |||
1126 | /** |
||
1127 | * Returns an array of primary keys for the given row. |
||
1128 | * The primary keys are extracted from the object columns. |
||
1129 | * |
||
1130 | * @param $table |
||
1131 | * @param array $columns |
||
1132 | * @return array |
||
1133 | */ |
||
1134 | public function _getPrimaryKeysFromObjectData($table, array $columns) { |
||
1144 | |||
1145 | /** |
||
1146 | * Attaches $object to this TDBMService. |
||
1147 | * The $object must be in DETACHED state and will pass in NEW state. |
||
1148 | * |
||
1149 | * @param AbstractTDBMObject $object |
||
1150 | * @throws TDBMInvalidOperationException |
||
1151 | */ |
||
1152 | public function attach(AbstractTDBMObject $object) { |
||
1155 | |||
1156 | /** |
||
1157 | * Returns an associative array (column => value) for the primary keys from the table name and an |
||
1158 | * indexed array of primary key values. |
||
1159 | * |
||
1160 | * @param string $tableName |
||
1161 | * @param array $indexedPrimaryKeys |
||
1162 | */ |
||
1163 | public function _getPrimaryKeysFromIndexedPrimaryKeys($tableName, array $indexedPrimaryKeys) { |
||
1173 | |||
1174 | /** |
||
1175 | * Return the list of tables (from child to parent) joining the tables passed in parameter. |
||
1176 | * Tables must be in a single line of inheritance. The method will find missing tables. |
||
1177 | * |
||
1178 | * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent, |
||
1179 | * we must be able to find all other tables. |
||
1180 | * |
||
1181 | * @param string[] $tables |
||
1182 | * @return string[] |
||
1183 | */ |
||
1184 | public function _getLinkBetweenInheritedTables(array $tables) |
||
1192 | |||
1193 | /** |
||
1194 | * Return the list of tables (from child to parent) joining the tables passed in parameter. |
||
1195 | * Tables must be in a single line of inheritance. The method will find missing tables. |
||
1196 | * |
||
1197 | * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent, |
||
1198 | * we must be able to find all other tables. |
||
1199 | * |
||
1200 | * @param string[] $tables |
||
1201 | * @return string[] |
||
1202 | */ |
||
1203 | private function _getLinkBetweenInheritedTablesWithoutCache(array $tables) { |
||
1224 | |||
1225 | /** |
||
1226 | * Returns the list of tables related to this table (via a parent or child inheritance relationship) |
||
1227 | * @param string $table |
||
1228 | * @return string[] |
||
1229 | */ |
||
1230 | public function _getRelatedTablesByInheritance($table) |
||
1236 | |||
1237 | /** |
||
1238 | * Returns the list of tables related to this table (via a parent or child inheritance relationship) |
||
1239 | * @param string $table |
||
1240 | * @return string[] |
||
1241 | */ |
||
1242 | private function _getRelatedTablesByInheritanceWithoutCache($table) { |
||
1262 | |||
1263 | /** |
||
1264 | * Explore all the children and descendant of $table and returns ForeignKeyConstraints on those. |
||
1265 | * |
||
1266 | * @param string $table |
||
1267 | * @return string[] |
||
1268 | */ |
||
1269 | private function exploreChildrenTablesRelationships(SchemaAnalyzer $schemaAnalyzer, $table) { |
||
1279 | |||
1280 | /** |
||
1281 | * Casts a foreign key into SQL, assuming table name is used with no alias. |
||
1282 | * The returned value does contain only one table. For instance: |
||
1283 | * |
||
1284 | * " LEFT JOIN table2 ON table1.id = table2.table1_id" |
||
1285 | * |
||
1286 | * @param ForeignKeyConstraint $fk |
||
1287 | * @param bool $leftTableIsLocal |
||
1288 | * @return string |
||
1289 | */ |
||
1290 | /*private function foreignKeyToSql(ForeignKeyConstraint $fk, $leftTableIsLocal) { |
||
1291 | $onClauses = []; |
||
1292 | $foreignTableName = $this->connection->quoteIdentifier($fk->getForeignTableName()); |
||
1293 | $foreignColumns = $fk->getForeignColumns(); |
||
1294 | $localTableName = $this->connection->quoteIdentifier($fk->getLocalTableName()); |
||
1295 | $localColumns = $fk->getLocalColumns(); |
||
1296 | $columnCount = count($localTableName); |
||
1297 | |||
1298 | for ($i = 0; $i < $columnCount; $i++) { |
||
1299 | $onClauses[] = sprintf("%s.%s = %s.%s", |
||
1300 | $localTableName, |
||
1301 | $this->connection->quoteIdentifier($localColumns[$i]), |
||
1302 | $foreignColumns, |
||
1303 | $this->connection->quoteIdentifier($foreignColumns[$i]) |
||
1304 | ); |
||
1305 | } |
||
1306 | |||
1307 | $onClause = implode(' AND ', $onClauses); |
||
1308 | |||
1309 | if ($leftTableIsLocal) { |
||
1310 | return sprintf(" LEFT JOIN %s ON (%s)", $foreignTableName, $onClause); |
||
1311 | } else { |
||
1312 | return sprintf(" LEFT JOIN %s ON (%s)", $localTableName, $onClause); |
||
1313 | } |
||
1314 | }*/ |
||
1315 | |||
1316 | /** |
||
1317 | * Returns an identifier for the group of tables passed in parameter. |
||
1318 | * |
||
1319 | * @param string[] $relatedTables |
||
1320 | * @return string |
||
1321 | */ |
||
1322 | private function getTableGroupName(array $relatedTables) { |
||
1326 | |||
1327 | /** |
||
1328 | * |
||
1329 | * @param string $mainTable The name of the table queried |
||
1330 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1331 | * @param array $parameters |
||
1332 | * @param string|null $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column) |
||
1333 | * @param array $additionalTablesFetch |
||
1334 | * @param string $mode |
||
1335 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned. |
||
1336 | * @return ResultIterator An object representing an array of results. |
||
1337 | * @throws TDBMException |
||
1338 | */ |
||
1339 | public function findObjects($mainTable, $filter=null, array $parameters = array(), $orderString=null, array $additionalTablesFetch = array(), $mode = null, $className=null) { |
||
1412 | |||
1413 | /** |
||
1414 | * @param $table |
||
1415 | * @param array $primaryKeys |
||
1416 | * @param array $additionalTablesFetch |
||
1417 | * @param bool $lazy Whether to perform lazy loading on this object or not. |
||
1418 | * @param string $className |
||
1419 | * @return AbstractTDBMObject |
||
1420 | * @throws TDBMException |
||
1421 | */ |
||
1422 | public function findObjectByPk($table, array $primaryKeys, array $additionalTablesFetch = array(), $lazy = false, $className=null) { |
||
1459 | |||
1460 | /** |
||
1461 | * Returns a unique bean (or null) according to the filters passed in parameter. |
||
1462 | * |
||
1463 | * @param string $mainTable The name of the table queried |
||
1464 | * @param string|null $filterString The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1465 | * @param array $parameters |
||
1466 | * @param array $additionalTablesFetch |
||
1467 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned. |
||
1468 | * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters. |
||
1469 | * @throws TDBMException |
||
1470 | */ |
||
1471 | public function findObject($mainTable, $filterString=null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
||
1482 | |||
1483 | /** |
||
1484 | * Returns a unique bean according to the filters passed in parameter. |
||
1485 | * Throws a NoBeanFoundException if no bean was found for the filter passed in parameter. |
||
1486 | * |
||
1487 | * @param string $mainTable The name of the table queried |
||
1488 | * @param string|null $filterString The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1489 | * @param array $parameters |
||
1490 | * @param array $additionalTablesFetch |
||
1491 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned. |
||
1492 | * @return AbstractTDBMObject The object we want |
||
1493 | * @throws TDBMException |
||
1494 | */ |
||
1495 | public function findObjectOrFail($mainTable, $filterString=null, array $parameters = array(), array $additionalTablesFetch = array(), $className = null) { |
||
1502 | |||
1503 | /** |
||
1504 | * @param array $beanData An array of data: array<table, array<column, value>> |
||
1505 | * @return array an array with first item = class name and second item = table name |
||
1506 | */ |
||
1507 | public function _getClassNameFromBeanData(array $beanData) { |
||
1538 | |||
1539 | /** |
||
1540 | * Returns an item from cache or computes it using $closure and puts it in cache. |
||
1541 | * |
||
1542 | * @param string $key |
||
1543 | * @param callable $closure |
||
1544 | * |
||
1545 | * @return mixed |
||
1546 | */ |
||
1547 | private function fromCache($key, callable $closure) |
||
1557 | |||
1558 | /** |
||
1559 | * Returns the foreign key object. |
||
1560 | * @param string $table |
||
1561 | * @param string $fkName |
||
1562 | * @return ForeignKeyConstraint |
||
1563 | */ |
||
1564 | public function _getForeignKeyByName($table, $fkName) { |
||
1567 | |||
1568 | /** |
||
1569 | * @param $pivotTableName |
||
1570 | * @param AbstractTDBMObject $bean |
||
1571 | * @return AbstractTDBMObject[] |
||
1572 | */ |
||
1573 | public function _getRelatedBeans($pivotTableName, AbstractTDBMObject $bean) { |
||
1588 | |||
1589 | /** |
||
1590 | * @param $pivotTableName |
||
1591 | * @param AbstractTDBMObject $bean The LOCAL bean |
||
1592 | * @return ForeignKeyConstraint[] First item: the LOCAL bean, second item: the REMOTE bean. |
||
1593 | * @throws TDBMException |
||
1594 | */ |
||
1595 | private function getPivotTableForeignKeys($pivotTableName, AbstractTDBMObject $bean) { |
||
1610 | } |
||
1611 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.