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 declare(strict_types=1); |
||
| 34 | class FluentPdoModel |
||
| 35 | { |
||
| 36 | const ACTIVE = 1; |
||
| 37 | const INACTIVE = 0; |
||
| 38 | const ARCHIVED = -1; |
||
| 39 | const GZIP_PREFIX = 'gzipped|'; |
||
| 40 | const OPERATOR_AND = ' AND '; |
||
| 41 | const OPERATOR_OR = ' OR '; |
||
| 42 | const ORDERBY_ASC = 'ASC'; |
||
| 43 | const ORDERBY_DESC = 'DESC'; |
||
| 44 | const SAVE_INSERT = 'INSERT'; |
||
| 45 | const SAVE_UPDATE = 'UPDATE'; |
||
| 46 | const LEFT_JOIN = 'LEFT'; |
||
| 47 | const INNER_JOIN = 'INNER'; |
||
| 48 | const ONE_DAY = 86400; |
||
| 49 | const ONE_WEEK = 60480060; |
||
| 50 | const ONE_HOUR = 3600; |
||
| 51 | const TEN_MINS = 600; |
||
| 52 | const CACHE_NO = -1; |
||
| 53 | const CACHE_DEFAULT = 0; |
||
| 54 | |||
| 55 | /** @var AbstractPdo $_connection */ |
||
| 56 | protected $_connection = null; |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | protected $_primary_key = 'id'; |
||
| 60 | |||
| 61 | /** @var array */ |
||
| 62 | protected $_where_parameters = []; |
||
| 63 | |||
| 64 | /** @var array */ |
||
| 65 | protected $_select_fields = []; |
||
| 66 | |||
| 67 | /** @var array */ |
||
| 68 | protected $_join_sources = []; |
||
| 69 | |||
| 70 | /** @var array */ |
||
| 71 | protected $_join_aliases = []; |
||
| 72 | |||
| 73 | /** @var array $_associations */ |
||
| 74 | protected $_associations = [ |
||
| 75 | 'belongsTo' => [], |
||
| 76 | ]; |
||
| 77 | |||
| 78 | /** @var array */ |
||
| 79 | protected $_where_conditions = []; |
||
| 80 | |||
| 81 | protected $_raw_sql = ''; |
||
| 82 | |||
| 83 | /** @var int */ |
||
| 84 | protected $_limit = 0; |
||
| 85 | |||
| 86 | /** @var int */ |
||
| 87 | protected $_offset = 0; |
||
| 88 | |||
| 89 | /** @var array */ |
||
| 90 | protected $_order_by = []; |
||
| 91 | |||
| 92 | /** @var array */ |
||
| 93 | protected $_group_by = []; |
||
| 94 | |||
| 95 | /** @var string */ |
||
| 96 | protected $_and_or_operator = self::OPERATOR_AND; |
||
| 97 | |||
| 98 | /** @var array */ |
||
| 99 | protected $_having = []; |
||
| 100 | |||
| 101 | /** @var bool */ |
||
| 102 | protected $_wrap_open = false; |
||
| 103 | |||
| 104 | /** @var int */ |
||
| 105 | protected $_last_wrap_position = 0; |
||
| 106 | |||
| 107 | /** @var PDOStatement $_pdo_stmt */ |
||
| 108 | protected $_pdo_stmt = null; |
||
| 109 | |||
| 110 | /** @var bool */ |
||
| 111 | protected $_distinct = false; |
||
| 112 | |||
| 113 | /** @var null */ |
||
| 114 | protected $_requested_fields = []; |
||
| 115 | |||
| 116 | /** @var null */ |
||
| 117 | protected $_filter_meta = []; |
||
| 118 | |||
| 119 | /** @var bool */ |
||
| 120 | protected $_log_queries = false; |
||
| 121 | |||
| 122 | /** @var array */ |
||
| 123 | protected $_timer = []; |
||
| 124 | |||
| 125 | /** @var int */ |
||
| 126 | protected $_slow_query_secs = 5; |
||
| 127 | |||
| 128 | /** @var array */ |
||
| 129 | protected $_pagination_attribs = [ |
||
| 130 | '_limit', |
||
| 131 | '_offset', |
||
| 132 | '_order', |
||
| 133 | '_fields', |
||
| 134 | '_search' |
||
| 135 | ]; |
||
| 136 | |||
| 137 | /** @var string $_table_name */ |
||
| 138 | protected $_table_name = ''; |
||
| 139 | |||
| 140 | /** @var string $_table_alias */ |
||
| 141 | protected $_table_alias = ''; |
||
| 142 | |||
| 143 | /** @var string $_display_column */ |
||
| 144 | protected $_display_column = ''; |
||
| 145 | |||
| 146 | /** @var string $_connection_name */ |
||
| 147 | protected $_connection_name = ''; |
||
| 148 | |||
| 149 | /** @var array $_schema */ |
||
| 150 | protected $_schema = []; |
||
| 151 | |||
| 152 | /** @var array $_virtual_fields */ |
||
| 153 | protected $_virtual_fields = []; |
||
| 154 | |||
| 155 | /** @var array $_errors */ |
||
| 156 | protected $_errors = []; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @var int - true = connection default x days |
||
| 160 | * - false = no cache |
||
| 161 | * - int = a specific amount |
||
| 162 | */ |
||
| 163 | protected $_cache_ttl = self::CACHE_NO; |
||
| 164 | |||
| 165 | /** @var string */ |
||
| 166 | protected $_tmp_table_prefix = 'tmp_'; |
||
| 167 | |||
| 168 | /** @var null|string */ |
||
| 169 | protected $_built_query = ''; |
||
| 170 | |||
| 171 | /** @var array */ |
||
| 172 | protected $_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 = -1; |
||
| 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 | /** @var string */ |
||
| 191 | static protected $_model_namespace = ''; |
||
| 192 | |||
| 193 | /** @var bool */ |
||
| 194 | protected $_validation_exceptions = true; |
||
| 195 | |||
| 196 | /** @var array */ |
||
| 197 | protected $_paging_meta = []; |
||
| 198 | |||
| 199 | /** @var bool */ |
||
| 200 | protected $_soft_deletes = true; |
||
| 201 | |||
| 202 | |||
| 203 | /** @var bool */ |
||
| 204 | protected $_allow_meta_override = false; |
||
| 205 | |||
| 206 | /** @var int */ |
||
| 207 | protected $_default_max = 250; |
||
| 208 | |||
| 209 | /** @var array */ |
||
| 210 | protected $removeUnauthorisedFields = []; |
||
| 211 | |||
| 212 | protected $_can_generic_update = true; |
||
| 213 | protected $_can_generic_add = true; |
||
| 214 | protected $_can_generic_delete = true; |
||
| 215 | |||
| 216 | /** @var array */ |
||
| 217 | protected $row_meta_data = []; |
||
| 218 | |||
| 219 | /** @var array */ |
||
| 220 | protected $globalRemoveUnauthorisedFields = [ |
||
| 221 | '/global_table_meta#view' => [ |
||
| 222 | 'created_by_id', |
||
| 223 | 'created_by', |
||
| 224 | 'created_ts', |
||
| 225 | 'modified_by_id', |
||
| 226 | 'modified_by', |
||
| 227 | 'modified_ts', |
||
| 228 | 'status', |
||
| 229 | ], |
||
| 230 | ]; |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * @param AbstractPdo|null $connection |
||
| 235 | */ |
||
| 236 | public function __construct(AbstractPdo $connection=null) |
||
| 243 | |||
| 244 | public function init() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return AbstractPdo |
||
| 249 | * @throws Exception |
||
| 250 | */ |
||
| 251 | public function getPdo() : AbstractPdo |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return LoggerInterface |
||
| 258 | */ |
||
| 259 | public function getLogger() : LoggerInterface |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return CacheInterface |
||
| 266 | */ |
||
| 267 | public function getCache() : CacheInterface |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Define the working table and create a new instance |
||
| 274 | * |
||
| 275 | * @param string $tableName - Table name |
||
| 276 | * @param string $alias - The table alias name |
||
| 277 | * @param string $displayColumn |
||
| 278 | * @param string $primaryKeyName |
||
| 279 | * |
||
| 280 | * @return FluentPdoModel|$this |
||
| 281 | */ |
||
| 282 | public function table(string $tableName, string $alias='', string $displayColumn='', string $primaryKeyName='id') : FluentPdoModel |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $primaryKeyName |
||
| 293 | * @return FluentPdoModel|$this |
||
| 294 | */ |
||
| 295 | public function primaryKeyName(string $primaryKeyName) : FluentPdoModel |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $tableName |
||
| 304 | * |
||
| 305 | * @return FluentPdoModel|$this |
||
| 306 | */ |
||
| 307 | public function tableName(string $tableName) : FluentPdoModel |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $explicitSelect |
||
| 316 | * |
||
| 317 | * @return FluentPdoModel|$this |
||
| 318 | */ |
||
| 319 | public function explicitSelectMode(bool $explicitSelect=true) : FluentPdoModel |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param bool $filterOnFetch |
||
| 328 | * |
||
| 329 | * @return FluentPdoModel|$this |
||
| 330 | */ |
||
| 331 | public function filterOnFetch(bool $filterOnFetch=true) : FluentPdoModel |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param bool $logFilterChanges |
||
| 340 | * |
||
| 341 | * @return FluentPdoModel|$this |
||
| 342 | */ |
||
| 343 | public function logFilterChanges(bool $logFilterChanges=true) : FluentPdoModel |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return the name of the table |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getTableName() : string |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getDisplayColumn() : string |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set the display column |
||
| 370 | * |
||
| 371 | * @param string $column |
||
| 372 | * |
||
| 373 | * @return FluentPdoModel|$this |
||
| 374 | */ |
||
| 375 | public function displayColumn(string $column) : FluentPdoModel |
||
| 381 | /** |
||
| 382 | * Set the table alias |
||
| 383 | * |
||
| 384 | * @param string $alias |
||
| 385 | * |
||
| 386 | * @return FluentPdoModel|$this |
||
| 387 | */ |
||
| 388 | public function tableAlias(string $alias) : FluentPdoModel |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param int $cacheTtl |
||
| 397 | * @return FluentPdoModel|$this |
||
| 398 | * @throws Exception |
||
| 399 | */ |
||
| 400 | protected function _cacheTtl(int $cacheTtl) : FluentPdoModel |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | public function getTableAlias() : string |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $associations |
||
| 422 | * |
||
| 423 | * @return FluentPdoModel|$this |
||
| 424 | */ |
||
| 425 | public function associations(array $associations) : FluentPdoModel |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $alias |
||
| 434 | * @param array $definition |
||
| 435 | * @return FluentPdoModel|$this |
||
| 436 | */ |
||
| 437 | public function setBelongsTo(string $alias, array $definition) : FluentPdoModel |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param $alias |
||
| 449 | * @param $displayField |
||
| 450 | * @return FluentPdoModel|$this |
||
| 451 | * @throws \Terah\Assert\AssertionFailedException |
||
| 452 | */ |
||
| 453 | public function setBelongsToDisplayField(string $alias, string $displayField) : FluentPdoModel |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param PDOStatement $stmt |
||
| 466 | * |
||
| 467 | * @param PDOStatement $stmt |
||
| 468 | * @param Closure $fnCallback |
||
| 469 | * @return bool|stdClass |
||
| 470 | */ |
||
| 471 | public function fetchRow(PDOStatement $stmt, Closure $fnCallback=null) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param PDOStatement $stmt |
||
| 497 | * @param $record |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | protected function getColumnMeta(PDOStatement $stmt, $record) : array |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param array $schema |
||
| 518 | * |
||
| 519 | * @return FluentPdoModel|$this |
||
| 520 | */ |
||
| 521 | public function schema(array $schema) : FluentPdoModel |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string|array $field |
||
| 530 | * @param $type |
||
| 531 | * @return FluentPdoModel|$this |
||
| 532 | */ |
||
| 533 | public function addSchema($field, string $type) : FluentPdoModel |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param $keysOnly |
||
| 552 | * @return array |
||
| 553 | */ |
||
| 554 | public function getColumns(bool $keysOnly=true) : array |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Get the primary key name |
||
| 561 | * |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | public function getPrimaryKeyName() : string |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $query |
||
| 571 | * @param array $parameters |
||
| 572 | * |
||
| 573 | * @return bool |
||
| 574 | * @throws Exception |
||
| 575 | */ |
||
| 576 | public function execute(string $query, array $parameters=[]) : bool |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param string $query |
||
| 604 | * @param array $params |
||
| 605 | * @return FluentPdoModel|$this |
||
| 606 | */ |
||
| 607 | public function query(string $query, array $params=[]) : FluentPdoModel |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @param string $sql |
||
| 617 | * @param array $params |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | public function buildQuery(string $sql, array $params=[]) : string |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param stdClass $record |
||
| 652 | * |
||
| 653 | * @return stdClass |
||
| 654 | */ |
||
| 655 | protected function _trimAndLowerCaseKeys(stdClass $record) : stdClass |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Return the number of affected row by the last statement |
||
| 668 | * |
||
| 669 | * @return int |
||
| 670 | */ |
||
| 671 | public function rowCount() : int |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return PDOStatement |
||
| 680 | * @throws PDOException |
||
| 681 | */ |
||
| 682 | public function fetchStmt() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @return array |
||
| 694 | */ |
||
| 695 | public function fetchSqlQuery() : array |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $tableName |
||
| 708 | * @param bool $dropIfExists |
||
| 709 | * @param array $indexes |
||
| 710 | * @return boolean |
||
| 711 | * @throws Exception |
||
| 712 | */ |
||
| 713 | public function fetchIntoMemoryTable(string $tableName, bool $dropIfExists=true, array $indexes=[]) : bool |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @param string $keyedOn |
||
| 738 | * @param int $cacheTtl |
||
| 739 | * @return stdClass[] |
||
| 740 | */ |
||
| 741 | public function fetch(string $keyedOn='', int $cacheTtl=self::CACHE_NO) : array |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | protected function _parseWhereForPrimaryLookup() : string |
||
| 799 | |||
| 800 | /** |
||
| 801 | * @param string $cacheKey |
||
| 802 | * @param Closure $func |
||
| 803 | * @param int $cacheTtl - 0 for default ttl, -1 for no cache or int for custom ttl |
||
| 804 | * @return mixed |
||
| 805 | */ |
||
| 806 | protected function _cacheData(string $cacheKey, Closure $func, int $cacheTtl=self::CACHE_DEFAULT) |
||
| 844 | |||
| 845 | /** |
||
| 846 | * @param string $cacheKey |
||
| 847 | * @return bool |
||
| 848 | */ |
||
| 849 | public function clearCache(string $cacheKey) : bool |
||
| 853 | |||
| 854 | /** |
||
| 855 | * @param string $table |
||
| 856 | * @return bool |
||
| 857 | */ |
||
| 858 | public function clearCacheByTable(string $table='') : bool |
||
| 868 | |||
| 869 | /** |
||
| 870 | * @param Closure $fnCallback |
||
| 871 | * @return int |
||
| 872 | */ |
||
| 873 | public function fetchCallback(Closure $fnCallback) : int |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param Closure $fnCallback |
||
| 885 | * @param string $keyedOn |
||
| 886 | * @return array |
||
| 887 | */ |
||
| 888 | public function fetchObjectsByCallback(Closure $fnCallback, string $keyedOn='') : array |
||
| 905 | |||
| 906 | /** |
||
| 907 | * @param $numFailures |
||
| 908 | * @return FluentPdoModel|$this |
||
| 909 | */ |
||
| 910 | public function maxCallbackFailures(int $numFailures) : FluentPdoModel |
||
| 917 | |||
| 918 | /** |
||
| 919 | * @param PDOStatement $stmt |
||
| 920 | * @param Closure $fnCallback |
||
| 921 | * @param int $successCnt |
||
| 922 | * @return bool|null|stdClass |
||
| 923 | */ |
||
| 924 | protected function _tallySuccessCount(PDOStatement $stmt, Closure $fnCallback, int &$successCnt) |
||
| 955 | |||
| 956 | /** |
||
| 957 | * @return bool |
||
| 958 | */ |
||
| 959 | public function canGenericUpdate() : bool |
||
| 963 | |||
| 964 | /** |
||
| 965 | * @return bool |
||
| 966 | */ |
||
| 967 | public function canGenericAdd() : bool |
||
| 971 | |||
| 972 | /** |
||
| 973 | * @return bool |
||
| 974 | */ |
||
| 975 | public function canGenericDelete() : bool |
||
| 979 | |||
| 980 | /** |
||
| 981 | * @param string $keyedOn |
||
| 982 | * @param string $valueField |
||
| 983 | * @param int $cacheTtl |
||
| 984 | * @return mixed |
||
| 985 | */ |
||
| 986 | public function fetchList(string $keyedOn='', string $valueField='', int $cacheTtl=self::CACHE_NO) : array |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * @param string $column |
||
| 1041 | * @param int $cacheTtl |
||
| 1042 | * @param bool|true $unique |
||
| 1043 | * @return array |
||
| 1044 | */ |
||
| 1045 | public function fetchColumn(string $column, int $cacheTtl=self::CACHE_NO, bool $unique=true) : array |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * @param string $field |
||
| 1057 | * @param int $itemId |
||
| 1058 | * @param int $cacheTtl |
||
| 1059 | * @return mixed|null |
||
| 1060 | */ |
||
| 1061 | public function fetchField(string $field='', int $itemId=0, int $cacheTtl=self::CACHE_NO) |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * @param string $field |
||
| 1087 | * @param int $itemId |
||
| 1088 | * @param int $cacheTtl |
||
| 1089 | * @return string |
||
| 1090 | */ |
||
| 1091 | public function fetchStr(string $field='', $itemId=0, int $cacheTtl=self::CACHE_NO) : string |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * @param int $cacheTtl |
||
| 1098 | * @return int |
||
| 1099 | */ |
||
| 1100 | public function fetchId(int $cacheTtl=self::CACHE_NO) : int |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * @param string $field |
||
| 1107 | * @param int $itemId |
||
| 1108 | * @param int $cacheTtl |
||
| 1109 | * @return int |
||
| 1110 | */ |
||
| 1111 | public function fetchInt(string $field='', int $itemId=0, int $cacheTtl=self::CACHE_NO) : int |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @param string $field |
||
| 1118 | * @param int $itemId |
||
| 1119 | * @param int $cacheTtl |
||
| 1120 | * @return float |
||
| 1121 | */ |
||
| 1122 | public function fetchFloat(string $field='', int $itemId=0, int $cacheTtl=self::CACHE_NO) : float |
||
| 1126 | |||
| 1127 | /** |
||
| 1128 | * @param string $field |
||
| 1129 | * @param int $itemId |
||
| 1130 | * @param int $cacheTtl |
||
| 1131 | * @return bool |
||
| 1132 | */ |
||
| 1133 | public function fetchBool(string $field='', int $itemId=0, int $cacheTtl=self::CACHE_NO) : bool |
||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * @param int|null $id |
||
| 1140 | * @param int $cacheTtl |
||
| 1141 | * @return stdClass|bool |
||
| 1142 | */ |
||
| 1143 | public function fetchOne(int $id=0, int $cacheTtl=self::CACHE_NO) |
||
| 1154 | |||
| 1155 | /** |
||
| 1156 | * @param int|null $id |
||
| 1157 | * @param int $cacheTtl |
||
| 1158 | * @return boolean |
||
| 1159 | */ |
||
| 1160 | public function fetchExists(int $id=0, int $cacheTtl=self::CACHE_NO) : bool |
||
| 1169 | |||
| 1170 | /*------------------------------------------------------------------------------ |
||
| 1171 | Fluent Query Builder |
||
| 1172 | *-----------------------------------------------------------------------------*/ |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Create the select clause |
||
| 1176 | * |
||
| 1177 | * @param mixed $columns - the column to select. Can be string or array of fields |
||
| 1178 | * @param string $alias - an alias to the column |
||
| 1179 | * @param boolean $explicitSelect |
||
| 1180 | * @return FluentPdoModel|$this |
||
| 1181 | */ |
||
| 1182 | public function select($columns='*', string $alias='', bool $explicitSelect=true) : FluentPdoModel |
||
| 1224 | |||
| 1225 | /** |
||
| 1226 | * @param string $select |
||
| 1227 | * @return FluentPdoModel|$this |
||
| 1228 | */ |
||
| 1229 | public function selectRaw(string $select) : FluentPdoModel |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * @param bool $logQueries |
||
| 1238 | * |
||
| 1239 | * @return FluentPdoModel|$this |
||
| 1240 | */ |
||
| 1241 | public function logQueries(bool $logQueries=true) : FluentPdoModel |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * @param bool $includeCnt |
||
| 1250 | * |
||
| 1251 | * @return FluentPdoModel|$this |
||
| 1252 | */ |
||
| 1253 | public function includeCount(bool $includeCnt=true) : FluentPdoModel |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * @param bool $distinct |
||
| 1262 | * |
||
| 1263 | * @return FluentPdoModel|$this |
||
| 1264 | */ |
||
| 1265 | public function distinct(bool $distinct=true) : FluentPdoModel |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * @param array $fields |
||
| 1274 | * @return FluentPdoModel|$this |
||
| 1275 | */ |
||
| 1276 | public function withBelongsTo(array $fields=[]) : FluentPdoModel |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * @param string $alias |
||
| 1292 | * @param string $type |
||
| 1293 | * @param bool $addSelectField |
||
| 1294 | * @return FluentPdoModel|$this |
||
| 1295 | */ |
||
| 1296 | public function autoJoin(string $alias, string $type=self::LEFT_JOIN, bool $addSelectField=true) : FluentPdoModel |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Add where condition, more calls appends with AND |
||
| 1316 | * |
||
| 1317 | * @param string $condition possibly containing ? or :name |
||
| 1318 | * @param mixed $parameters accepted by PDOStatement::execute or a scalar value |
||
| 1319 | * @param mixed ... |
||
| 1320 | * @return FluentPdoModel|$this |
||
| 1321 | */ |
||
| 1322 | public function where($condition, $parameters=[]) : FluentPdoModel |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Create an AND operator in the where clause |
||
| 1374 | * |
||
| 1375 | * @return FluentPdoModel|$this |
||
| 1376 | */ |
||
| 1377 | public function _and() : FluentPdoModel |
||
| 1391 | |||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Create an OR operator in the where clause |
||
| 1395 | * |
||
| 1396 | * @return FluentPdoModel|$this |
||
| 1397 | */ |
||
| 1398 | public function _or() : FluentPdoModel |
||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * To group multiple where clauses together. |
||
| 1415 | * |
||
| 1416 | * @return FluentPdoModel|$this |
||
| 1417 | */ |
||
| 1418 | public function wrap() : FluentPdoModel |
||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Where Primary key |
||
| 1431 | * |
||
| 1432 | * @param int $id |
||
| 1433 | * @param bool $addAlias |
||
| 1434 | * |
||
| 1435 | * @return FluentPdoModel|$this |
||
| 1436 | */ |
||
| 1437 | public function wherePk(int $id, bool $addAlias=true) : FluentPdoModel |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * WHERE $columnName != $value |
||
| 1446 | * |
||
| 1447 | * @param string $columnName |
||
| 1448 | * @param mixed $value |
||
| 1449 | * @return FluentPdoModel|$this |
||
| 1450 | */ |
||
| 1451 | public function whereNot(string $columnName, $value) : FluentPdoModel |
||
| 1455 | /** |
||
| 1456 | * WHERE $columnName != $value |
||
| 1457 | * |
||
| 1458 | * @param string $columnName |
||
| 1459 | * @param mixed $value |
||
| 1460 | * @return FluentPdoModel|$this |
||
| 1461 | */ |
||
| 1462 | public function whereCoercedNot(string $columnName, $value) : FluentPdoModel |
||
| 1466 | |||
| 1467 | /** |
||
| 1468 | * WHERE $columnName LIKE $value |
||
| 1469 | * |
||
| 1470 | * @param string $columnName |
||
| 1471 | * @param mixed $value |
||
| 1472 | * @return FluentPdoModel|$this |
||
| 1473 | */ |
||
| 1474 | public function whereLike(string $columnName, $value) : FluentPdoModel |
||
| 1478 | |||
| 1479 | /** |
||
| 1480 | * @param string $columnName |
||
| 1481 | * @param mixed $value1 |
||
| 1482 | * @param mixed $value2 |
||
| 1483 | * @return FluentPdoModel|$this |
||
| 1484 | */ |
||
| 1485 | public function whereBetween(string $columnName, $value1, $value2) : FluentPdoModel |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * @param string $columnName |
||
| 1495 | * @param mixed $value1 |
||
| 1496 | * @param mixed $value2 |
||
| 1497 | * @return FluentPdoModel|$this |
||
| 1498 | */ |
||
| 1499 | public function whereNotBetween(string $columnName, $value1, $value2) : FluentPdoModel |
||
| 1506 | |||
| 1507 | /** |
||
| 1508 | * @param string $columnName |
||
| 1509 | * @param string $regex |
||
| 1510 | * @return FluentPdoModel|$this |
||
| 1511 | */ |
||
| 1512 | public function whereRegex(string $columnName, string $regex) : FluentPdoModel |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * @param string $columnName |
||
| 1519 | * @param string $regex |
||
| 1520 | * @return FluentPdoModel|$this |
||
| 1521 | */ |
||
| 1522 | public function whereNotRegex(string $columnName, string $regex) : FluentPdoModel |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * WHERE $columnName NOT LIKE $value |
||
| 1529 | * |
||
| 1530 | * @param string $columnName |
||
| 1531 | * @param string $value |
||
| 1532 | * @return FluentPdoModel|$this |
||
| 1533 | */ |
||
| 1534 | public function whereNotLike(string $columnName, string $value) : FluentPdoModel |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * WHERE $columnName > $value |
||
| 1541 | * |
||
| 1542 | * @param string $columnName |
||
| 1543 | * @param mixed $value |
||
| 1544 | * @return FluentPdoModel|$this |
||
| 1545 | */ |
||
| 1546 | public function whereGt(string $columnName, $value) : FluentPdoModel |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * WHERE $columnName >= $value |
||
| 1553 | * |
||
| 1554 | * @param string $columnName |
||
| 1555 | * @param mixed $value |
||
| 1556 | * @return FluentPdoModel|$this |
||
| 1557 | */ |
||
| 1558 | public function whereGte(string $columnName, $value) : FluentPdoModel |
||
| 1562 | |||
| 1563 | /** |
||
| 1564 | * WHERE $columnName < $value |
||
| 1565 | * |
||
| 1566 | * @param string $columnName |
||
| 1567 | * @param mixed $value |
||
| 1568 | * @return FluentPdoModel|$this |
||
| 1569 | */ |
||
| 1570 | public function whereLt(string $columnName, $value) : FluentPdoModel |
||
| 1574 | |||
| 1575 | /** |
||
| 1576 | * WHERE $columnName <= $value |
||
| 1577 | * |
||
| 1578 | * @param string $columnName |
||
| 1579 | * @param mixed $value |
||
| 1580 | * @return FluentPdoModel|$this |
||
| 1581 | */ |
||
| 1582 | public function whereLte(string $columnName, $value) : FluentPdoModel |
||
| 1586 | |||
| 1587 | /** |
||
| 1588 | * WHERE $columnName IN (?,?,?,...) |
||
| 1589 | * |
||
| 1590 | * @param string $columnName |
||
| 1591 | * @param array $values |
||
| 1592 | * @return FluentPdoModel|$this |
||
| 1593 | */ |
||
| 1594 | public function whereIn(string $columnName, array $values) : FluentPdoModel |
||
| 1598 | |||
| 1599 | /** |
||
| 1600 | * WHERE $columnName NOT IN (?,?,?,...) |
||
| 1601 | * |
||
| 1602 | * @param string $columnName |
||
| 1603 | * @param array $values |
||
| 1604 | * @return FluentPdoModel|$this |
||
| 1605 | */ |
||
| 1606 | public function whereNotIn(string $columnName, array $values) : FluentPdoModel |
||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * WHERE $columnName IS NULL |
||
| 1615 | * |
||
| 1616 | * @param string $columnName |
||
| 1617 | * @return FluentPdoModel|$this |
||
| 1618 | */ |
||
| 1619 | public function whereNull(string $columnName) : FluentPdoModel |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * WHERE $columnName IS NOT NULL |
||
| 1626 | * |
||
| 1627 | * @param string $columnName |
||
| 1628 | * @return FluentPdoModel|$this |
||
| 1629 | */ |
||
| 1630 | public function whereNotNull(string $columnName) : FluentPdoModel |
||
| 1634 | |||
| 1635 | /** |
||
| 1636 | * @param string $statement |
||
| 1637 | * @param string $operator |
||
| 1638 | * @return FluentPdoModel|$this |
||
| 1639 | */ |
||
| 1640 | public function having(string $statement, string $operator=self::OPERATOR_AND) : FluentPdoModel |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * ORDER BY $columnName (ASC | DESC) |
||
| 1652 | * |
||
| 1653 | * @param string $columnName - The name of the column or an expression |
||
| 1654 | * @param string $ordering (DESC | ASC) |
||
| 1655 | * @return FluentPdoModel|$this |
||
| 1656 | */ |
||
| 1657 | public function orderBy(string $columnName='', string $ordering='DESC') : FluentPdoModel |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * GROUP BY $columnName |
||
| 1674 | * |
||
| 1675 | * @param string $columnName |
||
| 1676 | * @return FluentPdoModel|$this |
||
| 1677 | */ |
||
| 1678 | public function groupBy(string $columnName) : FluentPdoModel |
||
| 1688 | |||
| 1689 | |||
| 1690 | /** |
||
| 1691 | * LIMIT $limit |
||
| 1692 | * |
||
| 1693 | * @param int $limit |
||
| 1694 | * @param int|null $offset |
||
| 1695 | * @return FluentPdoModel|$this |
||
| 1696 | */ |
||
| 1697 | public function limit(int $limit, int $offset=0) : FluentPdoModel |
||
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Return the limit |
||
| 1709 | * |
||
| 1710 | * @return integer |
||
| 1711 | */ |
||
| 1712 | public function getLimit() : int |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * OFFSET $offset |
||
| 1719 | * |
||
| 1720 | * @param int $offset |
||
| 1721 | * @return FluentPdoModel|$this |
||
| 1722 | */ |
||
| 1723 | public function offset(int $offset) : FluentPdoModel |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Return the offset |
||
| 1732 | * |
||
| 1733 | * @return integer |
||
| 1734 | */ |
||
| 1735 | public function getOffset() : int |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Build a join |
||
| 1742 | * |
||
| 1743 | * @param string $table - The table name |
||
| 1744 | * @param string $constraint -> id = profile.user_id |
||
| 1745 | * @param string $tableAlias - The alias of the table name |
||
| 1746 | * @param string $joinOperator - LEFT | INNER | etc... |
||
| 1747 | * @return FluentPdoModel|$this |
||
| 1748 | */ |
||
| 1749 | public function join(string $table, string $constraint='', string $tableAlias='', string $joinOperator='') : FluentPdoModel |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Create a left join |
||
| 1771 | * |
||
| 1772 | * @param string $table |
||
| 1773 | * @param string $constraint |
||
| 1774 | * @param string $tableAlias |
||
| 1775 | * @return FluentPdoModel|$this |
||
| 1776 | */ |
||
| 1777 | public function leftJoin(string $table, string $constraint, string $tableAlias='') : FluentPdoModel |
||
| 1781 | |||
| 1782 | |||
| 1783 | /** |
||
| 1784 | * Return the build select query |
||
| 1785 | * |
||
| 1786 | * @return string |
||
| 1787 | */ |
||
| 1788 | public function getSelectQuery() : string |
||
| 1831 | |||
| 1832 | /** |
||
| 1833 | * Prepare columns to include the table alias name |
||
| 1834 | * @param array $columns |
||
| 1835 | * @return array |
||
| 1836 | */ |
||
| 1837 | protected function _prepareColumns(array $columns) : array |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * Build the WHERE clause(s) |
||
| 1870 | * |
||
| 1871 | * @param bool $purgeAliases |
||
| 1872 | * @return string |
||
| 1873 | */ |
||
| 1874 | protected function _getWhereString(bool $purgeAliases=false) : string |
||
| 1907 | |||
| 1908 | /** |
||
| 1909 | * Return the HAVING clause |
||
| 1910 | * |
||
| 1911 | * @return string |
||
| 1912 | */ |
||
| 1913 | protected function _getHavingString() : string |
||
| 1932 | |||
| 1933 | /** |
||
| 1934 | * Return the values to be bound for where |
||
| 1935 | * |
||
| 1936 | * @param bool $purgeAliases |
||
| 1937 | * @return array |
||
| 1938 | */ |
||
| 1939 | protected function _getWhereParameters(bool $purgeAliases=false) : array |
||
| 1945 | |||
| 1946 | /** |
||
| 1947 | * @param array $record |
||
| 1948 | * @return stdClass |
||
| 1949 | */ |
||
| 1950 | public function insertArr(array $record) : stdClass |
||
| 1954 | |||
| 1955 | /** |
||
| 1956 | * Insert new rows |
||
| 1957 | * $records can be a stdClass or an array of stdClass to add a bulk insert |
||
| 1958 | * If a single row is inserted, it will return it's row instance |
||
| 1959 | * |
||
| 1960 | * @param stdClass $record |
||
| 1961 | * @return stdClass |
||
| 1962 | * @throws Exception |
||
| 1963 | */ |
||
| 1964 | public function insert(stdClass $record) : stdClass |
||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * @param string $name |
||
| 1990 | * @return int |
||
| 1991 | */ |
||
| 1992 | public function getLastInsertId(string $name='') : int |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * @param stdClass[] $records |
||
| 1999 | * @return stdClass[] |
||
| 2000 | */ |
||
| 2001 | public function insertSqlQuery(array $records) : array |
||
| 2021 | |||
| 2022 | /** |
||
| 2023 | * @param $data |
||
| 2024 | * @param array $matchOn |
||
| 2025 | * @param bool $returnObj |
||
| 2026 | * @return bool|int|stdClass |
||
| 2027 | */ |
||
| 2028 | public function upsert($data, array $matchOn=[], $returnObj=false) |
||
| 2050 | |||
| 2051 | /** |
||
| 2052 | * @param stdClass $object |
||
| 2053 | * @param array $matchOn |
||
| 2054 | * @param bool $returnObj |
||
| 2055 | * @return bool|int|stdClass |
||
| 2056 | */ |
||
| 2057 | public function upsertOne(stdClass $object, array $matchOn=[], $returnObj=false) |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * @param array $data |
||
| 2100 | * @param array $matchOn |
||
| 2101 | * @param bool|false $returnObj |
||
| 2102 | * @return bool|int|stdClass |
||
| 2103 | */ |
||
| 2104 | public function upsertArr(array $data, array $matchOn=[], bool $returnObj=false) |
||
| 2108 | |||
| 2109 | /** |
||
| 2110 | * Update entries |
||
| 2111 | * Use the query builder to create the where clause |
||
| 2112 | * |
||
| 2113 | * @param stdClass $record |
||
| 2114 | * @param bool $updateAll |
||
| 2115 | * @return int |
||
| 2116 | * @throws Exception |
||
| 2117 | */ |
||
| 2118 | public function update(stdClass $record, $updateAll=false) : int |
||
| 2141 | |||
| 2142 | /** |
||
| 2143 | * @param array $record |
||
| 2144 | * @param bool|false $updateAll |
||
| 2145 | * @return int |
||
| 2146 | * @throws Exception |
||
| 2147 | */ |
||
| 2148 | public function updateArr(array $record, $updateAll=false) : int |
||
| 2152 | |||
| 2153 | |||
| 2154 | /** |
||
| 2155 | * @param string $field |
||
| 2156 | * @param mixed $value |
||
| 2157 | * @param int $id |
||
| 2158 | * @param bool|false $updateAll |
||
| 2159 | * @return int |
||
| 2160 | * @throws Exception |
||
| 2161 | */ |
||
| 2162 | public function updateField(string $field, $value, int $id=0, bool $updateAll=false) : int |
||
| 2171 | |||
| 2172 | /** |
||
| 2173 | * @param stdClass $record |
||
| 2174 | * @return bool|int |
||
| 2175 | * @throws Exception |
||
| 2176 | */ |
||
| 2177 | public function updateChanged(stdClass $record) : int |
||
| 2191 | |||
| 2192 | /** |
||
| 2193 | * @param string $expression |
||
| 2194 | * @param array $params |
||
| 2195 | * @return FluentPdoModel|$this |
||
| 2196 | */ |
||
| 2197 | public function updateByExpression(string $expression, array $params) : FluentPdoModel |
||
| 2203 | |||
| 2204 | /** |
||
| 2205 | * @param array $data |
||
| 2206 | * @return int |
||
| 2207 | * @throws Exception |
||
| 2208 | */ |
||
| 2209 | public function rawUpdate(array $data=[]) : int |
||
| 2218 | |||
| 2219 | /** |
||
| 2220 | * @param stdClass $record |
||
| 2221 | * @return array |
||
| 2222 | */ |
||
| 2223 | public function updateSqlQuery(stdClass $record) : array |
||
| 2233 | |||
| 2234 | /** |
||
| 2235 | * @param $record |
||
| 2236 | * @return array |
||
| 2237 | */ |
||
| 2238 | protected function updateSql(array $record) : array |
||
| 2269 | |||
| 2270 | /** |
||
| 2271 | * @param bool $deleteAll |
||
| 2272 | * @param bool $force |
||
| 2273 | * @return int |
||
| 2274 | * @throws Exception |
||
| 2275 | */ |
||
| 2276 | public function delete(bool $deleteAll=false, bool $force=false) : int |
||
| 2292 | |||
| 2293 | /** |
||
| 2294 | * @return bool |
||
| 2295 | */ |
||
| 2296 | public function isSoftDelete() : bool |
||
| 2300 | |||
| 2301 | /** |
||
| 2302 | * @param bool|false $force |
||
| 2303 | * @return FluentPdoModel|$this |
||
| 2304 | * @throws Exception |
||
| 2305 | */ |
||
| 2306 | public function truncate(bool $force=false) : FluentPdoModel |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * @return array |
||
| 2323 | */ |
||
| 2324 | public function deleteSqlQuery() : array |
||
| 2336 | |||
| 2337 | |||
| 2338 | /** |
||
| 2339 | * Return the aggregate count of column |
||
| 2340 | * |
||
| 2341 | * @param string $column |
||
| 2342 | * @param int $cacheTtl |
||
| 2343 | * @return float |
||
| 2344 | */ |
||
| 2345 | public function count(string $column='*', int $cacheTtl=self::CACHE_NO) : float |
||
| 2351 | |||
| 2352 | |||
| 2353 | /** |
||
| 2354 | * Return the aggregate max count of column |
||
| 2355 | * |
||
| 2356 | * @param string $column |
||
| 2357 | * @param int $cacheTtl |
||
| 2358 | * @return int|float|string|null |
||
| 2359 | */ |
||
| 2360 | public function max(string $column, int $cacheTtl=self::CACHE_NO) |
||
| 2366 | |||
| 2367 | |||
| 2368 | /** |
||
| 2369 | * Return the aggregate min count of column |
||
| 2370 | * |
||
| 2371 | * @param string $column |
||
| 2372 | * @param int $cacheTtl |
||
| 2373 | * @return int|float|string|null |
||
| 2374 | */ |
||
| 2375 | public function min(string $column, int $cacheTtl=self::CACHE_NO) |
||
| 2381 | |||
| 2382 | /** |
||
| 2383 | * Return the aggregate sum count of column |
||
| 2384 | * |
||
| 2385 | * @param string $column |
||
| 2386 | * @param int $cacheTtl |
||
| 2387 | * @return int|float|string|null |
||
| 2388 | */ |
||
| 2389 | public function sum(string $column, int $cacheTtl=self::CACHE_NO) |
||
| 2395 | |||
| 2396 | /** |
||
| 2397 | * Return the aggregate average count of column |
||
| 2398 | * |
||
| 2399 | * @param string $column |
||
| 2400 | * @param int $cacheTtl |
||
| 2401 | * @return int|float|string|null |
||
| 2402 | */ |
||
| 2403 | public function avg(string $column, int $cacheTtl=self::CACHE_NO) |
||
| 2409 | |||
| 2410 | /*******************************************************************************/ |
||
| 2411 | // Utilities methods |
||
| 2412 | |||
| 2413 | /** |
||
| 2414 | * Reset fields |
||
| 2415 | * |
||
| 2416 | * @return FluentPdoModel|$this |
||
| 2417 | */ |
||
| 2418 | public function reset() : FluentPdoModel |
||
| 2446 | |||
| 2447 | |||
| 2448 | /** |
||
| 2449 | * @return FluentPdoModel|$this |
||
| 2450 | */ |
||
| 2451 | public function removeUnauthorisedFields() : FluentPdoModel |
||
| 2455 | |||
| 2456 | /** |
||
| 2457 | * @return Closure[] |
||
| 2458 | */ |
||
| 2459 | protected function _getFieldHandlers() : array |
||
| 2526 | |||
| 2527 | /** |
||
| 2528 | * @return bool |
||
| 2529 | */ |
||
| 2530 | public function begin() : bool |
||
| 2544 | |||
| 2545 | /** |
||
| 2546 | * @return bool |
||
| 2547 | */ |
||
| 2548 | public function commit() : bool |
||
| 2566 | |||
| 2567 | /** |
||
| 2568 | * @return bool |
||
| 2569 | */ |
||
| 2570 | public function rollback() : bool |
||
| 2584 | |||
| 2585 | /** |
||
| 2586 | * @param stdClass $record |
||
| 2587 | * @param string $type |
||
| 2588 | * @return stdClass |
||
| 2589 | */ |
||
| 2590 | public function applyGlobalModifiers(stdClass $record, string $type) : stdClass |
||
| 2603 | |||
| 2604 | /** |
||
| 2605 | * @param stdClass $record |
||
| 2606 | * @param string $type |
||
| 2607 | * @return stdClass |
||
| 2608 | */ |
||
| 2609 | public function removeUnneededFields(stdClass $record, string $type) : stdClass |
||
| 2635 | |||
| 2636 | |||
| 2637 | /** |
||
| 2638 | * @param array $ids |
||
| 2639 | * @param array $values |
||
| 2640 | * @param int $batch |
||
| 2641 | * @return bool |
||
| 2642 | */ |
||
| 2643 | public function setById(array $ids, array $values, int $batch=1000) : bool |
||
| 2659 | |||
| 2660 | |||
| 2661 | /** |
||
| 2662 | * @param string $displayColumnValue |
||
| 2663 | * @return int |
||
| 2664 | */ |
||
| 2665 | public function resolveId(string $displayColumnValue) : int |
||
| 2676 | |||
| 2677 | /** |
||
| 2678 | * @param int $resourceId |
||
| 2679 | * @param array $query |
||
| 2680 | * @param array $extraFields |
||
| 2681 | * @param int $cacheTtl |
||
| 2682 | * @return array |
||
| 2683 | */ |
||
| 2684 | public function fetchApiResource(int $resourceId, array $query=[], array $extraFields=[], int $cacheTtl=self::CACHE_NO) : array |
||
| 2697 | |||
| 2698 | /** |
||
| 2699 | * @param array $query |
||
| 2700 | * @param array $extraFields |
||
| 2701 | * @param int $cacheTtl |
||
| 2702 | * @param string $permEntity |
||
| 2703 | * @return array |
||
| 2704 | */ |
||
| 2705 | public function fetchApiResources(array $query=[], array $extraFields=[], int $cacheTtl=self::CACHE_NO, string $permEntity='') : array |
||
| 2727 | |||
| 2728 | |||
| 2729 | /** |
||
| 2730 | * @return array |
||
| 2731 | */ |
||
| 2732 | public function getSearchableAssociations() : array |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * @param array $fields |
||
| 2742 | */ |
||
| 2743 | public function removeUnrequestedFields(array $fields) |
||
| 2754 | |||
| 2755 | /** |
||
| 2756 | * @param array $removeFields |
||
| 2757 | */ |
||
| 2758 | public function removeFields(array $removeFields=[]) |
||
| 2783 | |||
| 2784 | /** |
||
| 2785 | * @return FluentPdoModel|$this |
||
| 2786 | */ |
||
| 2787 | public function defaultFilters() : FluentPdoModel |
||
| 2791 | |||
| 2792 | /** |
||
| 2793 | * @param bool $allow |
||
| 2794 | * |
||
| 2795 | * @return FluentPdoModel|$this |
||
| 2796 | */ |
||
| 2797 | public function allowMetaColumnOverride(bool $allow=false) : FluentPdoModel |
||
| 2803 | |||
| 2804 | /** |
||
| 2805 | * @param stdClass $record |
||
| 2806 | * @return stdClass |
||
| 2807 | */ |
||
| 2808 | public function onFetch(stdClass $record) : stdClass |
||
| 2820 | |||
| 2821 | /** |
||
| 2822 | * @param $value |
||
| 2823 | * @return string |
||
| 2824 | */ |
||
| 2825 | public function gzEncodeData(string $value) : string |
||
| 2834 | |||
| 2835 | /** |
||
| 2836 | * @param $value |
||
| 2837 | * @return mixed|string |
||
| 2838 | */ |
||
| 2839 | public function gzDecodeData(string $value) : string |
||
| 2849 | |||
| 2850 | /** |
||
| 2851 | * @param $value |
||
| 2852 | * @return bool |
||
| 2853 | */ |
||
| 2854 | protected function _hasGzipPrefix(string $value) : bool |
||
| 2858 | |||
| 2859 | /** |
||
| 2860 | * @param stdClass $record |
||
| 2861 | * @return stdClass |
||
| 2862 | */ |
||
| 2863 | public function fixTimestamps(stdClass $record) : stdClass |
||
| 2875 | |||
| 2876 | /** |
||
| 2877 | * @param int $max |
||
| 2878 | * @return FluentPdoModel|$this |
||
| 2879 | */ |
||
| 2880 | public function setMaxRecords(int $max) : FluentPdoModel |
||
| 2887 | |||
| 2888 | |||
| 2889 | /** |
||
| 2890 | * @param stdClass $record |
||
| 2891 | * @param string $type |
||
| 2892 | * @return stdClass |
||
| 2893 | */ |
||
| 2894 | public function afterSave(stdClass $record, string $type) : stdClass |
||
| 2915 | |||
| 2916 | |||
| 2917 | /** |
||
| 2918 | * @param stdClass $record |
||
| 2919 | * @param string $type |
||
| 2920 | * @return stdClass |
||
| 2921 | */ |
||
| 2922 | public function addDefaultFields(stdClass $record, string $type) : stdClass |
||
| 2951 | |||
| 2952 | |||
| 2953 | /** |
||
| 2954 | * @return bool |
||
| 2955 | */ |
||
| 2956 | public function createTable() : bool |
||
| 2960 | |||
| 2961 | /** |
||
| 2962 | * @return bool |
||
| 2963 | */ |
||
| 2964 | public function dropTable() : bool |
||
| 2968 | |||
| 2969 | protected function _compileHandlers() |
||
| 2978 | |||
| 2979 | /** |
||
| 2980 | * @param string $viewName |
||
| 2981 | * @param int $cacheTtl |
||
| 2982 | * @return array |
||
| 2983 | */ |
||
| 2984 | public function getViewColumns($viewName, $cacheTtl=self::CACHE_NO) |
||
| 2988 | |||
| 2989 | /** |
||
| 2990 | * @param int $id |
||
| 2991 | * @return string |
||
| 2992 | */ |
||
| 2993 | public function getDisplayNameById(int $id) : string |
||
| 3003 | |||
| 3004 | /** |
||
| 3005 | * @param int $id |
||
| 3006 | * @param string $displayColumnValue |
||
| 3007 | * @return bool |
||
| 3008 | */ |
||
| 3009 | public function validIdDisplayNameCombo(int $id, $displayColumnValue) : bool |
||
| 3013 | |||
| 3014 | /** |
||
| 3015 | * @param array $toPopulate |
||
| 3016 | * @return stdClass |
||
| 3017 | */ |
||
| 3018 | protected function getEmptyObject(array $toPopulate=[]) : stdClass |
||
| 3024 | |||
| 3025 | /** |
||
| 3026 | * @param int $id |
||
| 3027 | * @return bool |
||
| 3028 | */ |
||
| 3029 | public static function isId(int $id) : bool |
||
| 3033 | |||
| 3034 | /** |
||
| 3035 | * @param int $cacheTtl |
||
| 3036 | * @return int |
||
| 3037 | */ |
||
| 3038 | public function activeCount(int $cacheTtl=self::CACHE_NO) : int |
||
| 3042 | |||
| 3043 | /** |
||
| 3044 | * @param string $tableAlias |
||
| 3045 | * @param string $columnName |
||
| 3046 | * @return FluentPdoModel|$this |
||
| 3047 | */ |
||
| 3048 | public function whereActive(string $tableAlias='', string $columnName='status') : FluentPdoModel |
||
| 3052 | |||
| 3053 | /** |
||
| 3054 | * @param string $tableAlias |
||
| 3055 | * @param string $columnName |
||
| 3056 | * @return FluentPdoModel|$this |
||
| 3057 | */ |
||
| 3058 | public function whereInactive(string $tableAlias='', string $columnName='status') : FluentPdoModel |
||
| 3062 | |||
| 3063 | /** |
||
| 3064 | * @param string $tableAlias |
||
| 3065 | * @param string $columnName |
||
| 3066 | * @return FluentPdoModel|$this |
||
| 3067 | */ |
||
| 3068 | public function whereArchived(string $tableAlias='', string $columnName='status') : FluentPdoModel |
||
| 3072 | |||
| 3073 | /** |
||
| 3074 | * @param int $status |
||
| 3075 | * @param string $tableAlias |
||
| 3076 | * @param string $columnName |
||
| 3077 | * @return FluentPdoModel|$this |
||
| 3078 | */ |
||
| 3079 | public function whereStatus(int $status, string $tableAlias='', string $columnName='status') : FluentPdoModel |
||
| 3088 | |||
| 3089 | /** |
||
| 3090 | * @param int $id |
||
| 3091 | * @return int |
||
| 3092 | */ |
||
| 3093 | public function updateActive(int $id=0) : int |
||
| 3103 | |||
| 3104 | /** |
||
| 3105 | * @param int $id |
||
| 3106 | * @return int |
||
| 3107 | */ |
||
| 3108 | public function updateInactive(int $id=0) : int |
||
| 3117 | |||
| 3118 | /** |
||
| 3119 | * @param string $field |
||
| 3120 | * @param int $id |
||
| 3121 | * @return int |
||
| 3122 | */ |
||
| 3123 | public function updateNow(string $field, int $id=0) : int |
||
| 3129 | /** |
||
| 3130 | * @param string $field |
||
| 3131 | * @param int $id |
||
| 3132 | * @return int |
||
| 3133 | */ |
||
| 3134 | public function updateToday($field, int $id=0) : int |
||
| 3140 | |||
| 3141 | /** |
||
| 3142 | * @param int $id |
||
| 3143 | * @return int |
||
| 3144 | */ |
||
| 3145 | public function updateArchived(int $id=0) : int |
||
| 3155 | |||
| 3156 | /** |
||
| 3157 | * @param int $status |
||
| 3158 | * @return int |
||
| 3159 | * @throws \Exception |
||
| 3160 | */ |
||
| 3161 | public function updateStatus(int $status) |
||
| 3167 | |||
| 3168 | /** |
||
| 3169 | * Return a YYYY-MM-DD HH:II:SS date format |
||
| 3170 | * |
||
| 3171 | * @param string $datetime - An english textual datetime description |
||
| 3172 | * now, yesterday, 3 days ago, +1 week |
||
| 3173 | * http://php.net/manual/en/function.strtotime.php |
||
| 3174 | * @return string YYYY-MM-DD HH:II:SS |
||
| 3175 | */ |
||
| 3176 | public static function NOW(string $datetime='now') : string |
||
| 3180 | |||
| 3181 | /** |
||
| 3182 | * Return a string containing the given number of question marks, |
||
| 3183 | * separated by commas. Eg '?, ?, ?' |
||
| 3184 | * |
||
| 3185 | * @param int - total of placeholder to insert |
||
| 3186 | * @return string |
||
| 3187 | */ |
||
| 3188 | protected function _makePlaceholders(int $numberOfPlaceholders=1) : string |
||
| 3192 | |||
| 3193 | /** |
||
| 3194 | * Format the table{Primary|Foreign}KeyName |
||
| 3195 | * |
||
| 3196 | * @param string $pattern |
||
| 3197 | * @param string $tableName |
||
| 3198 | * @return string |
||
| 3199 | */ |
||
| 3200 | protected function _formatKeyName(string $pattern, string $tableName) : string |
||
| 3204 | |||
| 3205 | |||
| 3206 | /** |
||
| 3207 | * @param array $query |
||
| 3208 | * @param array $extraFields |
||
| 3209 | * @return array |
||
| 3210 | * @throws \Exception |
||
| 3211 | */ |
||
| 3212 | protected function _prepareApiResource(array $query=[], array $extraFields=[]) : array |
||
| 3233 | |||
| 3234 | /** |
||
| 3235 | * @param string $query |
||
| 3236 | * @param array $parameters |
||
| 3237 | * |
||
| 3238 | * @return array |
||
| 3239 | */ |
||
| 3240 | protected function _logQuery(string $query, array $parameters) : array |
||
| 3253 | |||
| 3254 | /** |
||
| 3255 | * @param string $ident |
||
| 3256 | * @param string $builtQuery |
||
| 3257 | */ |
||
| 3258 | protected function _logSlowQueries(string $ident, string $builtQuery) |
||
| 3271 | |||
| 3272 | /** |
||
| 3273 | * @return float |
||
| 3274 | */ |
||
| 3275 | public function getTimeTaken() : float |
||
| 3281 | |||
| 3282 | /** |
||
| 3283 | * @param $secs |
||
| 3284 | * @return FluentPdoModel|$this |
||
| 3285 | */ |
||
| 3286 | public function slowQuerySeconds(int $secs) : FluentPdoModel |
||
| 3293 | |||
| 3294 | |||
| 3295 | /** |
||
| 3296 | * @param $field |
||
| 3297 | * @param array $values |
||
| 3298 | * @param string $placeholderPrefix |
||
| 3299 | * |
||
| 3300 | * @return array |
||
| 3301 | */ |
||
| 3302 | public function getNamedWhereIn(string $field, array $values, string $placeholderPrefix='') : array |
||
| 3326 | |||
| 3327 | /** |
||
| 3328 | * @param string $field |
||
| 3329 | * @param string $delimiter |
||
| 3330 | * |
||
| 3331 | * @return array |
||
| 3332 | */ |
||
| 3333 | protected function _getColumnAliasParts(string $field, string $delimiter=':') : array |
||
| 3343 | |||
| 3344 | /** |
||
| 3345 | * @param string $column |
||
| 3346 | * @param string $term |
||
| 3347 | * @return FluentPdoModel|$this |
||
| 3348 | */ |
||
| 3349 | protected function _addWhereClause(string $column, string $term) : FluentPdoModel |
||
| 3406 | |||
| 3407 | public function destroy() |
||
| 3416 | |||
| 3417 | public function __destruct() |
||
| 3421 | |||
| 3422 | /** |
||
| 3423 | * Load a model |
||
| 3424 | * |
||
| 3425 | * @param string $modelName |
||
| 3426 | * @param AbstractPdo $connection |
||
| 3427 | * @return FluentPdoModel|$this |
||
| 3428 | * @throws ModelNotFoundException |
||
| 3429 | */ |
||
| 3430 | public static function loadModel(string $modelName, AbstractPdo $connection=null) : FluentPdoModel |
||
| 3440 | |||
| 3441 | /** |
||
| 3442 | * Load a model |
||
| 3443 | * |
||
| 3444 | * @param string $tableName |
||
| 3445 | * @param AbstractPdo $connection |
||
| 3446 | * @return FluentPdoModel|$this |
||
| 3447 | */ |
||
| 3448 | public static function loadTable(string $tableName, AbstractPdo $connection=null) : FluentPdoModel |
||
| 3455 | |||
| 3456 | /** |
||
| 3457 | * @param string $columnName |
||
| 3458 | * @param int $cacheTtl |
||
| 3459 | * @param bool $flushCache |
||
| 3460 | * @return bool |
||
| 3461 | */ |
||
| 3462 | public function columnExists(string $columnName, int $cacheTtl=self::CACHE_NO, bool $flushCache=false) |
||
| 3467 | |||
| 3468 | /** |
||
| 3469 | * @param int $cacheTtl |
||
| 3470 | * @param bool $flushCache |
||
| 3471 | * @return FluentPdoModel|$this |
||
| 3472 | */ |
||
| 3473 | public function loadSchemaFromDb(int $cacheTtl=self::CACHE_NO, bool $flushCache=false) : FluentPdoModel |
||
| 3480 | |||
| 3481 | /** |
||
| 3482 | * @param int $cacheTtl |
||
| 3483 | * @param bool $flushCache |
||
| 3484 | * @return array |
||
| 3485 | */ |
||
| 3486 | public function getSchemaFromDb(int $cacheTtl=self::CACHE_NO, bool $flushCache=false) : array |
||
| 3498 | |||
| 3499 | /** |
||
| 3500 | * @param string $table |
||
| 3501 | * @param int $cacheTtl |
||
| 3502 | * @param bool $flushCache |
||
| 3503 | * @return array |
||
| 3504 | */ |
||
| 3505 | protected function _getColumnsByTableFromDb(string $table, int $cacheTtl=self::CACHE_NO, bool $flushCache=false) : array |
||
| 3521 | |||
| 3522 | /** |
||
| 3523 | * @param string $table |
||
| 3524 | * @return bool |
||
| 3525 | */ |
||
| 3526 | public function clearSchemaCache(string $table) : bool |
||
| 3530 | |||
| 3531 | /** |
||
| 3532 | * @param stdClass $record |
||
| 3533 | * @return stdClass |
||
| 3534 | */ |
||
| 3535 | public function cleanseRecord(stdClass $record) : stdClass |
||
| 3552 | |||
| 3553 | /** |
||
| 3554 | * @param stdClass $record |
||
| 3555 | * @param string $type |
||
| 3556 | * @return stdClass |
||
| 3557 | */ |
||
| 3558 | public function beforeSave(stdClass $record, string $type) : stdClass |
||
| 3567 | |||
| 3568 | /** |
||
| 3569 | * @param array $data |
||
| 3570 | * @param string $saveType |
||
| 3571 | * @return array |
||
| 3572 | */ |
||
| 3573 | public function cleanseWebData(array $data, string $saveType) : array |
||
| 3588 | |||
| 3589 | /** |
||
| 3590 | * @return array |
||
| 3591 | */ |
||
| 3592 | public function skeleton() : array |
||
| 3603 | |||
| 3604 | /** |
||
| 3605 | * @param bool $toString |
||
| 3606 | * @return array |
||
| 3607 | */ |
||
| 3608 | public function getErrors(bool $toString=false) : array |
||
| 3623 | |||
| 3624 | /** |
||
| 3625 | * @param bool $throw |
||
| 3626 | * @return FluentPdoModel|$this |
||
| 3627 | */ |
||
| 3628 | public function validationExceptions(bool $throw=true) : FluentPdoModel |
||
| 3634 | |||
| 3635 | /** |
||
| 3636 | * @param array $query array('_limit' => int, '_offset' => int, '_order' => string, '_fields' => string, _search) |
||
| 3637 | * |
||
| 3638 | * @return FluentPdoModel|$this |
||
| 3639 | * @throws Exception |
||
| 3640 | */ |
||
| 3641 | public function paginate(array $query=[]) : FluentPdoModel |
||
| 3655 | |||
| 3656 | /** |
||
| 3657 | * @param int $limit |
||
| 3658 | * @param int $offset |
||
| 3659 | * @return FluentPdoModel|$this |
||
| 3660 | */ |
||
| 3661 | protected function _setLimit(int $limit=0, int $offset=0) : FluentPdoModel |
||
| 3676 | |||
| 3677 | /** |
||
| 3678 | * @param array $fields |
||
| 3679 | * @return FluentPdoModel|$this |
||
| 3680 | * @throws Exception |
||
| 3681 | */ |
||
| 3682 | protected function _setFields(array $fields=[]) : FluentPdoModel |
||
| 3730 | |||
| 3731 | /** |
||
| 3732 | * @param string $orderBy |
||
| 3733 | * @return FluentPdoModel|$this|FluentPdoModel |
||
| 3734 | */ |
||
| 3735 | protected function _setOrderBy(string $orderBy='') : FluentPdoModel |
||
| 3772 | |||
| 3773 | /** |
||
| 3774 | * @return array |
||
| 3775 | */ |
||
| 3776 | public function getPagingMeta() |
||
| 3785 | |||
| 3786 | /** |
||
| 3787 | * @return FluentPdoModel|$this |
||
| 3788 | */ |
||
| 3789 | public function setPagingMeta() : FluentPdoModel |
||
| 3811 | |||
| 3812 | /** |
||
| 3813 | * Take a web request and format a query |
||
| 3814 | * |
||
| 3815 | * @param array $query |
||
| 3816 | * |
||
| 3817 | * @return FluentPdoModel|$this |
||
| 3818 | * @throws Exception |
||
| 3819 | */ |
||
| 3820 | public function filter(array $query=[]) : FluentPdoModel |
||
| 3886 | |||
| 3887 | /** |
||
| 3888 | * @param string $column |
||
| 3889 | * @param string $displayCol |
||
| 3890 | * @return string|null |
||
| 3891 | */ |
||
| 3892 | protected function _findFieldByQuery(string $column, string $displayCol) |
||
| 3939 | |||
| 3940 | /** |
||
| 3941 | * @param $keysOnly |
||
| 3942 | * @return array |
||
| 3943 | */ |
||
| 3944 | |||
| 3945 | public function columns(bool $keysOnly=true) : array |
||
| 3949 | |||
| 3950 | /** |
||
| 3951 | * @param string $field |
||
| 3952 | * @param mixed $value |
||
| 3953 | * @param array $pdoMetaData |
||
| 3954 | * @return float|int |
||
| 3955 | * @throws Exception |
||
| 3956 | */ |
||
| 3957 | protected function _fixTypeToSentinel(string $field, $value, array $pdoMetaData=[]) |
||
| 4009 | |||
| 4010 | /** |
||
| 4011 | * @param string $field |
||
| 4012 | * @param mixed $value |
||
| 4013 | * @param bool|false $permissive |
||
| 4014 | * @return float|int|null|string |
||
| 4015 | */ |
||
| 4016 | protected function _fixType(string $field, $value, bool $permissive=false) |
||
| 4067 | |||
| 4068 | /** |
||
| 4069 | * @param stdClass $record |
||
| 4070 | * @param string $type |
||
| 4071 | * @return stdClass |
||
| 4072 | */ |
||
| 4073 | public function fixTypesToSentinel(stdClass $record, string $type='') : stdClass |
||
| 4100 | |||
| 4101 | /** |
||
| 4102 | * @param stdClass $record |
||
| 4103 | * @param string $type |
||
| 4104 | * @return stdClass |
||
| 4105 | * @throws Exception |
||
| 4106 | */ |
||
| 4107 | public function applyHandlers(stdClass $record, string $type='INSERT') : stdClass |
||
| 4135 | |||
| 4136 | /** |
||
| 4137 | * @param string $field |
||
| 4138 | * @param mixed $value |
||
| 4139 | * @param string $type |
||
| 4140 | * @param stdClass $record |
||
| 4141 | * @return null |
||
| 4142 | * @throws Exception |
||
| 4143 | */ |
||
| 4144 | protected function applyHandler(string $field, $value, string $type='', stdClass $record=null) |
||
| 4168 | |||
| 4169 | /** |
||
| 4170 | * @param string $start |
||
| 4171 | * @param string $end |
||
| 4172 | * @param string $hayStack |
||
| 4173 | * @return mixed |
||
| 4174 | */ |
||
| 4175 | public static function between(string $start, string $end, string $hayStack) : string |
||
| 4179 | |||
| 4180 | /** |
||
| 4181 | * @param string $needle |
||
| 4182 | * @param string $hayStack |
||
| 4183 | * @param bool $returnOrigIfNeedleNotExists |
||
| 4184 | * @return mixed |
||
| 4185 | */ |
||
| 4186 | public static function before(string $needle, string $hayStack, bool $returnOrigIfNeedleNotExists=false) : string |
||
| 4196 | |||
| 4197 | /** |
||
| 4198 | * @param string $needle |
||
| 4199 | * @param string $hayStack |
||
| 4200 | * @param bool $returnOrigIfNeedleNotExists |
||
| 4201 | * @return string |
||
| 4202 | */ |
||
| 4203 | public static function after(string $needle, string $hayStack, bool $returnOrigIfNeedleNotExists=false) : string |
||
| 4212 | |||
| 4213 | /** |
||
| 4214 | * @return int |
||
| 4215 | */ |
||
| 4216 | public function getUserId() |
||
| 4220 | |||
| 4221 | /** |
||
| 4222 | * @param string $entity |
||
| 4223 | * @param int $id |
||
| 4224 | * @return int |
||
| 4225 | */ |
||
| 4226 | public function getMaskByResourceAndId(string $entity, int $id) : int |
||
| 4230 | |||
| 4231 | /** |
||
| 4232 | * @param string|int|null $time |
||
| 4233 | * @return string |
||
| 4234 | */ |
||
| 4235 | public static function date($time=null) : string |
||
| 4239 | |||
| 4240 | /** |
||
| 4241 | * @param string|int|null $time |
||
| 4242 | * @return string |
||
| 4243 | */ |
||
| 4244 | public static function dateTime($time=null) : string |
||
| 4248 | |||
| 4249 | /** |
||
| 4250 | * @param string|int|null $time |
||
| 4251 | * @return string |
||
| 4252 | */ |
||
| 4253 | public static function atom($time=null) : string |
||
| 4257 | |||
| 4258 | /** |
||
| 4259 | * @param string|int|null $time |
||
| 4260 | * @return int |
||
| 4261 | */ |
||
| 4262 | public static function getTime($time=null) : int |
||
| 4275 | |||
| 4276 | |||
| 4277 | /** |
||
| 4278 | * @param int $id |
||
| 4279 | * @param int $cacheTtl |
||
| 4280 | * @return string |
||
| 4281 | */ |
||
| 4282 | public function getCodeById(int $id, int $cacheTtl=self::ONE_DAY) : string |
||
| 4290 | } |
||
| 4291 |