Complex classes like FluentPdoModel 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 FluentPdoModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class FluentPdoModel |
||
38 | { |
||
39 | const OPERATOR_AND = ' AND '; |
||
40 | const OPERATOR_OR = ' OR '; |
||
41 | const ORDERBY_ASC = 'ASC'; |
||
42 | const ORDERBY_DESC = 'DESC'; |
||
43 | const SAVE_INSERT = 'INSERT'; |
||
44 | const SAVE_UPDATE = 'UPDATE'; |
||
45 | const LEFT_JOIN = 'LEFT'; |
||
46 | const INNER_JOIN = 'INNER'; |
||
47 | const ONE_DAY = 86400; |
||
48 | const ONE_WEEK = 60480060; |
||
49 | const ONE_HOUR = 3600; |
||
50 | const TEN_MINS = 600; |
||
51 | const CACHE_NO = -1; |
||
52 | const CACHE_DEFAULT = 0; |
||
53 | |||
54 | /** @var AbstractPdo $_connection */ |
||
55 | protected $_connection = null; |
||
56 | |||
57 | /** @var string */ |
||
58 | protected $_primary_key = 'id'; |
||
59 | |||
60 | /** @var array */ |
||
61 | protected $_where_parameters = []; |
||
62 | |||
63 | /** @var array */ |
||
64 | protected $_select_fields = []; |
||
65 | |||
66 | /** @var array */ |
||
67 | protected $_join_sources = []; |
||
68 | |||
69 | /** @var array */ |
||
70 | protected $_join_aliases = []; |
||
71 | |||
72 | /** @var array $_associations */ |
||
73 | protected $_associations = [ |
||
74 | 'belongsTo' => [], |
||
75 | ]; |
||
76 | |||
77 | /** @var array */ |
||
78 | protected $_where_conditions = []; |
||
79 | |||
80 | protected $_raw_sql = null; |
||
81 | |||
82 | /** @var int */ |
||
83 | protected $_limit = null; |
||
84 | |||
85 | /** @var int */ |
||
86 | protected $_offset = null; |
||
87 | |||
88 | /** @var array */ |
||
89 | protected $_order_by = []; |
||
90 | |||
91 | /** @var array */ |
||
92 | protected $_group_by = []; |
||
93 | |||
94 | /** @var string */ |
||
95 | protected $_and_or_operator = self::OPERATOR_AND; |
||
96 | |||
97 | /** @var array */ |
||
98 | protected $_having = []; |
||
99 | |||
100 | /** @var bool */ |
||
101 | protected $_wrap_open = false; |
||
102 | |||
103 | /** @var int */ |
||
104 | protected $_last_wrap_position = 0; |
||
105 | |||
106 | /** @var PDOStatement $_pdo_stmt */ |
||
107 | protected $_pdo_stmt = null; |
||
108 | |||
109 | /** @var bool */ |
||
110 | protected $_distinct = false; |
||
111 | |||
112 | /** @var null */ |
||
113 | protected $_requested_fields = []; |
||
114 | |||
115 | /** @var null */ |
||
116 | protected $_filter_meta = []; |
||
117 | |||
118 | /** @var bool */ |
||
119 | protected $_log_queries = false; |
||
120 | |||
121 | /** @var array */ |
||
122 | protected $_timer = []; |
||
123 | |||
124 | /** @var int */ |
||
125 | protected $_slow_query_secs = 5; |
||
126 | |||
127 | /** @var array */ |
||
128 | protected $_pagination_attribs = [ |
||
129 | '_limit', |
||
130 | '_offset', |
||
131 | '_order', |
||
132 | '_fields', |
||
133 | '_search' |
||
134 | ]; |
||
135 | |||
136 | /** @var string $_table_name */ |
||
137 | protected $_table_name = null; |
||
138 | |||
139 | /** @var string $_table_alias */ |
||
140 | protected $_table_alias = null; |
||
141 | |||
142 | /** @var string $_display_column */ |
||
143 | protected $_display_column = null; |
||
144 | |||
145 | /** @var string $_connection_name */ |
||
146 | protected $_connection_name = null; |
||
147 | |||
148 | /** @var array $_schema */ |
||
149 | protected $_schema = []; |
||
150 | |||
151 | /** @var array $_virtual_fields */ |
||
152 | protected $_virtual_fields = []; |
||
153 | |||
154 | /** @var array $_errors */ |
||
155 | protected $_errors = []; |
||
156 | |||
157 | /** |
||
158 | * @var int - true = connection default x days |
||
159 | * - false = no cache |
||
160 | * - int = a specific amount |
||
161 | */ |
||
162 | protected $_cache_ttl = self::CACHE_NO; |
||
163 | |||
164 | /** @var string */ |
||
165 | protected $_tmp_table_prefix = 'tmp_'; |
||
166 | |||
167 | /** @var null|string */ |
||
168 | protected $_built_query = null; |
||
169 | |||
170 | // Make handlers public so whe can delete them externally to reduce memory in hhmv |
||
171 | //protected $_handlers = []; |
||
172 | public $_handlers = []; |
||
173 | |||
174 | /** @var bool User want to directly specify the fields */ |
||
175 | protected $_explicit_select_mode = false; |
||
176 | |||
177 | /** @var string[] */ |
||
178 | protected $_update_raw = []; |
||
179 | |||
180 | protected $_max_callback_failures = null; |
||
181 | |||
182 | protected $_num_callback_failures = 0; |
||
183 | |||
184 | protected $_filter_on_fetch = false; |
||
185 | |||
186 | protected $_log_filter_changes = true; |
||
187 | |||
188 | protected $_include_count = false; |
||
189 | |||
190 | /** |
||
191 | * @param AbstractPdo|null $connection |
||
192 | */ |
||
193 | public function __construct(AbstractPdo $connection=null) |
||
200 | |||
201 | public function init() |
||
203 | |||
204 | /** |
||
205 | * @return AbstractPdo |
||
206 | * @throws Exception |
||
207 | */ |
||
208 | public function getPdo() |
||
212 | |||
213 | /** |
||
214 | * @return AbstractLogger |
||
215 | */ |
||
216 | public function getLogger() |
||
220 | |||
221 | /** |
||
222 | * @return RedisCache |
||
223 | */ |
||
224 | public function getCache() |
||
228 | |||
229 | /** |
||
230 | * Define the working table and create a new instance |
||
231 | * |
||
232 | * @param string $tableName - Table name |
||
233 | * @param string $alias - The table alias name |
||
234 | * @param string $displayColumn |
||
235 | * @param string $primaryKeyName |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function table($tableName, $alias='', $displayColumn='', $primaryKeyName='id') |
||
247 | |||
248 | /** |
||
249 | * @param $primaryKeyName |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function primaryKeyName($primaryKeyName) |
||
257 | |||
258 | /** |
||
259 | * @param $tableName |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function tableName($tableName) |
||
268 | |||
269 | /** |
||
270 | * @param $explicitSelect |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function explicitSelectMode($explicitSelect=true) |
||
279 | |||
280 | /** |
||
281 | * @param bool $filterOnFetch |
||
282 | * |
||
283 | * @return $this |
||
284 | */ |
||
285 | public function filterOnFetch($filterOnFetch=true) |
||
290 | |||
291 | /** |
||
292 | * @param bool $logFilterChanges |
||
293 | * |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function logFilterChanges($logFilterChanges=true) |
||
301 | |||
302 | /** |
||
303 | * Return the name of the table |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getTableName() |
||
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | public function getDisplayColumn() |
||
319 | |||
320 | /** |
||
321 | * Set the display column |
||
322 | * |
||
323 | * @param string $column |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function displayColumn($column) |
||
332 | /** |
||
333 | * Set the table alias |
||
334 | * |
||
335 | * @param string $alias |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function tableAlias($alias) |
||
344 | |||
345 | /** |
||
346 | * @param int $cacheTtl |
||
347 | * @return $this |
||
348 | * @throws Exception |
||
349 | */ |
||
350 | protected function _cacheTtl($cacheTtl) |
||
360 | |||
361 | /** |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getTableAlias() |
||
368 | |||
369 | /** |
||
370 | * @param array $associations |
||
371 | * |
||
372 | * @return $this |
||
373 | */ |
||
374 | public function associations(array $associations) |
||
379 | |||
380 | /** |
||
381 | * @param string $alias |
||
382 | * @param array $definition |
||
383 | * @return $this |
||
384 | */ |
||
385 | public function setBelongsTo($alias, array $definition) |
||
393 | |||
394 | public function setBelongsToDisplayField($alias, $displayField) |
||
403 | |||
404 | /** |
||
405 | * @param PDOStatement $stmt |
||
406 | * |
||
407 | * @param PDOStatement $stmt |
||
408 | * @param Closure $fnCallback |
||
409 | * @return bool|stdClass |
||
410 | */ |
||
411 | public function fetchRow(PDOStatement $stmt, Closure $fnCallback=null) |
||
430 | |||
431 | /** |
||
432 | * @param array $schema |
||
433 | * |
||
434 | * @return $this |
||
435 | */ |
||
436 | public function schema(array $schema) |
||
441 | |||
442 | /** |
||
443 | * @param string|array $field |
||
444 | * @param $type |
||
445 | * @return $this |
||
446 | */ |
||
447 | public function addSchema($field, $type) |
||
460 | |||
461 | /** |
||
462 | * @param $keys_only |
||
463 | * @return array |
||
464 | */ |
||
465 | public function getColumns($keys_only=true) |
||
469 | |||
470 | /** |
||
471 | * Get the primary key name |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getPrimaryKeyName() |
||
479 | |||
480 | /** |
||
481 | * @param string $query |
||
482 | * @param array $parameters |
||
483 | * |
||
484 | * @return bool |
||
485 | * @throws Exception |
||
486 | */ |
||
487 | public function execute($query, array $parameters=[]) |
||
510 | |||
511 | /** |
||
512 | * @param string $query |
||
513 | * @param array $params |
||
514 | * @return $this |
||
515 | */ |
||
516 | public function query($query, array $params=[]) |
||
522 | |||
523 | /** |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function begin() |
||
530 | |||
531 | /** |
||
532 | * @return bool |
||
533 | */ |
||
534 | public function commit() |
||
538 | |||
539 | /** |
||
540 | * @return bool |
||
541 | */ |
||
542 | public function rollback() |
||
547 | |||
548 | |||
549 | /** |
||
550 | * @param string $sql |
||
551 | * @param array $params |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | public function buildQuery($sql, array $params=[]) |
||
581 | |||
582 | /** |
||
583 | * @param stdClass $record |
||
584 | * |
||
585 | * @return stdClass |
||
586 | */ |
||
587 | protected function _trimAndLowerCaseKeys(stdClass $record) |
||
596 | |||
597 | /** |
||
598 | * Return the number of affected row by the last statement |
||
599 | * |
||
600 | * @return int |
||
601 | */ |
||
602 | public function rowCount() |
||
607 | |||
608 | /** |
||
609 | * @return PDOStatement |
||
610 | * @throws PDOException |
||
611 | */ |
||
612 | public function fetchStmt() |
||
620 | |||
621 | /** |
||
622 | * @param bool $build |
||
623 | * @return array |
||
624 | */ |
||
625 | public function fetchSqlQuery($build=false) |
||
634 | |||
635 | /** |
||
636 | * @param string $table_name |
||
637 | * @param bool $drop_if_exists |
||
638 | * @param array $indexes |
||
639 | * @return $this |
||
640 | * @throws Exception |
||
641 | */ |
||
642 | public function fetchIntoMemoryTable($table_name, $drop_if_exists=true, array $indexes=[]) |
||
663 | |||
664 | /** |
||
665 | * @param null $keyedOn |
||
666 | * @param int $cacheTtl |
||
667 | * @return mixed |
||
668 | */ |
||
669 | public function fetch($keyedOn=null, $cacheTtl=self::CACHE_NO) |
||
704 | |||
705 | protected function _parseWhereForPrimaryLookup() |
||
720 | |||
721 | /** |
||
722 | * @param string $cacheKey |
||
723 | * @param Closure $func |
||
724 | * @param int $cacheTtl - 0 for default ttl, -1 for no cache or int for custom ttl |
||
725 | * @return mixed|null |
||
726 | */ |
||
727 | protected function _cacheData($cacheKey, Closure $func, $cacheTtl=self::CACHE_DEFAULT) |
||
764 | |||
765 | /** |
||
766 | * @param string $cacheKey |
||
767 | * @return bool |
||
768 | */ |
||
769 | public function clearCache($cacheKey) |
||
773 | |||
774 | /** |
||
775 | * @param string|null $table |
||
776 | * @return bool |
||
777 | */ |
||
778 | public function clearCacheByTable($table=null) |
||
787 | |||
788 | /** |
||
789 | * @param Closure $fnCallback |
||
790 | * @param null $keyedOn |
||
791 | * @param bool $returnSuccessCnt |
||
792 | * @return stdClass[]|int |
||
793 | */ |
||
794 | public function fetchCallback(Closure $fnCallback, $keyedOn=null, $returnSuccessCnt=true) |
||
817 | |||
818 | /** |
||
819 | * @param $numFailures |
||
820 | * @return $this |
||
821 | */ |
||
822 | public function maxCallbackFailures($numFailures) |
||
828 | |||
829 | /** |
||
830 | * @param PDOStatement $stmt |
||
831 | * @param Closure $fnCallback |
||
832 | * @param int $successCnt |
||
833 | * @return bool|null|stdClass |
||
834 | */ |
||
835 | protected function _tallySuccessCount($stmt, Closure $fnCallback, &$successCnt) |
||
864 | |||
865 | /** |
||
866 | * @param null|string $keyedOn |
||
867 | * @param null|mixed $valueField |
||
868 | * @param int $cacheTtl |
||
869 | * @return mixed |
||
870 | */ |
||
871 | public function fetchList($keyedOn=null, $valueField=null, $cacheTtl=self::CACHE_NO) |
||
920 | |||
921 | /** |
||
922 | * @param string $column |
||
923 | * @param int $cacheTtl |
||
924 | * @param bool|true $unique |
||
925 | * @return array|mixed |
||
926 | */ |
||
927 | public function fetchColumn($column, $cacheTtl=self::CACHE_NO, $unique=true) |
||
936 | |||
937 | /** |
||
938 | * @param null|string $field |
||
939 | * @param null|int $itemId |
||
940 | * @param int $cacheTtl |
||
941 | * @return mixed|null |
||
942 | */ |
||
943 | public function fetchField($field=null, $itemId=null, $cacheTtl=self::CACHE_NO) |
||
966 | |||
967 | /** |
||
968 | * @param int|null $id |
||
969 | * @param int $cacheTtl |
||
970 | * @return \stdClass|bool |
||
971 | */ |
||
972 | public function fetchOne($id=null, $cacheTtl=self::CACHE_NO) |
||
983 | |||
984 | /** |
||
985 | * @param int|null $id |
||
986 | * @param int $cacheTtl |
||
987 | * @return \stdClass|bool |
||
988 | */ |
||
989 | public function fetchExists($id=null, $cacheTtl=self::CACHE_NO) |
||
998 | |||
999 | /*------------------------------------------------------------------------------ |
||
1000 | Fluent Query Builder |
||
1001 | *-----------------------------------------------------------------------------*/ |
||
1002 | |||
1003 | /** |
||
1004 | * Create the select clause |
||
1005 | * |
||
1006 | * @param mixed $columns - the column to select. Can be string or array of fields |
||
1007 | * @param string $alias - an alias to the column |
||
1008 | * @param bool|true $explicitSelect |
||
1009 | * @return $this |
||
1010 | */ |
||
1011 | public function select($columns='*', $alias=null, $explicitSelect=true) |
||
1051 | |||
1052 | public function selectRaw($select) |
||
1057 | |||
1058 | /** |
||
1059 | * @param bool $log_queries |
||
1060 | * |
||
1061 | * @return $this |
||
1062 | */ |
||
1063 | public function logQueries($log_queries=true) |
||
1068 | |||
1069 | /** |
||
1070 | * @param bool $include_count |
||
1071 | * |
||
1072 | * @return $this |
||
1073 | */ |
||
1074 | public function includeCount($include_count=true) |
||
1079 | |||
1080 | /** |
||
1081 | * @param bool $distinct |
||
1082 | * |
||
1083 | * @return $this |
||
1084 | */ |
||
1085 | public function distinct($distinct=true) |
||
1090 | |||
1091 | /** |
||
1092 | * @param array $fields |
||
1093 | * @return $this |
||
1094 | */ |
||
1095 | public function withBelongsTo($fields=[]) |
||
1107 | |||
1108 | /** |
||
1109 | * @param string $alias |
||
1110 | * @param string $type |
||
1111 | * @param bool $addSelectField |
||
1112 | * @return $this |
||
1113 | */ |
||
1114 | public function autoJoin($alias, $type=self::LEFT_JOIN, $addSelectField=true) |
||
1130 | |||
1131 | /** |
||
1132 | * Add where condition, more calls appends with AND |
||
1133 | * |
||
1134 | * @param string $condition possibly containing ? or :name |
||
1135 | * @param mixed $parameters accepted by PDOStatement::execute or a scalar value |
||
1136 | * @param mixed ... |
||
1137 | * @return $this |
||
1138 | */ |
||
1139 | public function where($condition, $parameters = []) |
||
1186 | |||
1187 | /** |
||
1188 | * Create an AND operator in the where clause |
||
1189 | * |
||
1190 | * @return $this |
||
1191 | */ |
||
1192 | public function _and() |
||
1204 | |||
1205 | |||
1206 | /** |
||
1207 | * Create an OR operator in the where clause |
||
1208 | * |
||
1209 | * @return $this |
||
1210 | */ |
||
1211 | public function _or() |
||
1223 | |||
1224 | /** |
||
1225 | * To group multiple where clauses together. |
||
1226 | * |
||
1227 | * @return $this |
||
1228 | */ |
||
1229 | public function wrap() |
||
1238 | |||
1239 | /** |
||
1240 | * Where Primary key |
||
1241 | * |
||
1242 | * @param integer $id |
||
1243 | * @param bool $addAlias |
||
1244 | * |
||
1245 | * @return $this |
||
1246 | */ |
||
1247 | public function wherePK($id, $addAlias=true) |
||
1252 | |||
1253 | /** |
||
1254 | * WHERE $columnName != $value |
||
1255 | * |
||
1256 | * @param string $columnName |
||
1257 | * @param mixed $value |
||
1258 | * @return $this |
||
1259 | */ |
||
1260 | public function whereNot($columnName, $value) |
||
1264 | /** |
||
1265 | * WHERE $columnName != $value |
||
1266 | * |
||
1267 | * @param string $columnName |
||
1268 | * @param mixed $value |
||
1269 | * @return $this |
||
1270 | */ |
||
1271 | public function whereCoercedNot($columnName, $value) |
||
1275 | |||
1276 | /** |
||
1277 | * WHERE $columnName LIKE $value |
||
1278 | * |
||
1279 | * @param string $columnName |
||
1280 | * @param mixed $value |
||
1281 | * @return $this |
||
1282 | */ |
||
1283 | public function whereLike($columnName, $value) |
||
1287 | |||
1288 | /** |
||
1289 | * @param string $columnName |
||
1290 | * @param mixed $value1 |
||
1291 | * @param mixed $value2 |
||
1292 | * @return $this |
||
1293 | */ |
||
1294 | public function whereBetween($columnName, $value1, $value2) |
||
1298 | |||
1299 | /** |
||
1300 | * @param string $columnName |
||
1301 | * @param mixed $value1 |
||
1302 | * @param mixed $value2 |
||
1303 | * @return $this |
||
1304 | */ |
||
1305 | public function whereNotBetween($columnName, $value1, $value2) |
||
1309 | |||
1310 | /** |
||
1311 | * @param string $columnName |
||
1312 | * @param string $regex |
||
1313 | * @return $this |
||
1314 | */ |
||
1315 | public function whereRegex($columnName, $regex) |
||
1319 | |||
1320 | /** |
||
1321 | * @param string $columnName |
||
1322 | * @param string $regex |
||
1323 | * @return $this |
||
1324 | */ |
||
1325 | public function whereNotRegex($columnName, $regex) |
||
1329 | |||
1330 | /** |
||
1331 | * WHERE $columnName NOT LIKE $value |
||
1332 | * |
||
1333 | * @param string $columnName |
||
1334 | * @param mixed $value |
||
1335 | * @return $this |
||
1336 | */ |
||
1337 | public function whereNotLike($columnName, $value) |
||
1341 | |||
1342 | /** |
||
1343 | * WHERE $columnName > $value |
||
1344 | * |
||
1345 | * @param string $columnName |
||
1346 | * @param mixed $value |
||
1347 | * @return $this |
||
1348 | */ |
||
1349 | public function whereGt($columnName, $value) |
||
1353 | |||
1354 | /** |
||
1355 | * WHERE $columnName >= $value |
||
1356 | * |
||
1357 | * @param string $columnName |
||
1358 | * @param mixed $value |
||
1359 | * @return $this |
||
1360 | */ |
||
1361 | public function whereGte($columnName, $value) |
||
1365 | |||
1366 | /** |
||
1367 | * WHERE $columnName < $value |
||
1368 | * |
||
1369 | * @param string $columnName |
||
1370 | * @param mixed $value |
||
1371 | * @return $this |
||
1372 | */ |
||
1373 | public function whereLt($columnName, $value) |
||
1377 | |||
1378 | /** |
||
1379 | * WHERE $columnName <= $value |
||
1380 | * |
||
1381 | * @param string $columnName |
||
1382 | * @param mixed $value |
||
1383 | * @return $this |
||
1384 | */ |
||
1385 | public function whereLte($columnName, $value) |
||
1389 | |||
1390 | /** |
||
1391 | * WHERE $columnName IN (?,?,?,...) |
||
1392 | * |
||
1393 | * @param string $columnName |
||
1394 | * @param array $values |
||
1395 | * @return $this |
||
1396 | */ |
||
1397 | public function whereIn($columnName, array $values) |
||
1401 | |||
1402 | /** |
||
1403 | * WHERE $columnName NOT IN (?,?,?,...) |
||
1404 | * |
||
1405 | * @param string $columnName |
||
1406 | * @param array $values |
||
1407 | * @return $this |
||
1408 | */ |
||
1409 | public function whereNotIn($columnName, array $values) |
||
1414 | |||
1415 | /** |
||
1416 | * WHERE $columnName IS NULL |
||
1417 | * |
||
1418 | * @param string $columnName |
||
1419 | * @return $this |
||
1420 | */ |
||
1421 | public function whereNull($columnName) |
||
1425 | |||
1426 | /** |
||
1427 | * WHERE $columnName IS NOT NULL |
||
1428 | * |
||
1429 | * @param string $columnName |
||
1430 | * @return $this |
||
1431 | */ |
||
1432 | public function whereNotNull($columnName) |
||
1436 | |||
1437 | /** |
||
1438 | * @param string $statement |
||
1439 | * @param string $operator |
||
1440 | * @return $this |
||
1441 | */ |
||
1442 | public function having($statement, $operator = self::OPERATOR_AND) |
||
1450 | |||
1451 | /** |
||
1452 | * ORDER BY $columnName (ASC | DESC) |
||
1453 | * |
||
1454 | * @param string $columnName - The name of the column or an expression |
||
1455 | * @param string $ordering (DESC | ASC) |
||
1456 | * @return $this |
||
1457 | */ |
||
1458 | public function orderBy($columnName, $ordering=null) |
||
1469 | |||
1470 | /** |
||
1471 | * GROUP BY $columnName |
||
1472 | * |
||
1473 | * @param string $columnName |
||
1474 | * @return $this |
||
1475 | */ |
||
1476 | public function groupBy($columnName) |
||
1485 | |||
1486 | |||
1487 | /** |
||
1488 | * LIMIT $limit |
||
1489 | * |
||
1490 | * @param int $limit |
||
1491 | * @param int|null $offset |
||
1492 | * @return $this |
||
1493 | */ |
||
1494 | public function limit($limit, $offset=null) |
||
1503 | |||
1504 | /** |
||
1505 | * Return the limit |
||
1506 | * |
||
1507 | * @return integer |
||
1508 | */ |
||
1509 | public function getLimit() |
||
1513 | |||
1514 | /** |
||
1515 | * OFFSET $offset |
||
1516 | * |
||
1517 | * @param int $offset |
||
1518 | * @return $this |
||
1519 | */ |
||
1520 | public function offset($offset) |
||
1525 | |||
1526 | /** |
||
1527 | * Return the offset |
||
1528 | * |
||
1529 | * @return integer |
||
1530 | */ |
||
1531 | public function getOffset() |
||
1535 | |||
1536 | /** |
||
1537 | * Build a join |
||
1538 | * |
||
1539 | * @param string $table - The table name |
||
1540 | * @param string $constraint -> id = profile.user_id |
||
1541 | * @param string $tableAlias - The alias of the table name |
||
1542 | * @param string $joinOperator - LEFT | INNER | etc... |
||
1543 | * @return $this |
||
1544 | */ |
||
1545 | public function join($table, $constraint=null, $tableAlias = null, $joinOperator = '') |
||
1563 | |||
1564 | /** |
||
1565 | * Create a left join |
||
1566 | * |
||
1567 | * @param string $table |
||
1568 | * @param string $constraint |
||
1569 | * @param string $tableAlias |
||
1570 | * @return $this |
||
1571 | */ |
||
1572 | public function leftJoin($table, $constraint, $tableAlias=null) |
||
1576 | |||
1577 | |||
1578 | /** |
||
1579 | * Return the build select query |
||
1580 | * |
||
1581 | * @return string |
||
1582 | */ |
||
1583 | public function getSelectQuery() |
||
1625 | |||
1626 | /** |
||
1627 | * Prepare columns to include the table alias name |
||
1628 | * @param array $columns |
||
1629 | * @return array |
||
1630 | */ |
||
1631 | protected function _prepareColumns(array $columns) |
||
1660 | |||
1661 | /** |
||
1662 | * Build the WHERE clause(s) |
||
1663 | * |
||
1664 | * @param bool $purgeAliases |
||
1665 | * @return string |
||
1666 | */ |
||
1667 | protected function _getWhereString($purgeAliases=false) |
||
1699 | |||
1700 | /** |
||
1701 | * Return the HAVING clause |
||
1702 | * |
||
1703 | * @return string |
||
1704 | */ |
||
1705 | protected function _getHavingString() |
||
1723 | |||
1724 | /** |
||
1725 | * Return the values to be bound for where |
||
1726 | * |
||
1727 | * @param bool $purgeAliases |
||
1728 | * @return array |
||
1729 | */ |
||
1730 | protected function _getWhereParameters($purgeAliases=false) |
||
1735 | |||
1736 | /** |
||
1737 | * Insert new rows |
||
1738 | * $records can be a stdClass or an array of stdClass to add a bulk insert |
||
1739 | * If a single row is inserted, it will return it's row instance |
||
1740 | * |
||
1741 | * @param stdClass|array $records |
||
1742 | * @return int|stdClass|bool |
||
1743 | * @throws Exception |
||
1744 | */ |
||
1745 | public function insert($records) |
||
1775 | |||
1776 | /** |
||
1777 | * @param null $name |
||
1778 | * @return int |
||
1779 | */ |
||
1780 | public function getLastInsertId($name=null) |
||
1784 | |||
1785 | /** |
||
1786 | * @param $records |
||
1787 | * @return array |
||
1788 | */ |
||
1789 | public function insertSqlQuery($records) |
||
1809 | |||
1810 | /** |
||
1811 | * @param $data |
||
1812 | * @param array $matchOn |
||
1813 | * @param bool $returnObj |
||
1814 | * @return bool|int|stdClass |
||
1815 | */ |
||
1816 | public function upsert($data, array $matchOn=[], $returnObj=false) |
||
1837 | |||
1838 | /** |
||
1839 | * @param stdClass $object |
||
1840 | * @param array $matchOn |
||
1841 | * @param bool $returnObj |
||
1842 | * @return bool|int|stdClass |
||
1843 | */ |
||
1844 | public function upsertOne(stdClass $object, array $matchOn=[], $returnObj=false) |
||
1882 | |||
1883 | /** |
||
1884 | * @param array $data |
||
1885 | * @param array $matchOn |
||
1886 | * @param bool|false $returnObj |
||
1887 | * @return bool|int|stdClass |
||
1888 | */ |
||
1889 | public function upsertArr(array $data, array $matchOn=[], $returnObj=false) |
||
1893 | |||
1894 | /** |
||
1895 | * Update entries |
||
1896 | * Use the query builder to create the where clause |
||
1897 | * |
||
1898 | * @param stdClass $record |
||
1899 | * @param bool $updateAll |
||
1900 | * @return bool|int |
||
1901 | * @throws Exception |
||
1902 | */ |
||
1903 | public function update(stdClass $record, $updateAll=false) |
||
1925 | |||
1926 | /** |
||
1927 | * @param array $record |
||
1928 | * @param bool|false $updateAll |
||
1929 | * @return bool|int |
||
1930 | * @throws Exception |
||
1931 | */ |
||
1932 | public function updateArr(array $record, $updateAll=false) |
||
1936 | |||
1937 | /** |
||
1938 | * @param array $record |
||
1939 | * @return bool|int|stdClass |
||
1940 | */ |
||
1941 | public function insertArr(array $record) |
||
1945 | |||
1946 | /** |
||
1947 | * @param int $field |
||
1948 | * @param mixed $value |
||
1949 | * @param null $id |
||
1950 | * @param bool|false $updateAll |
||
1951 | * @return bool|int |
||
1952 | * @throws Exception |
||
1953 | */ |
||
1954 | public function updateField($field, $value, $id=null, $updateAll=false) |
||
1962 | |||
1963 | /** |
||
1964 | * @param int $field |
||
1965 | * @param mixed $value |
||
1966 | * @param null $id |
||
1967 | * @param bool|false $updateAll |
||
1968 | * @return bool|int |
||
1969 | * @throws Exception |
||
1970 | */ |
||
1971 | public function concatField($field, $value, $id=null, $updateAll=false) |
||
1975 | |||
1976 | /** |
||
1977 | * @param stdClass $record |
||
1978 | * @return bool|int |
||
1979 | * @throws Exception |
||
1980 | */ |
||
1981 | public function updateChanged(stdClass $record) |
||
1994 | |||
1995 | /** |
||
1996 | * @param string $expression |
||
1997 | * @param array $params |
||
1998 | * @return $this |
||
1999 | */ |
||
2000 | public function updateByExpression($expression, array $params) |
||
2005 | |||
2006 | /** |
||
2007 | * @param array $data |
||
2008 | * @return int |
||
2009 | * @throws Exception |
||
2010 | */ |
||
2011 | public function rawUpdate(array $data=[]) |
||
2019 | |||
2020 | /** |
||
2021 | * @param stdClass $record |
||
2022 | * @return array |
||
2023 | */ |
||
2024 | public function updateSqlQuery(stdClass $record) |
||
2034 | |||
2035 | /** |
||
2036 | * @param $record |
||
2037 | * @return array |
||
2038 | */ |
||
2039 | protected function updateSql(array $record) |
||
2069 | |||
2070 | /** |
||
2071 | * Delete rows |
||
2072 | * Use the query builder to create the where clause |
||
2073 | * @param bool $deleteAll = When there is no where condition, setting to true will delete all |
||
2074 | * @return int - total affected rows |
||
2075 | * @throws Exception |
||
2076 | */ |
||
2077 | public function delete($deleteAll=false) |
||
2087 | |||
2088 | /** |
||
2089 | * @param bool|false $force |
||
2090 | * @return $this |
||
2091 | * @throws Exception |
||
2092 | */ |
||
2093 | public function truncate($force=false) |
||
2106 | |||
2107 | /** |
||
2108 | * @return array |
||
2109 | */ |
||
2110 | public function deleteSqlQuery() |
||
2120 | |||
2121 | |||
2122 | /** |
||
2123 | * Return the aggregate count of column |
||
2124 | * |
||
2125 | * @param string $column |
||
2126 | * @param int $cacheTtl |
||
2127 | * @return int |
||
2128 | */ |
||
2129 | public function count($column='*', $cacheTtl=self::CACHE_NO) |
||
2136 | |||
2137 | |||
2138 | /** |
||
2139 | * Return the aggregate max count of column |
||
2140 | * |
||
2141 | * @param string $column |
||
2142 | * @param int $cacheTtl |
||
2143 | * @return mixed|null |
||
2144 | */ |
||
2145 | public function max($column, $cacheTtl=self::CACHE_NO) |
||
2152 | |||
2153 | |||
2154 | /** |
||
2155 | * Return the aggregate min count of column |
||
2156 | * |
||
2157 | * @param string $column |
||
2158 | * @param int $cacheTtl |
||
2159 | * @return mixed|null |
||
2160 | */ |
||
2161 | public function min($column, $cacheTtl=self::CACHE_NO) |
||
2168 | |||
2169 | /** |
||
2170 | * Return the aggregate sum count of column |
||
2171 | * |
||
2172 | * @param string $column |
||
2173 | * @param int $cacheTtl |
||
2174 | * @return mixed|null |
||
2175 | */ |
||
2176 | public function sum($column, $cacheTtl=self::CACHE_NO) |
||
2183 | |||
2184 | /** |
||
2185 | * Return the aggregate average count of column |
||
2186 | * |
||
2187 | * @param string $column |
||
2188 | * @param int $cacheTtl |
||
2189 | * @return mixed|null |
||
2190 | */ |
||
2191 | public function avg($column, $cacheTtl=self::CACHE_NO) |
||
2198 | |||
2199 | /*******************************************************************************/ |
||
2200 | // Utilities methods |
||
2201 | |||
2202 | /** |
||
2203 | * Reset fields |
||
2204 | * |
||
2205 | * @return $this |
||
2206 | */ |
||
2207 | public function reset() |
||
2234 | |||
2235 | /** |
||
2236 | * Return a YYYY-MM-DD HH:II:SS date format |
||
2237 | * |
||
2238 | * @param string $datetime - An english textual datetime description |
||
2239 | * now, yesterday, 3 days ago, +1 week |
||
2240 | * http://php.net/manual/en/function.strtotime.php |
||
2241 | * @return string YYYY-MM-DD HH:II:SS |
||
2242 | */ |
||
2243 | public static function NOW($datetime = 'now') |
||
2247 | |||
2248 | /** |
||
2249 | * Return a string containing the given number of question marks, |
||
2250 | * separated by commas. Eg '?, ?, ?' |
||
2251 | * |
||
2252 | * @param int - total of placeholder to insert |
||
2253 | * @return string |
||
2254 | */ |
||
2255 | protected function _makePlaceholders($number_of_placeholders=1) |
||
2259 | |||
2260 | /** |
||
2261 | * Format the table{Primary|Foreign}KeyName |
||
2262 | * |
||
2263 | * @param string $pattern |
||
2264 | * @param string $tableName |
||
2265 | * @return string |
||
2266 | */ |
||
2267 | protected function _formatKeyName($pattern, $tableName) |
||
2271 | |||
2272 | /** |
||
2273 | * @param string $query |
||
2274 | * @param array $parameters |
||
2275 | * |
||
2276 | * @return array |
||
2277 | */ |
||
2278 | protected function _logQuery($query, array $parameters) |
||
2290 | |||
2291 | /** |
||
2292 | * @param $ident |
||
2293 | * @param $builtQuery |
||
2294 | */ |
||
2295 | protected function _logSlowQueries($ident, $builtQuery) |
||
2308 | |||
2309 | /** |
||
2310 | * @return float |
||
2311 | */ |
||
2312 | public function getTimeTaken() |
||
2317 | |||
2318 | /** |
||
2319 | * @param $secs |
||
2320 | * @return $this |
||
2321 | */ |
||
2322 | public function slowQuerySeconds($secs) |
||
2328 | |||
2329 | |||
2330 | /** |
||
2331 | * @param $field |
||
2332 | * @param array $values |
||
2333 | * @param null $placeholder_prefix |
||
2334 | * |
||
2335 | * @return array |
||
2336 | */ |
||
2337 | public function getNamedWhereIn($field, array $values, $placeholder_prefix=null) |
||
2358 | |||
2359 | /** |
||
2360 | * @param $field |
||
2361 | * @param string $delimiter |
||
2362 | * |
||
2363 | * @return array |
||
2364 | */ |
||
2365 | protected function _getColumnAliasParts($field, $delimiter=':') |
||
2374 | |||
2375 | /** |
||
2376 | * @param string $column |
||
2377 | * @param string $term |
||
2378 | * @return $this |
||
2379 | */ |
||
2380 | protected function _addWhereClause($column, $term) |
||
2436 | |||
2437 | public function destroy() |
||
2446 | |||
2447 | public function __destruct() |
||
2451 | |||
2452 | /** @var string */ |
||
2453 | static protected $_model_namespace = ''; |
||
2454 | |||
2455 | /** @var bool */ |
||
2456 | protected $_validation_exceptions = true; |
||
2457 | |||
2458 | /** @var array */ |
||
2459 | protected $_paging_meta = []; |
||
2460 | |||
2461 | /** @var int */ |
||
2462 | protected $_default_max = 100; |
||
2463 | |||
2464 | /** |
||
2465 | * Load a model |
||
2466 | * |
||
2467 | * @param string $model_name |
||
2468 | * @param AbstractPdo $connection |
||
2469 | * @return Model |
||
2470 | * @throws ModelNotFoundException |
||
2471 | */ |
||
2472 | public static function loadModel($model_name, AbstractPdo $connection=null) |
||
2481 | |||
2482 | /** |
||
2483 | * Load a model |
||
2484 | * |
||
2485 | * @param string $table_name |
||
2486 | * @param AbstractPdo $connection |
||
2487 | * @return $this |
||
2488 | */ |
||
2489 | public static function loadTable($table_name, AbstractPdo $connection=null) |
||
2495 | |||
2496 | /** |
||
2497 | * @param string $columnName |
||
2498 | * @param int $cacheTtl |
||
2499 | * @param bool $flushCache |
||
2500 | * @return bool |
||
2501 | */ |
||
2502 | public function columnExists($columnName, $cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2507 | |||
2508 | /** |
||
2509 | * @param int $cacheTtl |
||
2510 | * @param bool $flushCache |
||
2511 | * @return $this |
||
2512 | */ |
||
2513 | public function loadSchemaFromDb($cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2519 | |||
2520 | /** |
||
2521 | * @param int $cacheTtl |
||
2522 | * @param bool $flushCache |
||
2523 | * @return array |
||
2524 | * @throws \Terah\Assert\AssertionFailedException |
||
2525 | */ |
||
2526 | public function getSchemaFromDb($cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2538 | |||
2539 | /** |
||
2540 | * @param string $table |
||
2541 | * @param int $cacheTtl |
||
2542 | * @param bool $flushCache |
||
2543 | * @return array |
||
2544 | */ |
||
2545 | protected function _getColumnsByTableFromDb($table, $cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2560 | |||
2561 | /** |
||
2562 | * @param string $table |
||
2563 | * @return bool |
||
2564 | */ |
||
2565 | public function clearSchemaCache($table) |
||
2569 | |||
2570 | /** |
||
2571 | * @param stdClass $record |
||
2572 | * @return stdClass |
||
2573 | */ |
||
2574 | public function onFetch(stdClass $record) |
||
2583 | |||
2584 | /** |
||
2585 | * @param stdClass $record |
||
2586 | * @return stdClass |
||
2587 | */ |
||
2588 | public function cleanseRecord(stdClass $record) |
||
2604 | |||
2605 | /** |
||
2606 | * @param stdClass $record |
||
2607 | * @param string $type |
||
2608 | * @return stdClass |
||
2609 | */ |
||
2610 | public function beforeSave(stdClass $record, $type) |
||
2618 | |||
2619 | /** |
||
2620 | * @param stdClass $record |
||
2621 | * @param string $type |
||
2622 | * @return stdClass |
||
2623 | */ |
||
2624 | public function afterSave(stdClass $record, $type) |
||
2630 | |||
2631 | /** |
||
2632 | * @param stdClass $record |
||
2633 | * @param string $type |
||
2634 | * @return stdClass |
||
2635 | */ |
||
2636 | public function addDefaultFields(stdClass $record, $type) |
||
2641 | |||
2642 | public function applyGlobalModifiers(stdClass $record, $type) |
||
2647 | |||
2648 | public function removeUnneededFields(\stdClass $record, $type) |
||
2666 | |||
2667 | /** |
||
2668 | * @param array $data |
||
2669 | * @param string $saveType |
||
2670 | * @return array |
||
2671 | */ |
||
2672 | public function cleanseWebData($data, $saveType) |
||
2686 | |||
2687 | /** |
||
2688 | * @return array |
||
2689 | */ |
||
2690 | public function skeleton() |
||
2700 | |||
2701 | |||
2702 | /** |
||
2703 | * @return Closure[] |
||
2704 | */ |
||
2705 | protected function _getFieldHandlers() |
||
2709 | |||
2710 | /** |
||
2711 | * @param bool $toString |
||
2712 | * @return array|string |
||
2713 | */ |
||
2714 | public function getErrors($toString=false) |
||
2727 | |||
2728 | /** |
||
2729 | * @param bool $throw |
||
2730 | * @return $this |
||
2731 | */ |
||
2732 | public function validationExceptions($throw=true) |
||
2737 | |||
2738 | /** |
||
2739 | * @param array $query array('_limit' => int, '_offset' => int, '_order' => string, '_fields' => string, _search) |
||
2740 | * |
||
2741 | * @return $this |
||
2742 | * @throws Exception |
||
2743 | */ |
||
2744 | public function paginate(array $query=[]) |
||
2753 | |||
2754 | /** |
||
2755 | * @param null|string $limit |
||
2756 | * @param null|string|int $offset |
||
2757 | * @return $this |
||
2758 | */ |
||
2759 | protected function _setLimit($limit=null, $offset=null) |
||
2773 | |||
2774 | /** |
||
2775 | * @param null|string|array $fields |
||
2776 | * @return $this |
||
2777 | * @throws Exception |
||
2778 | */ |
||
2779 | protected function _setFields($fields=null) |
||
2826 | |||
2827 | /** |
||
2828 | * @param null|string $orderBy |
||
2829 | * @return $this|FluentPdoModel |
||
2830 | */ |
||
2831 | protected function _setOrderBy($orderBy=null) |
||
2866 | |||
2867 | /** |
||
2868 | * @param null $type |
||
2869 | * @return array |
||
2870 | */ |
||
2871 | public function getPagingMeta($type=null) |
||
2879 | |||
2880 | /** |
||
2881 | * @return $this |
||
2882 | */ |
||
2883 | public function setPagingMeta() |
||
2904 | |||
2905 | /** |
||
2906 | * Take a web request and format a query |
||
2907 | * |
||
2908 | * @param array $query |
||
2909 | * |
||
2910 | * @return $this |
||
2911 | * @throws Exception |
||
2912 | */ |
||
2913 | public function filter(array $query=[]) |
||
2978 | |||
2979 | /** |
||
2980 | * @param string $column |
||
2981 | * @param $displayCol |
||
2982 | * @return string|null |
||
2983 | */ |
||
2984 | protected function _findFieldByQuery($column, $displayCol) |
||
3029 | |||
3030 | public function getSearchableAssociations() |
||
3035 | |||
3036 | /** |
||
3037 | * @param $keys_only |
||
3038 | * @return array |
||
3039 | */ |
||
3040 | |||
3041 | public function columns($keys_only=true) |
||
3045 | |||
3046 | /** |
||
3047 | * @param string $field |
||
3048 | * @param mixed $value |
||
3049 | * @param bool|false $permissive |
||
3050 | * @return float|int|null|string |
||
3051 | */ |
||
3052 | protected function _fixType($field, $value, $permissive=false) |
||
3091 | |||
3092 | /** |
||
3093 | * @param stdClass $record |
||
3094 | * @param string $type |
||
3095 | * @return stdClass |
||
3096 | */ |
||
3097 | public function fixTypes(stdClass $record, $type=null) |
||
3110 | |||
3111 | /** |
||
3112 | * @param stdClass $record |
||
3113 | * @param string $type |
||
3114 | * @return stdClass |
||
3115 | * @throws Exception |
||
3116 | */ |
||
3117 | public function applyHandlers(stdClass $record, $type='INSERT') |
||
3144 | |||
3145 | /** |
||
3146 | * @param string $field |
||
3147 | * @param mixed $value |
||
3148 | * @param null $type |
||
3149 | * @param null $record |
||
3150 | * @return null |
||
3151 | * @throws Exception |
||
3152 | */ |
||
3153 | protected function applyHandler($field, $value, $type=null, $record=null) |
||
3175 | |||
3176 | protected function _compileHandlers() |
||
3184 | } |
||
3185 | |||
3213 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.