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 |
||
36 | class FluentPdoModel |
||
37 | { |
||
38 | const OPERATOR_AND = ' AND '; |
||
39 | const OPERATOR_OR = ' OR '; |
||
40 | const ORDERBY_ASC = 'ASC'; |
||
41 | const ORDERBY_DESC = 'DESC'; |
||
42 | const SAVE_INSERT = 'INSERT'; |
||
43 | const SAVE_UPDATE = 'UPDATE'; |
||
44 | const LEFT_JOIN = 'LEFT'; |
||
45 | const INNER_JOIN = 'INNER'; |
||
46 | const ONE_DAY = 86400; |
||
47 | const ONE_WEEK = 60480060; |
||
48 | const ONE_HOUR = 3600; |
||
49 | const TEN_MINS = 600; |
||
50 | const CACHE_NO = -1; |
||
51 | const CACHE_DEFAULT = 0; |
||
52 | |||
53 | /** @var AbstractPdo $_connection */ |
||
54 | protected $_connection = null; |
||
55 | |||
56 | /** @var string */ |
||
57 | protected $_primary_key = 'id'; |
||
58 | |||
59 | /** @var array */ |
||
60 | protected $_where_parameters = []; |
||
61 | |||
62 | /** @var array */ |
||
63 | protected $_select_fields = []; |
||
64 | |||
65 | /** @var array */ |
||
66 | protected $_join_sources = []; |
||
67 | |||
68 | /** @var array */ |
||
69 | protected $_join_aliases = []; |
||
70 | |||
71 | /** @var array $_associations */ |
||
72 | protected $_associations = [ |
||
73 | 'belongsTo' => [], |
||
74 | ]; |
||
75 | |||
76 | /** @var array */ |
||
77 | protected $_where_conditions = []; |
||
78 | |||
79 | protected $_raw_sql = null; |
||
80 | |||
81 | /** @var int */ |
||
82 | protected $_limit = null; |
||
83 | |||
84 | /** @var int */ |
||
85 | protected $_offset = null; |
||
86 | |||
87 | /** @var array */ |
||
88 | protected $_order_by = []; |
||
89 | |||
90 | /** @var array */ |
||
91 | protected $_group_by = []; |
||
92 | |||
93 | /** @var string */ |
||
94 | protected $_and_or_operator = self::OPERATOR_AND; |
||
95 | |||
96 | /** @var array */ |
||
97 | protected $_having = []; |
||
98 | |||
99 | /** @var bool */ |
||
100 | protected $_wrap_open = false; |
||
101 | |||
102 | /** @var int */ |
||
103 | protected $_last_wrap_position = 0; |
||
104 | |||
105 | /** @var PDOStatement $_pdo_stmt */ |
||
106 | protected $_pdo_stmt = null; |
||
107 | |||
108 | /** @var bool */ |
||
109 | protected $_distinct = false; |
||
110 | |||
111 | /** @var null */ |
||
112 | protected $_requested_fields = []; |
||
113 | |||
114 | /** @var null */ |
||
115 | protected $_filter_meta = []; |
||
116 | |||
117 | /** @var bool */ |
||
118 | protected $_log_queries = false; |
||
119 | |||
120 | /** @var array */ |
||
121 | protected $_timer = []; |
||
122 | |||
123 | /** @var int */ |
||
124 | protected $_slow_query_secs = 5; |
||
125 | |||
126 | /** @var array */ |
||
127 | protected $_pagination_attribs = [ |
||
128 | '_limit', |
||
129 | '_offset', |
||
130 | '_order', |
||
131 | '_fields', |
||
132 | '_search' |
||
133 | ]; |
||
134 | |||
135 | /** @var string $_table_name */ |
||
136 | protected $_table_name = null; |
||
137 | |||
138 | /** @var string $_table_alias */ |
||
139 | protected $_table_alias = null; |
||
140 | |||
141 | /** @var string $_display_column */ |
||
142 | protected $_display_column = null; |
||
143 | |||
144 | /** @var string $_connection_name */ |
||
145 | protected $_connection_name = null; |
||
146 | |||
147 | /** @var array $_schema */ |
||
148 | protected $_schema = []; |
||
149 | |||
150 | /** @var array $_virtual_fields */ |
||
151 | protected $_virtual_fields = []; |
||
152 | |||
153 | /** @var array $_errors */ |
||
154 | protected $_errors = []; |
||
155 | |||
156 | /** |
||
157 | * @var int - true = connection default x days |
||
158 | * - false = no cache |
||
159 | * - int = a specific amount |
||
160 | */ |
||
161 | protected $_cache_ttl = self::CACHE_NO; |
||
162 | |||
163 | /** @var string */ |
||
164 | protected $_tmp_table_prefix = 'tmp_'; |
||
165 | |||
166 | /** @var null|string */ |
||
167 | protected $_built_query = null; |
||
168 | |||
169 | // Make handlers public so whe can delete them externally to reduce memory in hhmv |
||
170 | //protected $_handlers = []; |
||
171 | public $_handlers = []; |
||
172 | |||
173 | /** @var bool User want to directly specify the fields */ |
||
174 | protected $_explicit_select_mode = false; |
||
175 | |||
176 | /** @var string[] */ |
||
177 | protected $_update_raw = []; |
||
178 | |||
179 | protected $_max_callback_failures = null; |
||
180 | |||
181 | protected $_num_callback_failures = 0; |
||
182 | |||
183 | protected $_filter_on_fetch = false; |
||
184 | |||
185 | protected $_log_filter_changes = true; |
||
186 | |||
187 | protected $_include_count = false; |
||
188 | |||
189 | /** |
||
190 | * @param AbstractPdo|null $connection |
||
191 | */ |
||
192 | public function __construct(AbstractPdo $connection=null) |
||
199 | |||
200 | public function init() |
||
202 | |||
203 | /** |
||
204 | * @return AbstractPdo |
||
205 | * @throws Exception |
||
206 | */ |
||
207 | public function getPdo() |
||
211 | |||
212 | /** |
||
213 | * @return AbstractLogger |
||
214 | */ |
||
215 | public function getLogger() |
||
219 | |||
220 | /** |
||
221 | * @return CacheInterface |
||
222 | */ |
||
223 | public function getCache() |
||
227 | |||
228 | /** |
||
229 | * Define the working table and create a new instance |
||
230 | * |
||
231 | * @param string $tableName - Table name |
||
232 | * @param string $alias - The table alias name |
||
233 | * @param string $displayColumn |
||
234 | * @param string $primaryKeyName |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function table($tableName, $alias='', $displayColumn='', $primaryKeyName='id') |
||
246 | |||
247 | /** |
||
248 | * @param string $primaryKeyName |
||
249 | * @return $this |
||
250 | */ |
||
251 | public function primaryKeyName($primaryKeyName) |
||
256 | |||
257 | /** |
||
258 | * @param string $tableName |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function tableName($tableName) |
||
267 | |||
268 | /** |
||
269 | * @param $explicitSelect |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function explicitSelectMode($explicitSelect=true) |
||
278 | |||
279 | /** |
||
280 | * @param bool $filterOnFetch |
||
281 | * |
||
282 | * @return $this |
||
283 | */ |
||
284 | public function filterOnFetch($filterOnFetch=true) |
||
289 | |||
290 | /** |
||
291 | * @param bool $logFilterChanges |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function logFilterChanges($logFilterChanges=true) |
||
300 | |||
301 | /** |
||
302 | * Return the name of the table |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getTableName() |
||
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getDisplayColumn() |
||
318 | |||
319 | /** |
||
320 | * Set the display column |
||
321 | * |
||
322 | * @param string $column |
||
323 | * |
||
324 | * @return $this |
||
325 | */ |
||
326 | public function displayColumn($column) |
||
331 | /** |
||
332 | * Set the table alias |
||
333 | * |
||
334 | * @param string $alias |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function tableAlias($alias) |
||
343 | |||
344 | /** |
||
345 | * @param int $cacheTtl |
||
346 | * @return $this |
||
347 | * @throws Exception |
||
348 | */ |
||
349 | protected function _cacheTtl($cacheTtl) |
||
359 | |||
360 | /** |
||
361 | * @return string |
||
362 | */ |
||
363 | public function getTableAlias() |
||
367 | |||
368 | /** |
||
369 | * @param array $associations |
||
370 | * |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function associations(array $associations) |
||
378 | |||
379 | /** |
||
380 | * @param string $alias |
||
381 | * @param array $definition |
||
382 | * @return $this |
||
383 | */ |
||
384 | public function setBelongsTo($alias, array $definition) |
||
392 | |||
393 | public function setBelongsToDisplayField($alias, $displayField) |
||
402 | |||
403 | /** |
||
404 | * @param PDOStatement $stmt |
||
405 | * |
||
406 | * @param PDOStatement $stmt |
||
407 | * @param Closure $fnCallback |
||
408 | * @return bool|stdClass |
||
409 | */ |
||
410 | public function fetchRow(PDOStatement $stmt, Closure $fnCallback=null) |
||
429 | |||
430 | /** |
||
431 | * @param array $schema |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function schema(array $schema) |
||
440 | |||
441 | /** |
||
442 | * @param string|array $field |
||
443 | * @param $type |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function addSchema($field, $type) |
||
459 | |||
460 | /** |
||
461 | * @param $keys_only |
||
462 | * @return array |
||
463 | */ |
||
464 | public function getColumns($keys_only=true) |
||
468 | |||
469 | /** |
||
470 | * Get the primary key name |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function getPrimaryKeyName() |
||
478 | |||
479 | /** |
||
480 | * @param string $query |
||
481 | * @param array $parameters |
||
482 | * |
||
483 | * @return bool |
||
484 | * @throws Exception |
||
485 | */ |
||
486 | public function execute($query, array $parameters=[]) |
||
509 | |||
510 | /** |
||
511 | * @param string $query |
||
512 | * @param array $params |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function query($query, array $params=[]) |
||
521 | |||
522 | /** |
||
523 | * @return bool |
||
524 | */ |
||
525 | public function begin() |
||
529 | |||
530 | /** |
||
531 | * @return bool |
||
532 | */ |
||
533 | public function commit() |
||
537 | |||
538 | /** |
||
539 | * @return bool |
||
540 | */ |
||
541 | public function rollback() |
||
546 | |||
547 | |||
548 | /** |
||
549 | * @param string $sql |
||
550 | * @param array $params |
||
551 | * |
||
552 | * @return string |
||
553 | */ |
||
554 | public function buildQuery($sql, array $params=[]) |
||
580 | |||
581 | /** |
||
582 | * @param stdClass $record |
||
583 | * |
||
584 | * @return stdClass |
||
585 | */ |
||
586 | protected function _trimAndLowerCaseKeys(stdClass $record) |
||
595 | |||
596 | /** |
||
597 | * Return the number of affected row by the last statement |
||
598 | * |
||
599 | * @return int |
||
600 | */ |
||
601 | public function rowCount() |
||
606 | |||
607 | /** |
||
608 | * @return PDOStatement |
||
609 | * @throws PDOException |
||
610 | */ |
||
611 | public function fetchStmt() |
||
619 | |||
620 | /** |
||
621 | * @param bool $build |
||
622 | * @return array |
||
623 | */ |
||
624 | public function fetchSqlQuery($build=false) |
||
633 | |||
634 | /** |
||
635 | * @param string $table_name |
||
636 | * @param bool $drop_if_exists |
||
637 | * @param array $indexes |
||
638 | * @return boolean |
||
639 | * @throws Exception |
||
640 | */ |
||
641 | public function fetchIntoMemoryTable($table_name, $drop_if_exists=true, array $indexes=[]) |
||
662 | |||
663 | /** |
||
664 | * @param null $keyedOn |
||
665 | * @param int $cacheTtl |
||
666 | * @return \stdClass[]|null |
||
667 | */ |
||
668 | public function fetch($keyedOn=null, $cacheTtl=self::CACHE_NO) |
||
703 | |||
704 | protected function _parseWhereForPrimaryLookup() |
||
719 | |||
720 | /** |
||
721 | * @param string $cacheKey |
||
722 | * @param Closure $func |
||
723 | * @param int $cacheTtl - 0 for default ttl, -1 for no cache or int for custom ttl |
||
724 | * @return mixed|null |
||
725 | */ |
||
726 | protected function _cacheData($cacheKey, Closure $func, $cacheTtl=self::CACHE_DEFAULT) |
||
763 | |||
764 | /** |
||
765 | * @param string $cacheKey |
||
766 | * @return bool |
||
767 | */ |
||
768 | public function clearCache($cacheKey) |
||
772 | |||
773 | /** |
||
774 | * @param string|null $table |
||
775 | * @return bool |
||
776 | */ |
||
777 | public function clearCacheByTable($table=null) |
||
786 | |||
787 | /** |
||
788 | * @param Closure $fnCallback |
||
789 | * @return int |
||
790 | */ |
||
791 | public function fetchCallback(Closure $fnCallback) |
||
799 | |||
800 | /** |
||
801 | * @param Closure $fnCallback |
||
802 | * @param string $keyedOn |
||
803 | * @return array |
||
804 | */ |
||
805 | public function fetchObjectsByCallback(Closure $fnCallback, $keyedOn=null) |
||
821 | |||
822 | /** |
||
823 | * @param $numFailures |
||
824 | * @return $this |
||
825 | */ |
||
826 | public function maxCallbackFailures($numFailures) |
||
832 | |||
833 | /** |
||
834 | * @param PDOStatement $stmt |
||
835 | * @param Closure $fnCallback |
||
836 | * @param int $successCnt |
||
837 | * @return bool|null|stdClass |
||
838 | */ |
||
839 | protected function _tallySuccessCount($stmt, Closure $fnCallback, &$successCnt) |
||
868 | |||
869 | /** |
||
870 | * @param null|string $keyedOn |
||
871 | * @param null|mixed $valueField |
||
872 | * @param int $cacheTtl |
||
873 | * @return mixed |
||
874 | */ |
||
875 | public function fetchList($keyedOn=null, $valueField=null, $cacheTtl=self::CACHE_NO) |
||
924 | |||
925 | /** |
||
926 | * @param string $column |
||
927 | * @param int $cacheTtl |
||
928 | * @param bool|true $unique |
||
929 | * @return array|mixed |
||
930 | */ |
||
931 | public function fetchColumn($column, $cacheTtl=self::CACHE_NO, $unique=true) |
||
940 | |||
941 | /** |
||
942 | * @param null|string $field |
||
943 | * @param null|int $itemId |
||
944 | * @param int $cacheTtl |
||
945 | * @return mixed|null |
||
946 | */ |
||
947 | public function fetchField($field=null, $itemId=null, $cacheTtl=self::CACHE_NO) |
||
969 | |||
970 | /** |
||
971 | * @param int|null $id |
||
972 | * @param int $cacheTtl |
||
973 | * @return \stdClass|bool |
||
974 | */ |
||
975 | public function fetchOne($id=null, $cacheTtl=self::CACHE_NO) |
||
985 | |||
986 | /** |
||
987 | * @param int|null $id |
||
988 | * @param int $cacheTtl |
||
989 | * @return boolean |
||
990 | */ |
||
991 | public function fetchExists($id=null, $cacheTtl=self::CACHE_NO) |
||
999 | |||
1000 | /*------------------------------------------------------------------------------ |
||
1001 | Fluent Query Builder |
||
1002 | *-----------------------------------------------------------------------------*/ |
||
1003 | |||
1004 | /** |
||
1005 | * Create the select clause |
||
1006 | * |
||
1007 | * @param mixed $columns - the column to select. Can be string or array of fields |
||
1008 | * @param string $alias - an alias to the column |
||
1009 | * @param boolean $explicitSelect |
||
1010 | * @return $this |
||
1011 | */ |
||
1012 | public function select($columns='*', $alias=null, $explicitSelect=true) |
||
1052 | |||
1053 | /** |
||
1054 | * @param string $select |
||
1055 | * @return $this |
||
1056 | */ |
||
1057 | public function selectRaw($select) |
||
1062 | |||
1063 | /** |
||
1064 | * @param bool $log_queries |
||
1065 | * |
||
1066 | * @return $this |
||
1067 | */ |
||
1068 | public function logQueries($log_queries=true) |
||
1073 | |||
1074 | /** |
||
1075 | * @param bool $include_count |
||
1076 | * |
||
1077 | * @return $this |
||
1078 | */ |
||
1079 | public function includeCount($include_count=true) |
||
1084 | |||
1085 | /** |
||
1086 | * @param bool $distinct |
||
1087 | * |
||
1088 | * @return $this |
||
1089 | */ |
||
1090 | public function distinct($distinct=true) |
||
1095 | |||
1096 | /** |
||
1097 | * @param array $fields |
||
1098 | * @return $this |
||
1099 | */ |
||
1100 | public function withBelongsTo($fields=[]) |
||
1112 | |||
1113 | /** |
||
1114 | * @param string $alias |
||
1115 | * @param string $type |
||
1116 | * @param bool $addSelectField |
||
1117 | * @return $this |
||
1118 | */ |
||
1119 | public function autoJoin($alias, $type=self::LEFT_JOIN, $addSelectField=true) |
||
1135 | |||
1136 | /** |
||
1137 | * Add where condition, more calls appends with AND |
||
1138 | * |
||
1139 | * @param string $condition possibly containing ? or :name |
||
1140 | * @param mixed $parameters accepted by PDOStatement::execute or a scalar value |
||
1141 | * @param mixed ... |
||
1142 | * @return $this |
||
1143 | */ |
||
1144 | public function where($condition, $parameters = []) |
||
1191 | |||
1192 | /** |
||
1193 | * Create an AND operator in the where clause |
||
1194 | * |
||
1195 | * @return $this |
||
1196 | */ |
||
1197 | public function _and() |
||
1209 | |||
1210 | |||
1211 | /** |
||
1212 | * Create an OR operator in the where clause |
||
1213 | * |
||
1214 | * @return $this |
||
1215 | */ |
||
1216 | public function _or() |
||
1228 | |||
1229 | /** |
||
1230 | * To group multiple where clauses together. |
||
1231 | * |
||
1232 | * @return $this |
||
1233 | */ |
||
1234 | public function wrap() |
||
1243 | |||
1244 | /** |
||
1245 | * Where Primary key |
||
1246 | * |
||
1247 | * @param integer $id |
||
1248 | * @param bool $addAlias |
||
1249 | * |
||
1250 | * @return $this |
||
1251 | */ |
||
1252 | public function wherePk($id, $addAlias=true) |
||
1257 | |||
1258 | /** |
||
1259 | * WHERE $columnName != $value |
||
1260 | * |
||
1261 | * @param string $columnName |
||
1262 | * @param mixed $value |
||
1263 | * @return $this |
||
1264 | */ |
||
1265 | public function whereNot($columnName, $value) |
||
1269 | /** |
||
1270 | * WHERE $columnName != $value |
||
1271 | * |
||
1272 | * @param string $columnName |
||
1273 | * @param mixed $value |
||
1274 | * @return $this |
||
1275 | */ |
||
1276 | public function whereCoercedNot($columnName, $value) |
||
1280 | |||
1281 | /** |
||
1282 | * WHERE $columnName LIKE $value |
||
1283 | * |
||
1284 | * @param string $columnName |
||
1285 | * @param mixed $value |
||
1286 | * @return $this |
||
1287 | */ |
||
1288 | public function whereLike($columnName, $value) |
||
1292 | |||
1293 | /** |
||
1294 | * @param string $columnName |
||
1295 | * @param mixed $value1 |
||
1296 | * @param mixed $value2 |
||
1297 | * @return $this |
||
1298 | */ |
||
1299 | public function whereBetween($columnName, $value1, $value2) |
||
1303 | |||
1304 | /** |
||
1305 | * @param string $columnName |
||
1306 | * @param mixed $value1 |
||
1307 | * @param mixed $value2 |
||
1308 | * @return $this |
||
1309 | */ |
||
1310 | public function whereNotBetween($columnName, $value1, $value2) |
||
1314 | |||
1315 | /** |
||
1316 | * @param string $columnName |
||
1317 | * @param string $regex |
||
1318 | * @return $this |
||
1319 | */ |
||
1320 | public function whereRegex($columnName, $regex) |
||
1324 | |||
1325 | /** |
||
1326 | * @param string $columnName |
||
1327 | * @param string $regex |
||
1328 | * @return $this |
||
1329 | */ |
||
1330 | public function whereNotRegex($columnName, $regex) |
||
1334 | |||
1335 | /** |
||
1336 | * WHERE $columnName NOT LIKE $value |
||
1337 | * |
||
1338 | * @param string $columnName |
||
1339 | * @param mixed $value |
||
1340 | * @return $this |
||
1341 | */ |
||
1342 | public function whereNotLike($columnName, $value) |
||
1346 | |||
1347 | /** |
||
1348 | * WHERE $columnName > $value |
||
1349 | * |
||
1350 | * @param string $columnName |
||
1351 | * @param mixed $value |
||
1352 | * @return $this |
||
1353 | */ |
||
1354 | public function whereGt($columnName, $value) |
||
1358 | |||
1359 | /** |
||
1360 | * WHERE $columnName >= $value |
||
1361 | * |
||
1362 | * @param string $columnName |
||
1363 | * @param mixed $value |
||
1364 | * @return $this |
||
1365 | */ |
||
1366 | public function whereGte($columnName, $value) |
||
1370 | |||
1371 | /** |
||
1372 | * WHERE $columnName < $value |
||
1373 | * |
||
1374 | * @param string $columnName |
||
1375 | * @param mixed $value |
||
1376 | * @return $this |
||
1377 | */ |
||
1378 | public function whereLt($columnName, $value) |
||
1382 | |||
1383 | /** |
||
1384 | * WHERE $columnName <= $value |
||
1385 | * |
||
1386 | * @param string $columnName |
||
1387 | * @param mixed $value |
||
1388 | * @return $this |
||
1389 | */ |
||
1390 | public function whereLte($columnName, $value) |
||
1394 | |||
1395 | /** |
||
1396 | * WHERE $columnName IN (?,?,?,...) |
||
1397 | * |
||
1398 | * @param string $columnName |
||
1399 | * @param array $values |
||
1400 | * @return $this |
||
1401 | */ |
||
1402 | public function whereIn($columnName, array $values) |
||
1406 | |||
1407 | /** |
||
1408 | * WHERE $columnName NOT IN (?,?,?,...) |
||
1409 | * |
||
1410 | * @param string $columnName |
||
1411 | * @param array $values |
||
1412 | * @return $this |
||
1413 | */ |
||
1414 | public function whereNotIn($columnName, array $values) |
||
1419 | |||
1420 | /** |
||
1421 | * WHERE $columnName IS NULL |
||
1422 | * |
||
1423 | * @param string $columnName |
||
1424 | * @return $this |
||
1425 | */ |
||
1426 | public function whereNull($columnName) |
||
1430 | |||
1431 | /** |
||
1432 | * WHERE $columnName IS NOT NULL |
||
1433 | * |
||
1434 | * @param string $columnName |
||
1435 | * @return $this |
||
1436 | */ |
||
1437 | public function whereNotNull($columnName) |
||
1441 | |||
1442 | /** |
||
1443 | * @param string $statement |
||
1444 | * @param string $operator |
||
1445 | * @return $this |
||
1446 | */ |
||
1447 | public function having($statement, $operator = self::OPERATOR_AND) |
||
1455 | |||
1456 | /** |
||
1457 | * ORDER BY $columnName (ASC | DESC) |
||
1458 | * |
||
1459 | * @param string $columnName - The name of the column or an expression |
||
1460 | * @param string $ordering (DESC | ASC) |
||
1461 | * @return $this |
||
1462 | */ |
||
1463 | public function orderBy($columnName, $ordering=null) |
||
1474 | |||
1475 | /** |
||
1476 | * GROUP BY $columnName |
||
1477 | * |
||
1478 | * @param string $columnName |
||
1479 | * @return $this |
||
1480 | */ |
||
1481 | public function groupBy($columnName) |
||
1490 | |||
1491 | |||
1492 | /** |
||
1493 | * LIMIT $limit |
||
1494 | * |
||
1495 | * @param int $limit |
||
1496 | * @param int|null $offset |
||
1497 | * @return $this |
||
1498 | */ |
||
1499 | public function limit($limit, $offset=null) |
||
1508 | |||
1509 | /** |
||
1510 | * Return the limit |
||
1511 | * |
||
1512 | * @return integer |
||
1513 | */ |
||
1514 | public function getLimit() |
||
1518 | |||
1519 | /** |
||
1520 | * OFFSET $offset |
||
1521 | * |
||
1522 | * @param int $offset |
||
1523 | * @return $this |
||
1524 | */ |
||
1525 | public function offset($offset) |
||
1530 | |||
1531 | /** |
||
1532 | * Return the offset |
||
1533 | * |
||
1534 | * @return integer |
||
1535 | */ |
||
1536 | public function getOffset() |
||
1540 | |||
1541 | /** |
||
1542 | * Build a join |
||
1543 | * |
||
1544 | * @param string $table - The table name |
||
1545 | * @param string $constraint -> id = profile.user_id |
||
1546 | * @param string $tableAlias - The alias of the table name |
||
1547 | * @param string $joinOperator - LEFT | INNER | etc... |
||
1548 | * @return $this |
||
1549 | */ |
||
1550 | public function join($table, $constraint=null, $tableAlias = null, $joinOperator = '') |
||
1568 | |||
1569 | /** |
||
1570 | * Create a left join |
||
1571 | * |
||
1572 | * @param string $table |
||
1573 | * @param string $constraint |
||
1574 | * @param string $tableAlias |
||
1575 | * @return $this |
||
1576 | */ |
||
1577 | public function leftJoin($table, $constraint, $tableAlias=null) |
||
1581 | |||
1582 | |||
1583 | /** |
||
1584 | * Return the build select query |
||
1585 | * |
||
1586 | * @return string |
||
1587 | */ |
||
1588 | public function getSelectQuery() |
||
1630 | |||
1631 | /** |
||
1632 | * Prepare columns to include the table alias name |
||
1633 | * @param array $columns |
||
1634 | * @return array |
||
1635 | */ |
||
1636 | protected function _prepareColumns(array $columns) |
||
1665 | |||
1666 | /** |
||
1667 | * Build the WHERE clause(s) |
||
1668 | * |
||
1669 | * @param bool $purgeAliases |
||
1670 | * @return string |
||
1671 | */ |
||
1672 | protected function _getWhereString($purgeAliases=false) |
||
1704 | |||
1705 | /** |
||
1706 | * Return the HAVING clause |
||
1707 | * |
||
1708 | * @return string |
||
1709 | */ |
||
1710 | protected function _getHavingString() |
||
1728 | |||
1729 | /** |
||
1730 | * Return the values to be bound for where |
||
1731 | * |
||
1732 | * @param bool $purgeAliases |
||
1733 | * @return array |
||
1734 | */ |
||
1735 | protected function _getWhereParameters($purgeAliases=false) |
||
1740 | |||
1741 | /** |
||
1742 | * Insert new rows |
||
1743 | * $records can be a stdClass or an array of stdClass to add a bulk insert |
||
1744 | * If a single row is inserted, it will return it's row instance |
||
1745 | * |
||
1746 | * @param stdClass|array $records |
||
1747 | * @return int|stdClass|bool |
||
1748 | * @throws Exception |
||
1749 | */ |
||
1750 | public function insert($records) |
||
1780 | |||
1781 | /** |
||
1782 | * @param string $name |
||
1783 | * @return int |
||
1784 | */ |
||
1785 | public function getLastInsertId($name=null) |
||
1789 | |||
1790 | /** |
||
1791 | * @param $records |
||
1792 | * @return array |
||
1793 | */ |
||
1794 | public function insertSqlQuery($records) |
||
1814 | |||
1815 | /** |
||
1816 | * @param $data |
||
1817 | * @param array $matchOn |
||
1818 | * @param bool $returnObj |
||
1819 | * @return bool|int|stdClass |
||
1820 | */ |
||
1821 | public function upsert($data, array $matchOn=[], $returnObj=false) |
||
1842 | |||
1843 | /** |
||
1844 | * @param stdClass $object |
||
1845 | * @param array $matchOn |
||
1846 | * @param bool $returnObj |
||
1847 | * @return bool|int|stdClass |
||
1848 | */ |
||
1849 | public function upsertOne(stdClass $object, array $matchOn=[], $returnObj=false) |
||
1887 | |||
1888 | /** |
||
1889 | * @param array $data |
||
1890 | * @param array $matchOn |
||
1891 | * @param bool|false $returnObj |
||
1892 | * @return bool|int|stdClass |
||
1893 | */ |
||
1894 | public function upsertArr(array $data, array $matchOn=[], $returnObj=false) |
||
1898 | |||
1899 | /** |
||
1900 | * Update entries |
||
1901 | * Use the query builder to create the where clause |
||
1902 | * |
||
1903 | * @param stdClass $record |
||
1904 | * @param bool $updateAll |
||
1905 | * @return bool|int |
||
1906 | * @throws Exception |
||
1907 | */ |
||
1908 | public function update(stdClass $record, $updateAll=false) |
||
1930 | |||
1931 | /** |
||
1932 | * @param array $record |
||
1933 | * @param bool|false $updateAll |
||
1934 | * @return bool|int |
||
1935 | * @throws Exception |
||
1936 | */ |
||
1937 | public function updateArr(array $record, $updateAll=false) |
||
1941 | |||
1942 | /** |
||
1943 | * @param array $record |
||
1944 | * @return bool|int|stdClass |
||
1945 | */ |
||
1946 | public function insertArr(array $record) |
||
1950 | |||
1951 | /** |
||
1952 | * @param int $field |
||
1953 | * @param mixed $value |
||
1954 | * @param null $id |
||
1955 | * @param bool|false $updateAll |
||
1956 | * @return bool|int |
||
1957 | * @throws Exception |
||
1958 | */ |
||
1959 | public function updateField($field, $value, $id=null, $updateAll=false) |
||
1967 | |||
1968 | /** |
||
1969 | * @param int $field |
||
1970 | * @param mixed $value |
||
1971 | * @param null $id |
||
1972 | * @param bool|false $updateAll |
||
1973 | * @return bool|int |
||
1974 | * @throws Exception |
||
1975 | */ |
||
1976 | public function concatField($field, $value, $id=null, $updateAll=false) |
||
1980 | |||
1981 | /** |
||
1982 | * @param stdClass $record |
||
1983 | * @return bool|int |
||
1984 | * @throws Exception |
||
1985 | */ |
||
1986 | public function updateChanged(stdClass $record) |
||
1999 | |||
2000 | /** |
||
2001 | * @param string $expression |
||
2002 | * @param array $params |
||
2003 | * @return $this |
||
2004 | */ |
||
2005 | public function updateByExpression($expression, array $params) |
||
2010 | |||
2011 | /** |
||
2012 | * @param array $data |
||
2013 | * @return int |
||
2014 | * @throws Exception |
||
2015 | */ |
||
2016 | public function rawUpdate(array $data=[]) |
||
2024 | |||
2025 | /** |
||
2026 | * @param stdClass $record |
||
2027 | * @return array |
||
2028 | */ |
||
2029 | public function updateSqlQuery(stdClass $record) |
||
2039 | |||
2040 | /** |
||
2041 | * @param $record |
||
2042 | * @return array |
||
2043 | */ |
||
2044 | protected function updateSql(array $record) |
||
2074 | |||
2075 | /** |
||
2076 | * Delete rows |
||
2077 | * Use the query builder to create the where clause |
||
2078 | * @param bool $deleteAll = When there is no where condition, setting to true will delete all |
||
2079 | * @return int - total affected rows |
||
2080 | * @throws Exception |
||
2081 | */ |
||
2082 | public function delete($deleteAll=false) |
||
2092 | |||
2093 | /** |
||
2094 | * @param bool|false $force |
||
2095 | * @return $this |
||
2096 | * @throws Exception |
||
2097 | */ |
||
2098 | public function truncate($force=false) |
||
2111 | |||
2112 | /** |
||
2113 | * @return array |
||
2114 | */ |
||
2115 | public function deleteSqlQuery() |
||
2125 | |||
2126 | |||
2127 | /** |
||
2128 | * Return the aggregate count of column |
||
2129 | * |
||
2130 | * @param string $column |
||
2131 | * @param int $cacheTtl |
||
2132 | * @return int |
||
2133 | */ |
||
2134 | public function count($column='*', $cacheTtl=self::CACHE_NO) |
||
2141 | |||
2142 | |||
2143 | /** |
||
2144 | * Return the aggregate max count of column |
||
2145 | * |
||
2146 | * @param string $column |
||
2147 | * @param int $cacheTtl |
||
2148 | * @return mixed|null |
||
2149 | */ |
||
2150 | public function max($column, $cacheTtl=self::CACHE_NO) |
||
2157 | |||
2158 | |||
2159 | /** |
||
2160 | * Return the aggregate min count of column |
||
2161 | * |
||
2162 | * @param string $column |
||
2163 | * @param int $cacheTtl |
||
2164 | * @return mixed|null |
||
2165 | */ |
||
2166 | public function min($column, $cacheTtl=self::CACHE_NO) |
||
2173 | |||
2174 | /** |
||
2175 | * Return the aggregate sum count of column |
||
2176 | * |
||
2177 | * @param string $column |
||
2178 | * @param int $cacheTtl |
||
2179 | * @return mixed|null |
||
2180 | */ |
||
2181 | public function sum($column, $cacheTtl=self::CACHE_NO) |
||
2188 | |||
2189 | /** |
||
2190 | * Return the aggregate average count of column |
||
2191 | * |
||
2192 | * @param string $column |
||
2193 | * @param int $cacheTtl |
||
2194 | * @return mixed|null |
||
2195 | */ |
||
2196 | public function avg($column, $cacheTtl=self::CACHE_NO) |
||
2203 | |||
2204 | /*******************************************************************************/ |
||
2205 | // Utilities methods |
||
2206 | |||
2207 | /** |
||
2208 | * Reset fields |
||
2209 | * |
||
2210 | * @return $this |
||
2211 | */ |
||
2212 | public function reset() |
||
2239 | |||
2240 | /** |
||
2241 | * Return a YYYY-MM-DD HH:II:SS date format |
||
2242 | * |
||
2243 | * @param string $datetime - An english textual datetime description |
||
2244 | * now, yesterday, 3 days ago, +1 week |
||
2245 | * http://php.net/manual/en/function.strtotime.php |
||
2246 | * @return string YYYY-MM-DD HH:II:SS |
||
2247 | */ |
||
2248 | public static function NOW($datetime = 'now') |
||
2252 | |||
2253 | /** |
||
2254 | * Return a string containing the given number of question marks, |
||
2255 | * separated by commas. Eg '?, ?, ?' |
||
2256 | * |
||
2257 | * @param int - total of placeholder to insert |
||
2258 | * @return string |
||
2259 | */ |
||
2260 | protected function _makePlaceholders($number_of_placeholders=1) |
||
2264 | |||
2265 | /** |
||
2266 | * Format the table{Primary|Foreign}KeyName |
||
2267 | * |
||
2268 | * @param string $pattern |
||
2269 | * @param string $tableName |
||
2270 | * @return string |
||
2271 | */ |
||
2272 | protected function _formatKeyName($pattern, $tableName) |
||
2276 | |||
2277 | /** |
||
2278 | * @param string $query |
||
2279 | * @param array $parameters |
||
2280 | * |
||
2281 | * @return array |
||
2282 | */ |
||
2283 | protected function _logQuery($query, array $parameters) |
||
2295 | |||
2296 | /** |
||
2297 | * @param $ident |
||
2298 | * @param $builtQuery |
||
2299 | */ |
||
2300 | protected function _logSlowQueries($ident, $builtQuery) |
||
2313 | |||
2314 | /** |
||
2315 | * @return float |
||
2316 | */ |
||
2317 | public function getTimeTaken() |
||
2322 | |||
2323 | /** |
||
2324 | * @param $secs |
||
2325 | * @return $this |
||
2326 | */ |
||
2327 | public function slowQuerySeconds($secs) |
||
2333 | |||
2334 | |||
2335 | /** |
||
2336 | * @param $field |
||
2337 | * @param array $values |
||
2338 | * @param null $placeholder_prefix |
||
2339 | * |
||
2340 | * @return array |
||
2341 | */ |
||
2342 | public function getNamedWhereIn($field, array $values, $placeholder_prefix=null) |
||
2363 | |||
2364 | /** |
||
2365 | * @param $field |
||
2366 | * @param string $delimiter |
||
2367 | * |
||
2368 | * @return array |
||
2369 | */ |
||
2370 | protected function _getColumnAliasParts($field, $delimiter=':') |
||
2379 | |||
2380 | /** |
||
2381 | * @param string $column |
||
2382 | * @param string $term |
||
2383 | * @return $this |
||
2384 | */ |
||
2385 | protected function _addWhereClause($column, $term) |
||
2441 | |||
2442 | public function destroy() |
||
2451 | |||
2452 | public function __destruct() |
||
2456 | |||
2457 | /** @var string */ |
||
2458 | static protected $_model_namespace = ''; |
||
2459 | |||
2460 | /** @var bool */ |
||
2461 | protected $_validation_exceptions = true; |
||
2462 | |||
2463 | /** @var array */ |
||
2464 | protected $_paging_meta = []; |
||
2465 | |||
2466 | /** @var int */ |
||
2467 | protected $_default_max = 100; |
||
2468 | |||
2469 | /** |
||
2470 | * Load a model |
||
2471 | * |
||
2472 | * @param string $model_name |
||
2473 | * @param AbstractPdo $connection |
||
2474 | * @return FluentPdoModel |
||
2475 | * @throws ModelNotFoundException |
||
2476 | */ |
||
2477 | public static function loadModel($model_name, AbstractPdo $connection=null) |
||
2486 | |||
2487 | /** |
||
2488 | * Load a model |
||
2489 | * |
||
2490 | * @param string $table_name |
||
2491 | * @param AbstractPdo $connection |
||
2492 | * @return $this |
||
2493 | */ |
||
2494 | public static function loadTable($table_name, AbstractPdo $connection=null) |
||
2500 | |||
2501 | /** |
||
2502 | * @param string $columnName |
||
2503 | * @param int $cacheTtl |
||
2504 | * @param bool $flushCache |
||
2505 | * @return bool |
||
2506 | */ |
||
2507 | public function columnExists($columnName, $cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2512 | |||
2513 | /** |
||
2514 | * @param int $cacheTtl |
||
2515 | * @param bool $flushCache |
||
2516 | * @return $this |
||
2517 | */ |
||
2518 | public function loadSchemaFromDb($cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2524 | |||
2525 | /** |
||
2526 | * @param int $cacheTtl |
||
2527 | * @param bool $flushCache |
||
2528 | * @return array |
||
2529 | * @throws \Terah\Assert\AssertionFailedException |
||
2530 | */ |
||
2531 | public function getSchemaFromDb($cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2543 | |||
2544 | /** |
||
2545 | * @param string $table |
||
2546 | * @param int $cacheTtl |
||
2547 | * @param bool $flushCache |
||
2548 | * @return array |
||
2549 | */ |
||
2550 | protected function _getColumnsByTableFromDb($table, $cacheTtl=self::CACHE_NO, $flushCache=false) |
||
2565 | |||
2566 | /** |
||
2567 | * @param string $table |
||
2568 | * @return bool |
||
2569 | */ |
||
2570 | public function clearSchemaCache($table) |
||
2574 | |||
2575 | /** |
||
2576 | * @param stdClass $record |
||
2577 | * @return stdClass |
||
2578 | */ |
||
2579 | public function onFetch(stdClass $record) |
||
2588 | |||
2589 | /** |
||
2590 | * @param stdClass $record |
||
2591 | * @return stdClass |
||
2592 | */ |
||
2593 | public function cleanseRecord(stdClass $record) |
||
2609 | |||
2610 | /** |
||
2611 | * @param stdClass $record |
||
2612 | * @param string $type |
||
2613 | * @return stdClass |
||
2614 | */ |
||
2615 | public function beforeSave(stdClass $record, $type) |
||
2623 | |||
2624 | /** |
||
2625 | * @param stdClass $record |
||
2626 | * @param string $type |
||
2627 | * @return stdClass |
||
2628 | */ |
||
2629 | public function afterSave(stdClass $record, $type) |
||
2635 | |||
2636 | /** |
||
2637 | * @param stdClass $record |
||
2638 | * @param string $type |
||
2639 | * @return stdClass |
||
2640 | */ |
||
2641 | public function addDefaultFields(stdClass $record, $type) |
||
2646 | |||
2647 | /** |
||
2648 | * @param stdClass $record |
||
2649 | * @param string $type |
||
2650 | * @return stdClass |
||
2651 | */ |
||
2652 | public function applyGlobalModifiers(stdClass $record, $type) |
||
2657 | |||
2658 | /** |
||
2659 | * @param stdClass $record |
||
2660 | * @param string $type |
||
2661 | * @return stdClass |
||
2662 | */ |
||
2663 | public function removeUnneededFields(\stdClass $record, $type) |
||
2681 | |||
2682 | /** |
||
2683 | * @param array $data |
||
2684 | * @param string $saveType |
||
2685 | * @return array |
||
2686 | */ |
||
2687 | public function cleanseWebData($data, $saveType) |
||
2701 | |||
2702 | /** |
||
2703 | * @return array |
||
2704 | */ |
||
2705 | public function skeleton() |
||
2715 | |||
2716 | |||
2717 | /** |
||
2718 | * @return Closure[] |
||
2719 | */ |
||
2720 | protected function _getFieldHandlers() |
||
2724 | |||
2725 | /** |
||
2726 | * @param bool $toString |
||
2727 | * @return array|string |
||
2728 | */ |
||
2729 | public function getErrors($toString=false) |
||
2742 | |||
2743 | /** |
||
2744 | * @param bool $throw |
||
2745 | * @return $this |
||
2746 | */ |
||
2747 | public function validationExceptions($throw=true) |
||
2752 | |||
2753 | /** |
||
2754 | * @param array $query array('_limit' => int, '_offset' => int, '_order' => string, '_fields' => string, _search) |
||
2755 | * |
||
2756 | * @return $this |
||
2757 | * @throws Exception |
||
2758 | */ |
||
2759 | public function paginate(array $query=[]) |
||
2768 | |||
2769 | /** |
||
2770 | * @param null|string $limit |
||
2771 | * @param null|string|int $offset |
||
2772 | * @return $this |
||
2773 | */ |
||
2774 | protected function _setLimit($limit=null, $offset=null) |
||
2788 | |||
2789 | /** |
||
2790 | * @param null|string|array $fields |
||
2791 | * @return $this |
||
2792 | * @throws Exception |
||
2793 | */ |
||
2794 | protected function _setFields($fields=null) |
||
2841 | |||
2842 | /** |
||
2843 | * @param null|string $orderBy |
||
2844 | * @return $this|FluentPdoModel |
||
2845 | */ |
||
2846 | protected function _setOrderBy($orderBy=null) |
||
2881 | |||
2882 | /** |
||
2883 | * @param null $type |
||
2884 | * @return array |
||
2885 | */ |
||
2886 | public function getPagingMeta($type=null) |
||
2894 | |||
2895 | /** |
||
2896 | * @return $this |
||
2897 | */ |
||
2898 | public function setPagingMeta() |
||
2919 | |||
2920 | /** |
||
2921 | * Take a web request and format a query |
||
2922 | * |
||
2923 | * @param array $query |
||
2924 | * |
||
2925 | * @return $this |
||
2926 | * @throws Exception |
||
2927 | */ |
||
2928 | public function filter(array $query=[]) |
||
2993 | |||
2994 | /** |
||
2995 | * @param string $column |
||
2996 | * @param string $displayCol |
||
2997 | * @return string|null |
||
2998 | */ |
||
2999 | protected function _findFieldByQuery($column, $displayCol) |
||
3044 | |||
3045 | public function getSearchableAssociations() |
||
3050 | |||
3051 | /** |
||
3052 | * @param $keys_only |
||
3053 | * @return array |
||
3054 | */ |
||
3055 | |||
3056 | public function columns($keys_only=true) |
||
3060 | |||
3061 | /** |
||
3062 | * @param string $field |
||
3063 | * @param mixed $value |
||
3064 | * @param bool|false $permissive |
||
3065 | * @return float|int|null|string |
||
3066 | */ |
||
3067 | protected function _fixType($field, $value, $permissive=false) |
||
3106 | |||
3107 | /** |
||
3108 | * @param stdClass $record |
||
3109 | * @param string $type |
||
3110 | * @return stdClass |
||
3111 | */ |
||
3112 | public function fixTypes(stdClass $record, $type=null) |
||
3125 | |||
3126 | /** |
||
3127 | * @param stdClass $record |
||
3128 | * @param string $type |
||
3129 | * @return stdClass |
||
3130 | * @throws Exception |
||
3131 | */ |
||
3132 | public function applyHandlers(stdClass $record, $type='INSERT') |
||
3159 | |||
3160 | /** |
||
3161 | * @param string $field |
||
3162 | * @param mixed $value |
||
3163 | * @param string $type |
||
3164 | * @param stdClass $record |
||
3165 | * @return null |
||
3166 | * @throws Exception |
||
3167 | */ |
||
3168 | protected function applyHandler($field, $value, $type=null, $record=null) |
||
3190 | |||
3191 | protected function _compileHandlers() |
||
3199 | } |
||
3200 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: