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 $OID; |
||
| 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 OID of the person who created this BO. |
||
| 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 OID of the person who last updated this BO. |
||
| 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 BO defined in this class. |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | * |
||
| 137 | * @since 1.0 |
||
| 138 | */ |
||
| 139 | protected $defaultAttributes = array('OID', '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 BO 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 BO. |
||
| 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) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Populates the child object with the properties retrived from the database for the object $OID. |
||
| 253 | * |
||
| 254 | * @param int $OID The object ID of the business object to load. |
||
| 255 | * @param int $version Optionaly, provide the version to load that version from the [tablename]_history table. |
||
| 256 | * |
||
| 257 | * @since 1.0 |
||
| 258 | * |
||
| 259 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 260 | */ |
||
| 261 | public function load($OID, $version = 0) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Load all old versions (if any) of this record from the [tablename]_history table. |
||
| 295 | * |
||
| 296 | * @param int $OID The object ID of the record to load. |
||
| 297 | * |
||
| 298 | * @return array An array containing objects of this type of record object, order by version. |
||
| 299 | * |
||
| 300 | * @since 2.0 |
||
| 301 | * |
||
| 302 | * @throws \Alpha\Exception\RecordFoundException |
||
| 303 | */ |
||
| 304 | public function loadAllOldVersions($OID) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Populates the child object from the database table by the given attribute value. |
||
| 320 | * |
||
| 321 | * @param string $attribute The name of the attribute to load the object by. |
||
| 322 | * @param string $value The value of the attribute to load the object by. |
||
| 323 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 324 | * @param array $loadAttributes The attributes to load from the database to this object (leave blank to load all attributes) |
||
| 325 | * |
||
| 326 | * @since 1.0 |
||
| 327 | * |
||
| 328 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 329 | */ |
||
| 330 | public function loadByAttribute($attribute, $value, $ignoreClassType = false, $loadAttributes = array()) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Loads all of the objects of this class into an array which is returned. |
||
| 359 | * |
||
| 360 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 361 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 362 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 363 | * @param string $order The order to sort the objects by. |
||
| 364 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 365 | * |
||
| 366 | * @return array An array containing objects of this type of business object. |
||
| 367 | * |
||
| 368 | * @since 1.0 |
||
| 369 | * |
||
| 370 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 371 | */ |
||
| 372 | public function loadAll($start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Loads all of the objects of this class by the specified attribute into an array which is returned. |
||
| 396 | * |
||
| 397 | * @param string $attribute The attribute to load the objects by. |
||
| 398 | * @param string $value The value of the attribute to load the objects by. |
||
| 399 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 400 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 401 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 402 | * @param string $order The order to sort the objects by. |
||
| 403 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type. |
||
| 404 | * @param array $constructorArgs An optional array of contructor arguements to pass to the BOs that will be generated and returned. Supports a maximum of 5 arguements. |
||
| 405 | * |
||
| 406 | * @return array An array containing objects of this type of business object. |
||
| 407 | * |
||
| 408 | * @since 1.0 |
||
| 409 | * |
||
| 410 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 411 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 412 | */ |
||
| 413 | public function loadAllByAttribute($attribute, $value, $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false, $constructorArgs = array()) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Loads all of the objects of this class by the specified attributes into an array which is returned. |
||
| 437 | * |
||
| 438 | * @param array $attributes The attributes to load the objects by. |
||
| 439 | * @param array $values The values of the attributes to load the objects by. |
||
| 440 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 441 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 442 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 443 | * @param string $order The order to sort the objects by. |
||
| 444 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 445 | * |
||
| 446 | * @return array An array containing objects of this type of business object. |
||
| 447 | * |
||
| 448 | * @since 1.0 |
||
| 449 | * |
||
| 450 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 451 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 452 | */ |
||
| 453 | public function loadAllByAttributes($attributes = array(), $values = array(), $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Loads all of the objects of this class that where updated (updated_ts value) on the date indicated. |
||
| 483 | * |
||
| 484 | * @param string $date The date for which to load the objects updated on, in the format 'YYYY-MM-DD'. |
||
| 485 | * @param int $start The start of the SQL LIMIT clause, useful for pagination. |
||
| 486 | * @param int $limit The amount (limit) of objects to load, useful for pagination. |
||
| 487 | * @param string $orderBy The name of the field to sort the objects by. |
||
| 488 | * @param string $order The order to sort the objects by. |
||
| 489 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type |
||
| 490 | * |
||
| 491 | * @return array An array containing objects of this type of business object. |
||
| 492 | * |
||
| 493 | * @since 1.0 |
||
| 494 | * |
||
| 495 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 496 | */ |
||
| 497 | public function loadAllByDayUpdated($date, $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Loads all of the specified attribute values of this class by the specified attribute into an |
||
| 521 | * array which is returned. |
||
| 522 | * |
||
| 523 | * @param string $attribute The attribute name to load the field values by. |
||
| 524 | * @param string $value The value of the attribute to load the field values by. |
||
| 525 | * @param string $returnAttribute The name of the attribute to return. |
||
| 526 | * @param string $order The order to sort the BOs by. |
||
| 527 | * @param bool $ignoreClassType Default is false, set to true if you want to load from overloaded tables and ignore the class type. |
||
| 528 | * |
||
| 529 | * @return array An array of field values. |
||
| 530 | * |
||
| 531 | * @since 1.0 |
||
| 532 | * |
||
| 533 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 534 | */ |
||
| 535 | public function loadAllFieldValuesByAttribute($attribute, $value, $returnAttribute, $order = 'ASC', $ignoreClassType = false) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Saves the object. If $this->OID is empty or null it will INSERT, otherwise UPDATE. |
||
| 551 | * |
||
| 552 | * @since 1.0 |
||
| 553 | * |
||
| 554 | * @throws \Alpha\Exception\FailedSaveException |
||
| 555 | * @throws \Alpha\Exception\LockingException |
||
| 556 | * @throws \Alpha\Exception\ValidationException |
||
| 557 | */ |
||
| 558 | public function save() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Saves the field specified with the value supplied. Only works for persistent BOs. Note that no Alpha type |
||
| 586 | * validation is performed with this method! |
||
| 587 | * |
||
| 588 | * @param string $attribute The name of the attribute to save. |
||
| 589 | * @param mixed $value The value of the attribute to save. |
||
| 590 | * |
||
| 591 | * @since 1.0 |
||
| 592 | * |
||
| 593 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 594 | * @throws \Alpha\Exception\FailedSaveException |
||
| 595 | */ |
||
| 596 | public function saveAttribute($attribute, $value) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Saves the history of the object in the [tablename]_history table. It will always perform an insert. |
||
| 631 | * |
||
| 632 | * @since 1.2 |
||
| 633 | * |
||
| 634 | * @throws \Alpha\Exception\FailedSaveException |
||
| 635 | */ |
||
| 636 | public function saveHistory() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Validates the object to be saved. |
||
| 656 | * |
||
| 657 | * @since 1.0 |
||
| 658 | * |
||
| 659 | * @throws \Alpha\Exception\ValidationException |
||
| 660 | */ |
||
| 661 | protected function validate() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Deletes the current object from the database. |
||
| 699 | * |
||
| 700 | * @since 1.0 |
||
| 701 | * |
||
| 702 | * @throws \Alpha\Exception\FailedDeleteException |
||
| 703 | */ |
||
| 704 | public function delete() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Delete all object instances from the database by the specified attribute matching the value provided. |
||
| 790 | * |
||
| 791 | * @param string $attribute The name of the field to delete the objects by. |
||
| 792 | * @param mixed $value The value of the field to delete the objects by. |
||
| 793 | * |
||
| 794 | * @return int The number of rows deleted. |
||
| 795 | * |
||
| 796 | * @since 1.0 |
||
| 797 | * |
||
| 798 | * @throws \Alpha\Exception\FailedDeleteException |
||
| 799 | */ |
||
| 800 | public function deleteAllByAttribute($attribute, $value) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Gets the version_num of the object from the database (returns 0 if the BO is not saved yet). |
||
| 837 | * |
||
| 838 | * @return int |
||
| 839 | * |
||
| 840 | * @since 1.0 |
||
| 841 | * |
||
| 842 | * @throws \Alpha\Exception\RecordNotFoundException |
||
| 843 | */ |
||
| 844 | public function getVersion() |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Builds a new database table for the BO class. |
||
| 868 | * |
||
| 869 | * @since 1.0 |
||
| 870 | * |
||
| 871 | * @throws \Alpha\Exception\AlphaException |
||
| 872 | */ |
||
| 873 | public function makeTable() |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Builds a new database table for the BO class to story it's history of changes. |
||
| 897 | * |
||
| 898 | * @since 1.2 |
||
| 899 | * |
||
| 900 | * @throws \Alpha\Exception\AlphaException |
||
| 901 | */ |
||
| 902 | public function makeHistoryTable() |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Re-builds the table if the model requirements have changed. All data is lost! |
||
| 926 | * |
||
| 927 | * @since 1.0 |
||
| 928 | * |
||
| 929 | * @throws \Alpha\Exception\AlphaException |
||
| 930 | */ |
||
| 931 | public function rebuildTable() |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Drops the table if the model requirements have changed. All data is lost! |
||
| 953 | * |
||
| 954 | * @since 1.0 |
||
| 955 | * |
||
| 956 | * @param string $tableName Optional table name, leave blank for the defined table for this class to be dropped |
||
| 957 | * |
||
| 958 | * @throws \Alpha\Exception\AlphaException |
||
| 959 | */ |
||
| 960 | public function dropTable($tableName = null) |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Adds in a new class property without loosing existing data (does an ALTER TABLE query on the |
||
| 982 | * database). |
||
| 983 | * |
||
| 984 | * @param string $propName The name of the new field to add to the database table. |
||
| 985 | * |
||
| 986 | * @since 1.0 |
||
| 987 | * |
||
| 988 | * @throws \Alpha\Exception\AlphaException |
||
| 989 | */ |
||
| 990 | public function addProperty($propName) |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Populates the current business object from the provided hash array. |
||
| 1012 | * |
||
| 1013 | * @param array $hashArray |
||
| 1014 | * |
||
| 1015 | * @since 1.2.1 |
||
| 1016 | */ |
||
| 1017 | public function populateFromArray($hashArray) |
||
| 1053 | |||
| 1054 | /** |
||
| 1055 | * Gets the maximum OID value from the database for this class type. |
||
| 1056 | * |
||
| 1057 | * @return int The maximum OID value in the class table. |
||
| 1058 | * |
||
| 1059 | * @since 1.0 |
||
| 1060 | * |
||
| 1061 | * @throws \Alpha\Exception\AlphaException |
||
| 1062 | */ |
||
| 1063 | public function getMAX() |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Gets the count from the database for the amount of objects of this class. |
||
| 1087 | * |
||
| 1088 | * @param array $attributes The attributes to count the objects by (optional). |
||
| 1089 | * @param array $values The values of the attributes to count the objects by (optional). |
||
| 1090 | * |
||
| 1091 | * @return int |
||
| 1092 | * |
||
| 1093 | * @since 1.0 |
||
| 1094 | * |
||
| 1095 | * @throws \Alpha\Exception\AlphaException |
||
| 1096 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1097 | */ |
||
| 1098 | public function getCount($attributes = array(), $values = array()) |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Gets the count from the database for the amount of entries in the [tableName]_history table for this business object. Only call |
||
| 1126 | * this method on classes where maintainHistory = true, otherwise an exception will be thrown. |
||
| 1127 | * |
||
| 1128 | * @return int |
||
| 1129 | * |
||
| 1130 | * @since 1.2 |
||
| 1131 | * |
||
| 1132 | * @throws \Alpha\Exception\AlphaException |
||
| 1133 | */ |
||
| 1134 | public function getHistoryCount() |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Gets the OID for the object in zero-padded format (same as getOID()). This version is designed |
||
| 1158 | * to be overridden in case you want to do something different with the ID of your objects outside |
||
| 1159 | * of the standard 11 digit OID sequence used internally in Alpha. |
||
| 1160 | * |
||
| 1161 | * @return int 11 digit zero-padded OID value. |
||
| 1162 | * |
||
| 1163 | * @since 1.0 |
||
| 1164 | */ |
||
| 1165 | public function getID() |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Gets the OID for the object in zero-padded format (same as getID()). This version is final so cannot |
||
| 1176 | * be overridden. |
||
| 1177 | * |
||
| 1178 | * @return int 11 digit zero-padded OID value. |
||
| 1179 | * |
||
| 1180 | * @since 1.0 |
||
| 1181 | */ |
||
| 1182 | final public function getOID() |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Method for getting version number of the object. |
||
| 1196 | * |
||
| 1197 | * @return \Alpha\Model\Type\Integer The object version number. |
||
| 1198 | * |
||
| 1199 | * @since 1.0 |
||
| 1200 | */ |
||
| 1201 | public function getVersionNumber() |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Populate all of the enum options for this object from the database. |
||
| 1211 | * |
||
| 1212 | * @since 1.0 |
||
| 1213 | * |
||
| 1214 | * @throws \Alpha\Exception\AlphaException |
||
| 1215 | */ |
||
| 1216 | protected function setEnumOptions() |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Generic getter method for accessing class properties. Will use the method get.ucfirst($prop) instead if that |
||
| 1238 | * method exists at a child level (by default). Set $noChildMethods to true if you don't want to use any |
||
| 1239 | * get.ucfirst($prop) method even if it exists, false otherwise (default). |
||
| 1240 | * |
||
| 1241 | * @param string $prop The name of the object property to get. |
||
| 1242 | * @param bool $noChildMethods Set to true if you do not want to use getters in the child object, defaults to false. |
||
| 1243 | * |
||
| 1244 | * @return mixed The property value. |
||
| 1245 | * |
||
| 1246 | * @since 1.0 |
||
| 1247 | * |
||
| 1248 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1249 | * @throws \Alpha\Exception\AlphaException |
||
| 1250 | */ |
||
| 1251 | public function get($prop, $noChildMethods = false) |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Generic setter method for setting class properties. Will use the method set.ucfirst($prop) instead if that |
||
| 1307 | * method exists at a child level (by default). Set $noChildMethods to true if you don't want to use |
||
| 1308 | * any get.ucfirst($prop) method even if it exists, false otherwise (default). |
||
| 1309 | * |
||
| 1310 | * @param string $prop The name of the property to set. |
||
| 1311 | * @param mixed $value The value of the property to set. |
||
| 1312 | * @param bool $noChildMethods Set to true if you do not want to use setters in the child object, defaults to false. |
||
| 1313 | * |
||
| 1314 | * @since 1.0 |
||
| 1315 | * |
||
| 1316 | * @throws \Alpha\Exception\AlphaException |
||
| 1317 | */ |
||
| 1318 | public function set($prop, $value, $noChildMethods = false) |
||
| 1360 | |||
| 1361 | /** |
||
| 1362 | * Gets the property object rather than the value for complex attributes. Returns false if |
||
| 1363 | * the property exists but is private. |
||
| 1364 | * |
||
| 1365 | * @param string $prop The name of the property we are getting. |
||
| 1366 | * |
||
| 1367 | * @return \Alpha\Model\Type\TypeInterface|bool The complex type object found. |
||
| 1368 | * |
||
| 1369 | * @since 1.0 |
||
| 1370 | * |
||
| 1371 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1372 | */ |
||
| 1373 | public function getPropObject($prop) |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Checks to see if the table exists in the database for the current business class. |
||
| 1418 | * |
||
| 1419 | * @param bool $checkHistoryTable Set to true if you want to check for the existance of the _history table for this DAO. |
||
| 1420 | * |
||
| 1421 | * @return bool |
||
| 1422 | * |
||
| 1423 | * @since 1.0 |
||
| 1424 | * |
||
| 1425 | * @throws \Alpha\Exception\AlphaException |
||
| 1426 | */ |
||
| 1427 | public function checkTableExists($checkHistoryTable = false) |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Static method to check the database and see if the table for the indicated BO class name |
||
| 1451 | * exists (assumes table name will be $BOClassName less "Object"). |
||
| 1452 | * |
||
| 1453 | * @param string $BOClassName The name of the business object class we are checking. |
||
| 1454 | * @param bool $checkHistoryTable Set to true if you want to check for the existance of the _history table for this DAO. |
||
| 1455 | * |
||
| 1456 | * @return bool |
||
| 1457 | * |
||
| 1458 | * @since 1.0 |
||
| 1459 | * |
||
| 1460 | * @throws \Alpha\Exception\AlphaException |
||
| 1461 | */ |
||
| 1462 | public static function checkBOTableExists($BOClassName, $checkHistoryTable = false) |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Checks to see if the table in the database matches (for fields) the business class definition, i.e. if the |
||
| 1482 | * database table is in sync with the class definition. |
||
| 1483 | * |
||
| 1484 | * @return bool |
||
| 1485 | * |
||
| 1486 | * @since 1.0 |
||
| 1487 | * |
||
| 1488 | * @throws \Alpha\Exception\AlphaException |
||
| 1489 | */ |
||
| 1490 | public function checkTableNeedsUpdate() |
||
| 1519 | |||
| 1520 | /** |
||
| 1521 | * Returns an array containing any properties on the class which have not been created on the database |
||
| 1522 | * table yet. |
||
| 1523 | * |
||
| 1524 | * @return array An array of missing fields in the database table. |
||
| 1525 | * |
||
| 1526 | * @since 1.0 |
||
| 1527 | * |
||
| 1528 | * @throws \Alpha\Exception\AlphaException |
||
| 1529 | */ |
||
| 1530 | public function findMissingFields() |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Getter for the TABLE_NAME, which should be set by a child of this class. |
||
| 1554 | * |
||
| 1555 | * @return string The table name in the database. |
||
| 1556 | * |
||
| 1557 | * @since 1.0 |
||
| 1558 | * |
||
| 1559 | * @throws \Alpha\Exception\AlphaException |
||
| 1560 | */ |
||
| 1561 | public function getTableName() |
||
| 1577 | |||
| 1578 | /** |
||
| 1579 | * Method for getting the OID of the person who created this BO. |
||
| 1580 | * |
||
| 1581 | * @return \Alpha\Model\Type\Integer The OID of the creator. |
||
| 1582 | * |
||
| 1583 | * @since 1.0 |
||
| 1584 | */ |
||
| 1585 | public function getCreatorId() |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Method for getting the OID of the person who updated this BO. |
||
| 1595 | * |
||
| 1596 | * @return \Alpha\Model\Type\Integer The OID of the updator. |
||
| 1597 | * |
||
| 1598 | * @since 1.0 |
||
| 1599 | */ |
||
| 1600 | public function getUpdatorId() |
||
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Method for getting the date/time of when the BO was created. |
||
| 1610 | * |
||
| 1611 | * @return \Alpha\Model\Type\Timestamp |
||
| 1612 | * |
||
| 1613 | * @since 1.0 |
||
| 1614 | */ |
||
| 1615 | public function getCreateTS() |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Method for getting the date/time of when the BO was last updated. |
||
| 1625 | * |
||
| 1626 | * @return \Alpha\Model\Type\Timestamp |
||
| 1627 | * |
||
| 1628 | * @since 1.0 |
||
| 1629 | */ |
||
| 1630 | public function getUpdateTS() |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Adds the name of the attribute provided to the list of transient (non-saved) attributes for this BO. |
||
| 1640 | * |
||
| 1641 | * @param string $attributeName The name of the attribute to not save. |
||
| 1642 | * |
||
| 1643 | * @since 1.0 |
||
| 1644 | */ |
||
| 1645 | public function markTransient($attributeName) |
||
| 1651 | |||
| 1652 | /** |
||
| 1653 | * Removes the name of the attribute provided from the list of transient (non-saved) attributes for this BO, |
||
| 1654 | * ensuring that it will be saved on the next attempt. |
||
| 1655 | * |
||
| 1656 | * @param string $attributeName The name of the attribute to save. |
||
| 1657 | * |
||
| 1658 | * @since 1.0 |
||
| 1659 | */ |
||
| 1660 | public function markPersistent($attributeName) |
||
| 1666 | |||
| 1667 | /** |
||
| 1668 | * Adds the name of the attribute(s) provided to the list of unique (constrained) attributes for this BO. |
||
| 1669 | * |
||
| 1670 | * @param string $attribute1Name The first attribute to mark unique in the database. |
||
| 1671 | * @param string $attribute2Name The second attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1672 | * @param string $attribute3Name The third attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1673 | * |
||
| 1674 | * @since 1.0 |
||
| 1675 | */ |
||
| 1676 | protected function markUnique($attribute1Name, $attribute2Name = '', $attribute3Name = '') |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Returns the array of names of unique attributes on this BO. |
||
| 1698 | * |
||
| 1699 | * @return array |
||
| 1700 | * |
||
| 1701 | * @since 1.1 |
||
| 1702 | */ |
||
| 1703 | public function getUniqueAttributes() |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Gets an array of all of the names of the active database indexes for this class. |
||
| 1713 | * |
||
| 1714 | * @return array An array of database indexes on this table. |
||
| 1715 | * |
||
| 1716 | * @since 1.0 |
||
| 1717 | * |
||
| 1718 | * @throws \Alpha\Exception\AlphaException |
||
| 1719 | */ |
||
| 1720 | public function getIndexes() |
||
| 1733 | |||
| 1734 | /** |
||
| 1735 | * Creates a foreign key constraint (index) in the database on the given attribute. |
||
| 1736 | * |
||
| 1737 | * @param string $attributeName The name of the attribute to apply the index on. |
||
| 1738 | * @param string $relatedClass The name of the related class in the format "NameObject". |
||
| 1739 | * @param string $relatedClassAttribute The name of the field to relate to on the related class. |
||
| 1740 | * @param string $indexName The optional name for the index, will calculate if not provided. |
||
| 1741 | * |
||
| 1742 | * @since 1.0 |
||
| 1743 | * |
||
| 1744 | * @throws \Alpha\Exception\FailedIndexCreateException |
||
| 1745 | */ |
||
| 1746 | public function createForeignIndex($attributeName, $relatedClass, $relatedClassAttribute, $indexName = null) |
||
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Creates a unique index in the database on the given attribute(s). |
||
| 1778 | * |
||
| 1779 | * @param string $attribute1Name The first attribute to mark unique in the database. |
||
| 1780 | * @param string $attribute2Name The second attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1781 | * @param string $attribute3Name The third attribute to mark unique in the databse (optional, use only for composite keys). |
||
| 1782 | * |
||
| 1783 | * @since 1.0 |
||
| 1784 | * |
||
| 1785 | * @throws \Alpha\Exception\FailedIndexCreateException |
||
| 1786 | */ |
||
| 1787 | public function createUniqueIndex($attribute1Name, $attribute2Name = '', $attribute3Name = '') |
||
| 1806 | |||
| 1807 | /** |
||
| 1808 | * Gets the data labels array. |
||
| 1809 | * |
||
| 1810 | * @return array An array of attribute labels. |
||
| 1811 | * |
||
| 1812 | * @since 1.0 |
||
| 1813 | */ |
||
| 1814 | public function getDataLabels() |
||
| 1821 | |||
| 1822 | /** |
||
| 1823 | * Sets the data labels array. |
||
| 1824 | * |
||
| 1825 | * @param array $labels |
||
| 1826 | * |
||
| 1827 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1828 | * |
||
| 1829 | * @since 1.2 |
||
| 1830 | */ |
||
| 1831 | public function setDataLabels($labels) |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Gets the data label for the given attribute name. |
||
| 1846 | * |
||
| 1847 | * @param $att The attribute name to get the label for. |
||
| 1848 | * |
||
| 1849 | * @return string |
||
| 1850 | * |
||
| 1851 | * @since 1.0 |
||
| 1852 | * |
||
| 1853 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1854 | */ |
||
| 1855 | public function getDataLabel($att) |
||
| 1868 | |||
| 1869 | /** |
||
| 1870 | * Loops over the core and custom BO directories and builds an array of all of the BO class names in the system. |
||
| 1871 | * |
||
| 1872 | * @return array An array of business object class names. |
||
| 1873 | * |
||
| 1874 | * @since 1.0 |
||
| 1875 | */ |
||
| 1876 | public static function getBOClassNames() |
||
| 1926 | |||
| 1927 | /** |
||
| 1928 | * Get the array of default attribute names. |
||
| 1929 | * |
||
| 1930 | * @return array An array of attribute names. |
||
| 1931 | * |
||
| 1932 | * @since 1.0 |
||
| 1933 | */ |
||
| 1934 | public function getDefaultAttributes() |
||
| 1941 | |||
| 1942 | /** |
||
| 1943 | * Get the array of transient attribute names. |
||
| 1944 | * |
||
| 1945 | * @return array An array of attribute names. |
||
| 1946 | * |
||
| 1947 | * @since 1.0 |
||
| 1948 | */ |
||
| 1949 | public function getTransientAttributes() |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * Get the array of persistent attribute names, i.e. those that are saved in the database. |
||
| 1959 | * |
||
| 1960 | * @return array An array of attribute names. |
||
| 1961 | * |
||
| 1962 | * @since 1.0 |
||
| 1963 | */ |
||
| 1964 | public function getPersistentAttributes() |
||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * Setter for the Object ID (OID). |
||
| 1990 | * |
||
| 1991 | * @param int $OID The Object ID. |
||
| 1992 | * |
||
| 1993 | * @since 1.0 |
||
| 1994 | */ |
||
| 1995 | public function setOID($OID) |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Inspector to see if the business object is transient (not presently stored in the database). |
||
| 2004 | * |
||
| 2005 | * @return bool |
||
| 2006 | * |
||
| 2007 | * @since 1.0 |
||
| 2008 | */ |
||
| 2009 | public function isTransient() |
||
| 2023 | |||
| 2024 | /** |
||
| 2025 | * Get the last database query run on this object. |
||
| 2026 | * |
||
| 2027 | * @return string An SQL query string. |
||
| 2028 | * |
||
| 2029 | * @since 1.0 |
||
| 2030 | */ |
||
| 2031 | public function getLastQuery() |
||
| 2038 | |||
| 2039 | /** |
||
| 2040 | * Unsets all of the attributes of this object to null. |
||
| 2041 | * |
||
| 2042 | * @since 1.0 |
||
| 2043 | */ |
||
| 2044 | private function clear() |
||
| 2061 | |||
| 2062 | /** |
||
| 2063 | * Reloads the object from the database, overwritting any attribute values in memory. |
||
| 2064 | * |
||
| 2065 | * @since 1.0 |
||
| 2066 | * |
||
| 2067 | * @throws \Alpha\Exception\AlphaException |
||
| 2068 | */ |
||
| 2069 | public function reload() |
||
| 2080 | |||
| 2081 | /** |
||
| 2082 | * Loads the definition from the file system for the BO class name provided. |
||
| 2083 | * |
||
| 2084 | * @param string $classname The name of the business object class name. |
||
| 2085 | * |
||
| 2086 | * @since 1.0 |
||
| 2087 | * |
||
| 2088 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 2089 | * |
||
| 2090 | * @deprecated Use autoloader! |
||
| 2091 | */ |
||
| 2092 | public static function loadClassDef($classname) |
||
| 2113 | |||
| 2114 | /** |
||
| 2115 | * Checks that a record exists for the BO in the database. |
||
| 2116 | * |
||
| 2117 | * @param int $OID The Object ID of the object we want to see whether it exists or not. |
||
| 2118 | * |
||
| 2119 | * @return bool |
||
| 2120 | * |
||
| 2121 | * @since 1.0 |
||
| 2122 | * |
||
| 2123 | * @throws \Alpha\Exception\AlphaException |
||
| 2124 | */ |
||
| 2125 | public function checkRecordExists($OID) |
||
| 2146 | |||
| 2147 | /** |
||
| 2148 | * Checks to see if the table name matches the classname, and if not if the table |
||
| 2149 | * name matches the classname name of another BO, i.e. the table is used to store |
||
| 2150 | * multiple types of BOs. |
||
| 2151 | * |
||
| 2152 | * @return bool |
||
| 2153 | * |
||
| 2154 | * @since 1.0 |
||
| 2155 | * |
||
| 2156 | * @throws \Alpha\Exception\BadBOTableNameException |
||
| 2157 | */ |
||
| 2158 | public function isTableOverloaded() |
||
| 2171 | |||
| 2172 | /** |
||
| 2173 | * Starts a new database transaction. |
||
| 2174 | * |
||
| 2175 | * @param $BO The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2176 | * |
||
| 2177 | * @since 1.0 |
||
| 2178 | * |
||
| 2179 | * @throws \Alpha\Exception\AlphaException |
||
| 2180 | */ |
||
| 2181 | public static function begin($BO = null) |
||
| 2204 | |||
| 2205 | /** |
||
| 2206 | * Commits the current database transaction. |
||
| 2207 | * |
||
| 2208 | * @param $BO The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2209 | * |
||
| 2210 | * @since 1.0 |
||
| 2211 | * |
||
| 2212 | * @throws \Alpha\Exception\FailedSaveException |
||
| 2213 | */ |
||
| 2214 | public static function commit($BO = null) |
||
| 2237 | |||
| 2238 | /** |
||
| 2239 | * Aborts the current database transaction. |
||
| 2240 | * |
||
| 2241 | * @param $BO The ActiveRecord instance to pass to the database provider. Leave empty to have a new Person passed. |
||
| 2242 | * |
||
| 2243 | * @since 1.0 |
||
| 2244 | * |
||
| 2245 | * @throws \Alpha\Exception\AlphaException |
||
| 2246 | */ |
||
| 2247 | public static function rollback($BO = null) |
||
| 2270 | |||
| 2271 | /** |
||
| 2272 | * Static method that tries to determine if the system database has been installed or not. |
||
| 2273 | * |
||
| 2274 | * @return bool |
||
| 2275 | * |
||
| 2276 | * @since 1.0 |
||
| 2277 | */ |
||
| 2278 | public static function isInstalled() |
||
| 2303 | |||
| 2304 | /** |
||
| 2305 | * Returns true if the BO has a Relation property called tags, false otherwise. |
||
| 2306 | * |
||
| 2307 | * @return bool |
||
| 2308 | * |
||
| 2309 | * @since 1.0 |
||
| 2310 | */ |
||
| 2311 | public function isTagged() |
||
| 2319 | |||
| 2320 | /** |
||
| 2321 | * Returns the contents of the taggedAttributes array, or an empty array if that does not exist. |
||
| 2322 | * |
||
| 2323 | * @return array |
||
| 2324 | * |
||
| 2325 | * @since 1.2.3 |
||
| 2326 | */ |
||
| 2327 | public function getTaggedAttributes() |
||
| 2335 | |||
| 2336 | /** |
||
| 2337 | * Setter for the BO version number. |
||
| 2338 | * |
||
| 2339 | * @param int $versionNumber The version number. |
||
| 2340 | * |
||
| 2341 | * @since 1.0 |
||
| 2342 | */ |
||
| 2343 | private function setVersion($versionNumber) |
||
| 2347 | |||
| 2348 | /** |
||
| 2349 | * Cast a BO to another type of BO. A new BO will be returned with the same OID and |
||
| 2350 | * version_num as the old BO, so this is NOT a true cast but is a copy. All attribute |
||
| 2351 | * values will be copied accross. |
||
| 2352 | * |
||
| 2353 | * @param string $targetClassName The fully-qualified name of the target BO class. |
||
| 2354 | * @param \Alpha\Model\ActiveRecord $originalBO The original business object. |
||
| 2355 | * |
||
| 2356 | * @return \Alpha\Model\ActiveRecord The new business object resulting from the cast. |
||
| 2357 | * |
||
| 2358 | * @since 1.0 |
||
| 2359 | */ |
||
| 2360 | public function cast($targetClassName, $originalBO) |
||
| 2394 | |||
| 2395 | /** |
||
| 2396 | * Returns the simple class name, stripped of the namespace. |
||
| 2397 | * |
||
| 2398 | * @return string |
||
| 2399 | * |
||
| 2400 | * @since 1.0 |
||
| 2401 | */ |
||
| 2402 | public function getFriendlyClassName() |
||
| 2408 | |||
| 2409 | /** |
||
| 2410 | * Check to see if an attribute exists on the BO. |
||
| 2411 | * |
||
| 2412 | * @param $attribute The attribute name. |
||
| 2413 | * |
||
| 2414 | * @return bool |
||
| 2415 | * |
||
| 2416 | * @since 1.0 |
||
| 2417 | */ |
||
| 2418 | public function hasAttribute($attribute) |
||
| 2428 | |||
| 2429 | /** |
||
| 2430 | * Stores the business object to the configured cache instance. |
||
| 2431 | * |
||
| 2432 | * @since 1.1 |
||
| 2433 | */ |
||
| 2434 | public function addToCache() |
||
| 2449 | |||
| 2450 | /** |
||
| 2451 | * Removes the business object from the configured cache instance. |
||
| 2452 | * |
||
| 2453 | * @since 1.1 |
||
| 2454 | */ |
||
| 2455 | public function removeFromCache() |
||
| 2470 | |||
| 2471 | /** |
||
| 2472 | * Attempts to load the business object from the configured cache instance. |
||
| 2473 | * |
||
| 2474 | * @since 1.1 |
||
| 2475 | * |
||
| 2476 | * @return bool |
||
| 2477 | */ |
||
| 2478 | public function loadFromCache() |
||
| 2526 | |||
| 2527 | /** |
||
| 2528 | * Sets the last query executed on this business object. |
||
| 2529 | * |
||
| 2530 | * @param string $query |
||
| 2531 | * |
||
| 2532 | * @since 1.1 |
||
| 2533 | */ |
||
| 2534 | public function setLastQuery($query) |
||
| 2539 | |||
| 2540 | /** |
||
| 2541 | * Re-initialize the static logger property on the BO after de-serialize, as PHP does |
||
| 2542 | * not serialize static properties. |
||
| 2543 | * |
||
| 2544 | * @since 1.2 |
||
| 2545 | */ |
||
| 2546 | public function __wakeup() |
||
| 2552 | |||
| 2553 | /** |
||
| 2554 | * Sets maintainHistory attribute on this DAO. |
||
| 2555 | * |
||
| 2556 | * @param bool $maintainHistory |
||
| 2557 | * |
||
| 2558 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 2559 | * |
||
| 2560 | * @since 1.2 |
||
| 2561 | */ |
||
| 2562 | public function setMaintainHistory($maintainHistory) |
||
| 2570 | |||
| 2571 | /** |
||
| 2572 | * Gets the value of the maintainHistory attribute. |
||
| 2573 | * |
||
| 2574 | * @return bool |
||
| 2575 | * |
||
| 2576 | * @since 1.2 |
||
| 2577 | */ |
||
| 2578 | public function getMaintainHistory() |
||
| 2582 | |||
| 2583 | /** |
||
| 2584 | * Return a hash array of the object containing attribute names and simplfied values. |
||
| 2585 | * |
||
| 2586 | * @return array |
||
| 2587 | * |
||
| 2588 | * @since 1.2.4 |
||
| 2589 | */ |
||
| 2590 | public function toArray() |
||
| 2614 | |||
| 2615 | /** |
||
| 2616 | * Check to see if the configured database exists. |
||
| 2617 | * |
||
| 2618 | * @return bool |
||
| 2619 | * |
||
| 2620 | * @since 2.0 |
||
| 2621 | */ |
||
| 2622 | public static function checkDatabaseExists() |
||
| 2630 | |||
| 2631 | /** |
||
| 2632 | * Creates the configured database. |
||
| 2633 | * |
||
| 2634 | * @throws \Alpha\Exception\AlphaException |
||
| 2635 | * |
||
| 2636 | * @since 2.0 |
||
| 2637 | */ |
||
| 2638 | public static function createDatabase() |
||
| 2645 | |||
| 2646 | /** |
||
| 2647 | * Drops the configured database. |
||
| 2648 | * |
||
| 2649 | * @throws \Alpha\Exception\AlphaException |
||
| 2650 | * |
||
| 2651 | * @since 2.0 |
||
| 2652 | */ |
||
| 2653 | public static function dropDatabase() |
||
| 2660 | } |
||
| 2661 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.