Complex classes like ActiveRecord 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 ActiveRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 67 | abstract class ActiveRecord |
||
| 68 | { |
||
| 69 | /** |
||
| 70 | * The object ID. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | * |
||
| 74 | * @since 1.0 |
||
| 75 | */ |
||
| 76 | protected $ID; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The last database query run by this object. Useful for tracing an error. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | * |
||
| 83 | * @since 1.0 |
||
| 84 | */ |
||
| 85 | protected $lastQuery; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The version number of the object, used for locking mechanism. |
||
| 89 | * |
||
| 90 | * @var \Alpha\Model\Type\Integer |
||
| 91 | * |
||
| 92 | * @since 1.0 |
||
| 93 | */ |
||
| 94 | protected $version_num; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The timestamp of creation. |
||
| 98 | * |
||
| 99 | * @var \Alpha\Model\Type\Timestamp |
||
| 100 | * |
||
| 101 | * @since 1.0 |
||
| 102 | */ |
||
| 103 | protected $created_ts; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The ID of the person who created this record. |
||
| 107 | * |
||
| 108 | * @var \Alpha\Model\Type\Integer |
||
| 109 | * |
||
| 110 | * @since 1.0 |
||
| 111 | */ |
||
| 112 | protected $created_by; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The timestamp of the last update. |
||
| 116 | * |
||
| 117 | * @var \Alpha\Model\Type\Timestamp |
||
| 118 | * |
||
| 119 | * @since 1.0 |
||
| 120 | */ |
||
| 121 | protected $updated_ts; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The ID of the person who last updated this record. |
||
| 125 | * |
||
| 126 | * @var \Alpha\Model\Type\Integer |
||
| 127 | * |
||
| 128 | * @since 1.0 |
||
| 129 | */ |
||
| 130 | protected $updated_by; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * An array of the names of all of the default attributes of a persistent Record defined in this class. |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | * |
||
| 137 | * @since 1.0 |
||
| 138 | */ |
||
| 139 | protected $defaultAttributes = array('ID', 'lastQuery', 'version_num', 'dataLabels', 'created_ts', 'created_by', 'updated_ts', 'updated_by', 'defaultAttributes', 'transientAttributes', 'uniqueAttributes', 'TABLE_NAME', 'logger'); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * An array of the names of all of the transient attributes of a persistent Record which are not saved to the DB. |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | * |
||
| 146 | * @since 1.0 |
||
| 147 | */ |
||
| 148 | protected $transientAttributes = array('lastQuery', 'dataLabels', 'defaultAttributes', 'transientAttributes', 'uniqueAttributes', 'TABLE_NAME', 'logger'); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * An array of the uniquely-constained attributes of this persistent record. |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | * |
||
| 155 | * @since 1.0 |
||
| 156 | */ |
||
| 157 | protected $uniqueAttributes = array(); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * An array of the data labels used for displaying class attributes. |
||
| 161 | * |
||
| 162 | * @var array |
||
| 163 | * |
||
| 164 | * @since 1.0 |
||
| 165 | */ |
||
| 166 | protected $dataLabels = array(); |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Trace logger. |
||
| 170 | * |
||
| 171 | * @var \Alpha\Util\Logging\Logger |
||
| 172 | * |
||
| 173 | * @since 1.0 |
||
| 174 | */ |
||
| 175 | private static $logger = null; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Determines if we will maintain a _history table for this record (default is false). |
||
| 179 | * |
||
| 180 | * @var bool |
||
| 181 | * |
||
| 182 | * @since 1.2 |
||
| 183 | */ |
||
| 184 | private $maintainHistory = false; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * The constructor which sets up some housekeeping attributes. |
||
| 188 | * |
||
| 189 | * @since 1.0 |
||
| 190 | */ |
||
| 191 | public function __construct() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Disconnects the current database connection if one exists. |
||
| 215 | * |
||
| 216 | * @since 1.0 |
||
| 217 | */ |
||
| 218 | public static function disconnect() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns a 2d array, where each element in the array is another array representing a database row. |
||
| 228 | * |
||
| 229 | * @param string $sqlQuery |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | * |
||
| 233 | * @since 1.1 |
||
| 234 | * |
||
| 235 | * @throws \Alpha\Exception\CustomQueryException |
||
| 236 | */ |
||
| 237 | public function query($sqlQuery) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Populates the child object with the properties retrived from the database for the object $ID. |
||
| 254 | * |
||
| 255 | * @param int $ID The object ID of the business object to load. |
||
| 256 | * @param int $version Optionaly, provide the version to load that version from the [tablename]_history table. |
||
| 257 | * |
||
| 258 | * @since 1.0 |
||
| 259 | * |
||
| 260 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 261 | */ |
||
| 262 | public function load($ID, $version = 0) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Load all old versions (if any) of this record from the [tablename]_history table. |
||
| 297 | * |
||
| 298 | * @param int $ID The object ID of the record to load. |
||
| 299 | * |
||
| 300 | * @return array An array containing objects of this type of record object, order by version. |
||
| 301 | * |
||
| 302 | * @since 2.0 |
||
| 303 | * |
||
| 304 | * @throws \Alpha\Exception\RecordFoundException |
||
| 305 | */ |
||
| 306 | public function loadAllOldVersions($ID) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Populates the child object from the database table by the given attribute value. |
||
| 323 | * |
||
| 324 | * @param string $attribute The name of the attribute to load the object by. |
||
| 325 | * @param string $value The value of the attribute to load the object by. |
||
| 326 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 327 | * @param array $loadAttributes The attributes to load from the database to this object (leave blank to load all attributes) |
||
| 328 | * |
||
| 329 | * @since 1.0 |
||
| 330 | * |
||
| 331 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 332 | */ |
||
| 333 | public function loadByAttribute($attribute, $value, $ignoreClassType = false, $loadAttributes = array()) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Loads all of the objects of this class into an array which is returned. |
||
| 363 | * |
||
| 364 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 365 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 366 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 367 | * @param string $order The order to sort the objects by. |
||
| 368 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 369 | * |
||
| 370 | * @return array An array containing objects of this type of business object. |
||
| 371 | * |
||
| 372 | * @since 1.0 |
||
| 373 | * |
||
| 374 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 375 | */ |
||
| 376 | public function loadAll($start = 0, $limit = 0, $orderBy = 'ID', $order = 'ASC', $ignoreClassType = false) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Loads all of the objects of this class by the specified attribute into an array which is returned. |
||
| 401 | * |
||
| 402 | * @param string $attribute The attribute to load the objects by. |
||
| 403 | * @param string $value The value of the attribute to load the objects by. |
||
| 404 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 405 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 406 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 407 | * @param string $order The order to sort the objects by. |
||
| 408 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type. |
||
| 409 | * @param array $constructorArgs An optional array of contructor arguements to pass to the records that will be generated and returned. Supports a maximum of 5 arguements. |
||
| 410 | * |
||
| 411 | * @return array An array containing objects of this type of business object. |
||
| 412 | * |
||
| 413 | * @since 1.0 |
||
| 414 | * |
||
| 415 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 416 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 417 | */ |
||
| 418 | public function loadAllByAttribute($attribute, $value, $start = 0, $limit = 0, $orderBy = 'ID', $order = 'ASC', $ignoreClassType = false, $constructorArgs = array()) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Loads all of the objects of this class by the specified attributes into an array which is returned. |
||
| 443 | * |
||
| 444 | * @param array $attributes The attributes to load the objects by. |
||
| 445 | * @param array $values The values of the attributes to load the objects by. |
||
| 446 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 447 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 448 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 449 | * @param string $order The order to sort the objects by. |
||
| 450 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 451 | * |
||
| 452 | * @return array An array containing objects of this type of business object. |
||
| 453 | * |
||
| 454 | * @since 1.0 |
||
| 455 | * |
||
| 456 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 457 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 458 | */ |
||
| 459 | public function loadAllByAttributes($attributes = array(), $values = array(), $start = 0, $limit = 0, $orderBy = 'ID', $order = 'ASC', $ignoreClassType = false) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Loads all of the objects of this class that where updated (updated_ts value) on the date indicated. |
||
| 490 | * |
||
| 491 | * @param string $date The date for which to load the objects updated on, in the format 'YYYY-MM-DD'. |
||
| 492 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 493 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 494 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 495 | * @param string $order The order to sort the objects by. |
||
| 496 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 497 | * |
||
| 498 | * @return array An array containing objects of this type of business object. |
||
| 499 | * |
||
| 500 | * @since 1.0 |
||
| 501 | * |
||
| 502 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 503 | */ |
||
| 504 | public function loadAllByDayUpdated($date, $start = 0, $limit = 0, $orderBy = 'ID', $order = 'ASC', $ignoreClassType = false) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Loads all of the specified attribute values of this class by the specified attribute into an |
||
| 529 | * array which is returned. |
||
| 530 | * |
||
| 531 | * @param string $attribute The attribute name to load the field values by. |
||
| 532 | * @param string $value The value of the attribute to load the field values by. |
||
| 533 | * @param string $returnAttribute The name of the attribute to return. |
||
| 534 | * @param string $order The order to sort the records by. |
||
| 535 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type. |
||
| 536 | * |
||
| 537 | * @return array An array of field values. |
||
| 538 | * |
||
| 539 | * @since 1.0 |
||
| 540 | * |
||
| 541 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 542 | */ |
||
| 543 | public function loadAllFieldValuesByAttribute($attribute, $value, $returnAttribute, $order = 'ASC', $ignoreClassType = false) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Saves the object. If $this->ID is empty or null it will INSERT, otherwise UPDATE. |
||
| 560 | * |
||
| 561 | * @since 1.0 |
||
| 562 | * |
||
| 563 | * @throws \Alpha\Exception\FailedSaveException |
||
| 564 | * @throws \Alpha\Exception\LockingException |
||
| 565 | * @throws \Alpha\Exception\ValidationException |
||
| 566 | */ |
||
| 567 | public function save() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Saves relationship values, including lookup entries, for this record. |
||
| 610 | * |
||
| 611 | * @since 3.0 |
||
| 612 | * |
||
| 613 | * @throws \Alpha\Exception\FailedSaveException |
||
| 614 | */ |
||
| 615 | public function saveRelations() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Saves the field specified with the value supplied. Only works for persistent records. Note that no Alpha type |
||
| 684 | * validation is performed with this method! |
||
| 685 | * |
||
| 686 | * @param string $attribute The name of the attribute to save. |
||
| 687 | * @param mixed $value The value of the attribute to save. |
||
| 688 | * |
||
| 689 | * @since 1.0 |
||
| 690 | * |
||
| 691 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 692 | * @throws \Alpha\Exception\FailedSaveException |
||
| 693 | */ |
||
| 694 | public function saveAttribute($attribute, $value) |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Saves the history of the object in the [tablename]_history table. It will always perform an insert. |
||
| 730 | * |
||
| 731 | * @since 1.2 |
||
| 732 | * |
||
| 733 | * @throws \Alpha\Exception\FailedSaveException |
||
| 734 | */ |
||
| 735 | public function saveHistory() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Validates the object to be saved. |
||
| 756 | * |
||
| 757 | * @since 1.0 |
||
| 758 | * |
||
| 759 | * @throws \Alpha\Exception\ValidationException |
||
| 760 | */ |
||
| 761 | protected function validate() |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Deletes the current object from the database. |
||
| 799 | * |
||
| 800 | * @since 1.0 |
||
| 801 | * |
||
| 802 | * @throws \Alpha\Exception\FailedDeleteException |
||
| 803 | */ |
||
| 804 | public function delete() |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Delete all object instances from the database by the specified attribute matching the value provided. |
||
| 891 | * |
||
| 892 | * @param string $attribute The name of the field to delete the objects by. |
||
| 893 | * @param mixed $value The value of the field to delete the objects by. |
||
| 894 | * |
||
| 895 | * @return int The number of rows deleted. |
||
| 896 | * |
||
| 897 | * @since 1.0 |
||
| 898 | * |
||
| 899 | * @throws \Alpha\Exception\FailedDeleteException |
||
| 900 | */ |
||
| 901 | public function deleteAllByAttribute($attribute, $value) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Gets the version_num of the object from the database (returns 0 if the Record is not saved yet). |
||
| 938 | * |
||
| 939 | * @return int |
||
| 940 | * |
||
| 941 | * @since 1.0 |
||
| 942 | * |
||
| 943 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 944 | */ |
||
| 945 | public function getVersion() |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Builds a new database table for the Record class. |
||
| 970 | * |
||
| 971 | * @since 1.0 |
||
| 972 | * |
||
| 973 | * @throws \Alpha\Exception\AlphaException |
||
| 974 | */ |
||
| 975 | public function makeTable($checkIndexes = true) |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Builds a new database table for the Record class to story it's history of changes. |
||
| 1000 | * |
||
| 1001 | * @since 1.2 |
||
| 1002 | * |
||
| 1003 | * @throws \Alpha\Exception\AlphaException |
||
| 1004 | */ |
||
| 1005 | public function makeHistoryTable() |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Re-builds the table if the model requirements have changed. All data is lost! |
||
| 1030 | * |
||
| 1031 | * @since 1.0 |
||
| 1032 | * |
||
| 1033 | * @throws \Alpha\Exception\AlphaException |
||
| 1034 | */ |
||
| 1035 | public function rebuildTable() |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Drops the table if the model requirements have changed. All data is lost! |
||
| 1058 | * |
||
| 1059 | * @since 1.0 |
||
| 1060 | * |
||
| 1061 | * @param string $tableName Optional table name, leave blank for the defined table for this class to be dropped |
||
| 1062 | * |
||
| 1063 | * @throws \Alpha\Exception\AlphaException |
||
| 1064 | */ |
||
| 1065 | public function dropTable($tableName = null) |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Adds in a new class property without loosing existing data (does an ALTER TABLE query on the |
||
| 1088 | * database). |
||
| 1089 | * |
||
| 1090 | * @param string $propName The name of the new field to add to the database table. |
||
| 1091 | * |
||
| 1092 | * @since 1.0 |
||
| 1093 | * |
||
| 1094 | * @throws \Alpha\Exception\AlphaException |
||
| 1095 | */ |
||
| 1096 | public function addProperty($propName) |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Populates the current business object from the provided hash array. |
||
| 1119 | * |
||
| 1120 | * @param array $hashArray |
||
| 1121 | * |
||
| 1122 | * @since 1.2.1 |
||
| 1123 | */ |
||
| 1124 | public function populateFromArray($hashArray) |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Gets the maximum ID value from the database for this class type. |
||
| 1163 | * |
||
| 1164 | * @return int The maximum ID value in the class table. |
||
| 1165 | * |
||
| 1166 | * @since 1.0 |
||
| 1167 | * |
||
| 1168 | * @throws \Alpha\Exception\AlphaException |
||
| 1169 | */ |
||
| 1170 | public function getMAX() |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Gets the count from the database for the amount of objects of this class. |
||
| 1195 | * |
||
| 1196 | * @param array $attributes The attributes to count the objects by (optional). |
||
| 1197 | * @param array $values The values of the attributes to count the objects by (optional). |
||
| 1198 | * |
||
| 1199 | * @return int |
||
| 1200 | * |
||
| 1201 | * @since 1.0 |
||
| 1202 | * |
||
| 1203 | * @throws \Alpha\Exception\AlphaException |
||
| 1204 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1205 | */ |
||
| 1206 | public function getCount($attributes = array(), $values = array()) |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Gets the count from the database for the amount of entries in the [tableName]_history table for this business object. Only call |
||
| 1235 | * this method on classes where maintainHistory = true, otherwise an exception will be thrown. |
||
| 1236 | * |
||
| 1237 | * @return int |
||
| 1238 | * |
||
| 1239 | * @since 1.2 |
||
| 1240 | * |
||
| 1241 | * @throws \Alpha\Exception\AlphaException |
||
| 1242 | */ |
||
| 1243 | public function getHistoryCount() |
||
| 1265 | |||
| 1266 | /** |
||
| 1267 | * Gets the ID for the object in zero-padded format (same as getID()). |
||
| 1268 | * |
||
| 1269 | * @return string 11 digit zero-padded ID value. |
||
| 1270 | * |
||
| 1271 | * @since 1.0 |
||
| 1272 | */ |
||
| 1273 | final public function getID() |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Method for getting version number of the object. |
||
| 1287 | * |
||
| 1288 | * @return \Alpha\Model\Type\Integer The object version number. |
||
| 1289 | * |
||
| 1290 | * @since 1.0 |
||
| 1291 | */ |
||
| 1292 | public function getVersionNumber() |
||
| 1299 | |||
| 1300 | /** |
||
| 1301 | * Populate all of the enum options for this object from the database. |
||
| 1302 | * |
||
| 1303 | * @since 1.0 |
||
| 1304 | * |
||
| 1305 | * @throws \Alpha\Exception\AlphaException |
||
| 1306 | */ |
||
| 1307 | protected function setEnumOptions() |
||
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Generic getter method for accessing class properties. Will use the method get.ucfirst($prop) instead if that |
||
| 1330 | * method exists at a child level (by default). Set $noChildMethods to true if you don't want to use any |
||
| 1331 | * get.ucfirst($prop) method even if it exists, false otherwise (default). |
||
| 1332 | * |
||
| 1333 | * @param string $prop The name of the object property to get. |
||
| 1334 | * @param bool $noChildMethods Set to true if you do not want to use getters in the child object, defaults to false. |
||
| 1335 | * |
||
| 1336 | * @return mixed The property value. |
||
| 1337 | * |
||
| 1338 | * @since 1.0 |
||
| 1339 | * |
||
| 1340 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1341 | * @throws \Alpha\Exception\AlphaException |
||
| 1342 | */ |
||
| 1343 | public function get($prop, $noChildMethods = false) |
||
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Generic setter method for setting class properties. Will use the method set.ucfirst($prop) instead if that |
||
| 1399 | * method exists at a child level (by default). Set $noChildMethods to true if you don't want to use |
||
| 1400 | * any get.ucfirst($prop) method even if it exists, false otherwise (default). |
||
| 1401 | * |
||
| 1402 | * @param string $prop The name of the property to set. |
||
| 1403 | * @param mixed $value The value of the property to set. |
||
| 1404 | * @param bool $noChildMethods Set to true if you do not want to use setters in the child object, defaults to false. |
||
| 1405 | * |
||
| 1406 | * @since 1.0 |
||
| 1407 | * |
||
| 1408 | * @throws \Alpha\Exception\AlphaException |
||
| 1409 | */ |
||
| 1410 | public function set($prop, $value, $noChildMethods = false) |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Gets the property object rather than the value for complex attributes. Returns false if |
||
| 1455 | * the property exists but is private. |
||
| 1456 | * |
||
| 1457 | * @param string $prop The name of the property we are getting. |
||
| 1458 | * |
||
| 1459 | * @return \Alpha\Model\Type\Type|bool The complex type object found. |
||
| 1460 | * |
||
| 1461 | * @since 1.0 |
||
| 1462 | * |
||
| 1463 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1464 | */ |
||
| 1465 | public function getPropObject($prop) |
||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * Checks to see if the table exists in the database for the current business class. |
||
| 1510 | * |
||
| 1511 | * @param bool $checkHistoryTable Set to true if you want to check for the existance of the _history table for this DAO. |
||
| 1512 | * |
||
| 1513 | * @return bool |
||
| 1514 | * |
||
| 1515 | * @since 1.0 |
||
| 1516 | * |
||
| 1517 | * @throws \Alpha\Exception\AlphaException |
||
| 1518 | */ |
||
| 1519 | public function checkTableExists($checkHistoryTable = false) |
||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * Static method to check the database and see if the table for the indicated Record class name |
||
| 1544 | * exists (assumes table name will be $recordClassName less "Object"). |
||
| 1545 | * |
||
| 1546 | * @param string $recordClassName The name of the business object class we are checking. |
||
| 1547 | * @param bool $checkHistoryTable Set to true if you want to check for the existance of the _history table for this DAO. |
||
| 1548 | * |
||
| 1549 | * @return bool |
||
| 1550 | * |
||
| 1551 | * @since 1.0 |
||
| 1552 | * |
||
| 1553 | * @throws \Alpha\Exception\AlphaException |
||
| 1554 | */ |
||
| 1555 | public static function checkRecordTableExists($recordClassName, $checkHistoryTable = false) |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Checks to see if the table in the database matches (for fields) the business class definition, i.e. if the |
||
| 1575 | * database table is in sync with the class definition. |
||
| 1576 | * |
||
| 1577 | * @return bool |
||
| 1578 | * |
||
| 1579 | * @since 1.0 |
||
| 1580 | * |
||
| 1581 | * @throws \Alpha\Exception\AlphaException |
||
| 1582 | */ |
||
| 1583 | public function checkTableNeedsUpdate() |
||
| 1613 | |||
| 1614 | /** |
||
| 1615 | * Returns an array containing any properties on the class which have not been created on the database |
||
| 1616 | * table yet. |
||
| 1617 | * |
||
| 1618 | * @return array An array of missing fields in the database table. |
||
| 1619 | * |
||
| 1620 | * @since 1.0 |
||
| 1621 | * |
||
| 1622 | * @throws \Alpha\Exception\AlphaException |
||
| 1623 | */ |
||
| 1624 | public function findMissingFields() |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Getter for the TABLE_NAME, which should be set by a child of this class. |
||
| 1649 | * |
||
| 1650 | * @return string The table name in the database. |
||
| 1651 | * |
||
| 1652 | * @since 1.0 |
||
| 1653 | * |
||
| 1654 | * @throws \Alpha\Exception\AlphaException |
||
| 1655 | */ |
||
| 1656 | public function getTableName() |
||
| 1672 | |||
| 1673 | /** |
||
| 1674 | * Method for getting the ID of the person who created this record. |
||
| 1675 | * |
||
| 1676 | * @return \Alpha\Model\Type\Integer The ID of the creator. |
||
| 1677 | * |
||
| 1678 | * @since 1.0 |
||
| 1679 | */ |
||
| 1680 | public function getCreatorId() |
||
| 1687 | |||
| 1688 | /** |
||
| 1689 | * Method for getting the ID of the person who updated this record. |
||
| 1690 | * |
||
| 1691 | * @return \Alpha\Model\Type\Integer The ID of the updator. |
||
| 1692 | * |
||
| 1693 | * @since 1.0 |
||
| 1694 | */ |
||
| 1695 | public function getUpdatorId() |
||
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Method for getting the date/time of when the Record was created. |
||
| 1705 | * |
||
| 1706 | * @return \Alpha\Model\Type\Timestamp |
||
| 1707 | * |
||
| 1708 | * @since 1.0 |
||
| 1709 | */ |
||
| 1710 | public function getCreateTS() |
||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Method for getting the date/time of when the Record was last updated. |
||
| 1720 | * |
||
| 1721 | * @return \Alpha\Model\Type\Timestamp |
||
| 1722 | * |
||
| 1723 | * @since 1.0 |
||
| 1724 | */ |
||
| 1725 | public function getUpdateTS() |
||
| 1732 | |||
| 1733 | /** |
||
| 1734 | * Adds the name of the attribute provided to the list of transient (non-saved) attributes for this record. |
||
| 1735 | * |
||
| 1736 | * @param string $attributeName The name of the attribute to not save. |
||
| 1737 | * |
||
| 1738 | * @since 1.0 |
||
| 1739 | */ |
||
| 1740 | public function markTransient($attributeName) |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * Removes the name of the attribute provided from the list of transient (non-saved) attributes for this record, |
||
| 1749 | * ensuring that it will be saved on the next attempt. |
||
| 1750 | * |
||
| 1751 | * @param string $attributeName The name of the attribute to save. |
||
| 1752 | * |
||
| 1753 | * @since 1.0 |
||
| 1754 | */ |
||
| 1755 | public function markPersistent($attributeName) |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Adds the name of the attribute(s) provided to the list of unique (constrained) attributes for this record. |
||
| 1764 | * |
||
| 1765 | * @param string $attribute1Name The first attribute to mark unique in the database. |
||
| 1766 | * @param string $attribute2Name The second attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1767 | * @param string $attribute3Name The third attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1768 | * |
||
| 1769 | * @since 1.0 |
||
| 1770 | */ |
||
| 1771 | protected function markUnique($attribute1Name, $attribute2Name = '', $attribute3Name = '') |
||
| 1790 | |||
| 1791 | /** |
||
| 1792 | * Returns the array of names of unique attributes on this record. |
||
| 1793 | * |
||
| 1794 | * @return array |
||
| 1795 | * |
||
| 1796 | * @since 1.1 |
||
| 1797 | */ |
||
| 1798 | public function getUniqueAttributes() |
||
| 1805 | |||
| 1806 | /** |
||
| 1807 | * Gets an array of all of the names of the active database indexes for this class. |
||
| 1808 | * |
||
| 1809 | * @return array An array of database indexes on this table. |
||
| 1810 | * |
||
| 1811 | * @since 1.0 |
||
| 1812 | * |
||
| 1813 | * @throws \Alpha\Exception\AlphaException |
||
| 1814 | */ |
||
| 1815 | public function getIndexes() |
||
| 1829 | |||
| 1830 | /** |
||
| 1831 | * Creates a foreign key constraint (index) in the database on the given attribute. |
||
| 1832 | * |
||
| 1833 | * @param string $attributeName The name of the attribute to apply the index on. |
||
| 1834 | * @param string $relatedClass The name of the related class in the format "NameObject". |
||
| 1835 | * @param string $relatedClassAttribute The name of the field to relate to on the related class. |
||
| 1836 | * @param string $indexName The optional name for the index, will calculate if not provided. |
||
| 1837 | * |
||
| 1838 | * @since 1.0 |
||
| 1839 | * |
||
| 1840 | * @throws \Alpha\Exception\FailedIndexCreateException |
||
| 1841 | */ |
||
| 1842 | public function createForeignIndex($attributeName, $relatedClass, $relatedClassAttribute, $indexName = null) |
||
| 1872 | |||
| 1873 | /** |
||
| 1874 | * Creates a unique index in the database on the given attribute(s). |
||
| 1875 | * |
||
| 1876 | * @param string $attribute1Name The first attribute to mark unique in the database. |
||
| 1877 | * @param string $attribute2Name The second attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1878 | * @param string $attribute3Name The third attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1879 | * |
||
| 1880 | * @since 1.0 |
||
| 1881 | * |
||
| 1882 | * @throws \Alpha\Exception\FailedIndexCreateException |
||
| 1883 | */ |
||
| 1884 | public function createUniqueIndex($attribute1Name, $attribute2Name = '', $attribute3Name = '') |
||
| 1904 | |||
| 1905 | /** |
||
| 1906 | * Gets the data labels array. |
||
| 1907 | * |
||
| 1908 | * @return array An array of attribute labels. |
||
| 1909 | * |
||
| 1910 | * @since 1.0 |
||
| 1911 | */ |
||
| 1912 | public function getDataLabels() |
||
| 1919 | |||
| 1920 | /** |
||
| 1921 | * Sets the data labels array. |
||
| 1922 | * |
||
| 1923 | * @param array $labels |
||
| 1924 | * |
||
| 1925 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1926 | * |
||
| 1927 | * @since 1.2 |
||
| 1928 | */ |
||
| 1929 | public function setDataLabels($labels) |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * Gets the data label for the given attribute name. |
||
| 1944 | * |
||
| 1945 | * @param $att The attribute name to get the label for. |
||
| 1946 | * |
||
| 1947 | * @return string |
||
| 1948 | * |
||
| 1949 | * @since 1.0 |
||
| 1950 | * |
||
| 1951 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1952 | */ |
||
| 1953 | public function getDataLabel($att) |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * Loops over the core and custom Record directories and builds an array of all of the Record class names in the system. |
||
| 1969 | * |
||
| 1970 | * @return array An array of business object class names. |
||
| 1971 | * |
||
| 1972 | * @since 1.0 |
||
| 1973 | */ |
||
| 1974 | public static function getRecordClassNames() |
||
| 2024 | |||
| 2025 | /** |
||
| 2026 | * Get the array of default attribute names. |
||
| 2027 | * |
||
| 2028 | * @return array An array of attribute names. |
||
| 2029 | * |
||
| 2030 | * @since 1.0 |
||
| 2031 | */ |
||
| 2032 | public function getDefaultAttributes() |
||
| 2039 | |||
| 2040 | /** |
||
| 2041 | * Get the array of transient attribute names. |
||
| 2042 | * |
||
| 2043 | * @return array An array of attribute names. |
||
| 2044 | * |
||
| 2045 | * @since 1.0 |
||
| 2046 | */ |
||
| 2047 | public function getTransientAttributes() |
||
| 2054 | |||
| 2055 | /** |
||
| 2056 | * Get the array of persistent attribute names, i.e. those that are saved in the database. |
||
| 2057 | * |
||
| 2058 | * @return array An array of attribute names. |
||
| 2059 | * |
||
| 2060 | * @since 1.0 |
||
| 2061 | */ |
||
| 2062 | public function getPersistentAttributes() |
||
| 2085 | |||
| 2086 | /** |
||
| 2087 | * Setter for the Object ID (ID). |
||
| 2088 | * |
||
| 2089 | * @param int $ID The Object ID. |
||
| 2090 | * |
||
| 2091 | * @since 1.0 |
||
| 2092 | */ |
||
| 2093 | public function setID($ID) |
||
| 2099 | |||
| 2100 | /** |
||
| 2101 | * Inspector to see if the business object is transient (not presently stored in the database). |
||
| 2102 | * |
||
| 2103 | * @return bool |
||
| 2104 | * |
||
| 2105 | * @since 1.0 |
||
| 2106 | */ |
||
| 2107 | public function isTransient() |
||
| 2121 | |||
| 2122 | /** |
||
| 2123 | * Get the last database query run on this object. |
||
| 2124 | * |
||
| 2125 | * @return string An SQL query string. |
||
| 2126 | * |
||
| 2127 | * @since 1.0 |
||
| 2128 | */ |
||
| 2129 | public function getLastQuery() |
||
| 2136 | |||
| 2137 | /** |
||
| 2138 | * Unsets all of the attributes of this object to null. |
||
| 2139 | * |
||
| 2140 | * @since 1.0 |
||
| 2141 | */ |
||
| 2142 | private function clear() |
||
| 2159 | |||
| 2160 | /** |
||
| 2161 | * Reloads the object from the database, overwritting any attribute values in memory. |
||
| 2162 | * |
||
| 2163 | * @since 1.0 |
||
| 2164 | * |
||
| 2165 | * @throws \Alpha\Exception\AlphaException |
||
| 2166 | */ |
||
| 2167 | public function reload() |
||
| 2178 | |||
| 2179 | /** |
||
| 2180 | * Checks that a record exists for the Record in the database. |
||
| 2181 | * |
||
| 2182 | * @param int $ID The Object ID of the object we want to see whether it exists or not. |
||
| 2183 | * |
||
| 2184 | * @return bool |
||
| 2185 | * |
||
| 2186 | * @since 1.0 |
||
| 2187 | * |
||
| 2188 | * @throws \Alpha\Exception\AlphaException |
||
| 2189 | */ |
||
| 2190 | public function checkRecordExists($ID) |
||
| 2212 | |||
| 2213 | /** |
||
| 2214 | * Checks to see if the table name matches the classname, and if not if the table |
||
| 2215 | * name matches the classname name of another record, i.e. the table is used to store |
||
| 2216 | * multiple types of records. |
||
| 2217 | * |
||
| 2218 | * @return bool |
||
| 2219 | * |
||
| 2220 | * @since 1.0 |
||
| 2221 | * |
||
| 2222 | * @throws \Alpha\Exception\BadTableNameException |
||
| 2223 | */ |
||
| 2224 | public function isTableOverloaded() |
||
| 2238 | |||
| 2239 | /** |
||
| 2240 | * Starts a new database transaction. |
||
| 2241 | * |
||
| 2242 | * @param ActiveRecord $record The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2243 | * |
||
| 2244 | * @since 1.0 |
||
| 2245 | * |
||
| 2246 | * @throws \Alpha\Exception\AlphaException |
||
| 2247 | */ |
||
| 2248 | public static function begin($record = null) |
||
| 2273 | |||
| 2274 | /** |
||
| 2275 | * Commits the current database transaction. |
||
| 2276 | * |
||
| 2277 | * @param ActiveRecord $record The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2278 | * |
||
| 2279 | * @since 1.0 |
||
| 2280 | * |
||
| 2281 | * @throws \Alpha\Exception\FailedSaveException |
||
| 2282 | */ |
||
| 2283 | public static function commit($record = null) |
||
| 2308 | |||
| 2309 | /** |
||
| 2310 | * Aborts the current database transaction. |
||
| 2311 | * |
||
| 2312 | * @param ActiveRecord $record The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2313 | * |
||
| 2314 | * @since 1.0 |
||
| 2315 | * |
||
| 2316 | * @throws \Alpha\Exception\AlphaException |
||
| 2317 | */ |
||
| 2318 | public static function rollback($record = null) |
||
| 2343 | |||
| 2344 | /** |
||
| 2345 | * Static method that tries to determine if the system database has been installed or not. |
||
| 2346 | * |
||
| 2347 | * @return bool |
||
| 2348 | * |
||
| 2349 | * @since 1.0 |
||
| 2350 | */ |
||
| 2351 | public static function isInstalled() |
||
| 2374 | |||
| 2375 | /** |
||
| 2376 | * Returns true if the Record has a Relation property called tags, false otherwise. |
||
| 2377 | * |
||
| 2378 | * @return bool |
||
| 2379 | * |
||
| 2380 | * @since 1.0 |
||
| 2381 | */ |
||
| 2382 | public function isTagged() |
||
| 2390 | |||
| 2391 | /** |
||
| 2392 | * Returns the contents of the taggedAttributes array, or an empty array if that does not exist. |
||
| 2393 | * |
||
| 2394 | * @return array |
||
| 2395 | * |
||
| 2396 | * @since 1.2.3 |
||
| 2397 | */ |
||
| 2398 | public function getTaggedAttributes() |
||
| 2406 | |||
| 2407 | /** |
||
| 2408 | * Setter for the Record version number. |
||
| 2409 | * |
||
| 2410 | * @param int $versionNumber The version number. |
||
| 2411 | * |
||
| 2412 | * @since 1.0 |
||
| 2413 | */ |
||
| 2414 | private function setVersion($versionNumber) |
||
| 2418 | |||
| 2419 | /** |
||
| 2420 | * Cast a Record to another type of record. A new Record will be returned with the same ID and |
||
| 2421 | * version_num as the old record, so this is NOT a true cast but is a copy. All attribute |
||
| 2422 | * values will be copied accross. |
||
| 2423 | * |
||
| 2424 | * @param string $targetClassName The fully-qualified name of the target Record class. |
||
| 2425 | * @param \Alpha\Model\ActiveRecord $originalRecord The original business object. |
||
| 2426 | * |
||
| 2427 | * @return \Alpha\Model\ActiveRecord The new business object resulting from the cast. |
||
| 2428 | * |
||
| 2429 | * @since 1.0 |
||
| 2430 | */ |
||
| 2431 | public function cast($targetClassName, $originalRecord) |
||
| 2465 | |||
| 2466 | /** |
||
| 2467 | * Returns the simple class name, stripped of the namespace. |
||
| 2468 | * |
||
| 2469 | * @return string |
||
| 2470 | * |
||
| 2471 | * @since 1.0 |
||
| 2472 | */ |
||
| 2473 | public function getFriendlyClassName() |
||
| 2479 | |||
| 2480 | /** |
||
| 2481 | * Check to see if an attribute exists on the record. |
||
| 2482 | * |
||
| 2483 | * @param string $attribute The attribute name. |
||
| 2484 | * |
||
| 2485 | * @return bool |
||
| 2486 | * |
||
| 2487 | * @since 1.0 |
||
| 2488 | */ |
||
| 2489 | public function hasAttribute($attribute) |
||
| 2493 | |||
| 2494 | /** |
||
| 2495 | * Stores the business object to the configured cache instance. |
||
| 2496 | * |
||
| 2497 | * @since 1.1 |
||
| 2498 | */ |
||
| 2499 | public function addToCache() |
||
| 2514 | |||
| 2515 | /** |
||
| 2516 | * Removes the business object from the configured cache instance. |
||
| 2517 | * |
||
| 2518 | * @since 1.1 |
||
| 2519 | */ |
||
| 2520 | public function removeFromCache() |
||
| 2535 | |||
| 2536 | /** |
||
| 2537 | * Attempts to load the business object from the configured cache instance. |
||
| 2538 | * |
||
| 2539 | * @since 1.1 |
||
| 2540 | * |
||
| 2541 | * @return bool |
||
| 2542 | */ |
||
| 2543 | public function loadFromCache() |
||
| 2591 | |||
| 2592 | /** |
||
| 2593 | * Sets the last query executed on this business object. |
||
| 2594 | * |
||
| 2595 | * @param string $query |
||
| 2596 | * |
||
| 2597 | * @since 1.1 |
||
| 2598 | */ |
||
| 2599 | public function setLastQuery($query) |
||
| 2604 | |||
| 2605 | /** |
||
| 2606 | * Re-initialize the static logger property on the Record after de-serialize, as PHP does |
||
| 2607 | * not serialize static properties. |
||
| 2608 | * |
||
| 2609 | * @since 1.2 |
||
| 2610 | */ |
||
| 2611 | public function __wakeup() |
||
| 2617 | |||
| 2618 | /** |
||
| 2619 | * Sets maintainHistory attribute on this DAO. |
||
| 2620 | * |
||
| 2621 | * @param bool $maintainHistory |
||
| 2622 | * |
||
| 2623 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 2624 | * |
||
| 2625 | * @since 1.2 |
||
| 2626 | */ |
||
| 2627 | public function setMaintainHistory($maintainHistory) |
||
| 2635 | |||
| 2636 | /** |
||
| 2637 | * Gets the value of the maintainHistory attribute. |
||
| 2638 | * |
||
| 2639 | * @return bool |
||
| 2640 | * |
||
| 2641 | * @since 1.2 |
||
| 2642 | */ |
||
| 2643 | public function getMaintainHistory() |
||
| 2647 | |||
| 2648 | /** |
||
| 2649 | * Return a hash array of the object containing attribute names and simplfied values. |
||
| 2650 | * |
||
| 2651 | * @return array |
||
| 2652 | * |
||
| 2653 | * @since 1.2.4 |
||
| 2654 | */ |
||
| 2655 | public function toArray() |
||
| 2679 | |||
| 2680 | /** |
||
| 2681 | * Check to see if the configured database exists. |
||
| 2682 | * |
||
| 2683 | * @return bool |
||
| 2684 | * |
||
| 2685 | * @since 2.0 |
||
| 2686 | */ |
||
| 2687 | public static function checkDatabaseExists() |
||
| 2696 | |||
| 2697 | /** |
||
| 2698 | * Creates the configured database. |
||
| 2699 | * |
||
| 2700 | * @throws \Alpha\Exception\AlphaException |
||
| 2701 | * |
||
| 2702 | * @since 2.0 |
||
| 2703 | */ |
||
| 2704 | public static function createDatabase() |
||
| 2712 | |||
| 2713 | /** |
||
| 2714 | * Drops the configured database. |
||
| 2715 | * |
||
| 2716 | * @throws \Alpha\Exception\AlphaException |
||
| 2717 | * |
||
| 2718 | * @since 2.0 |
||
| 2719 | */ |
||
| 2720 | public static function dropDatabase() |
||
| 2728 | |||
| 2729 | /** |
||
| 2730 | * Backup the configured database. |
||
| 2731 | * |
||
| 2732 | * @param string $targetFile The file that the backup data will be written to. |
||
| 2733 | * |
||
| 2734 | * @throws \Alpha\Exception\AlphaException |
||
| 2735 | * |
||
| 2736 | * @since 3.0 |
||
| 2737 | */ |
||
| 2738 | public static function backupDatabase($targetFile) |
||
| 2746 | } |
||
| 2747 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: