Complex classes like DataObject 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 DataObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 103 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider, Resettable |
||
| 104 | { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Human-readable singular name. |
||
| 108 | * @var string |
||
| 109 | * @config |
||
| 110 | */ |
||
| 111 | private static $singular_name = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Human-readable plural name |
||
| 115 | * @var string |
||
| 116 | * @config |
||
| 117 | */ |
||
| 118 | private static $plural_name = null; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Allow API access to this object? |
||
| 122 | * @todo Define the options that can be set here |
||
| 123 | * @config |
||
| 124 | */ |
||
| 125 | private static $api_access = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Allows specification of a default value for the ClassName field. |
||
| 129 | * Configure this value only in subclasses of DataObject. |
||
| 130 | * |
||
| 131 | * @config |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | private static $default_classname = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * True if this DataObject has been destroyed. |
||
| 138 | * @var boolean |
||
| 139 | */ |
||
| 140 | public $destroyed = false; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The DataModel from this this object comes |
||
| 144 | */ |
||
| 145 | protected $model; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 149 | * |
||
| 150 | * Use {@link toMap()} if you want an array representation |
||
| 151 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | protected $record; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * If selected through a many_many through relation, this is the instance of the through record |
||
| 159 | * |
||
| 160 | * @var DataObject |
||
| 161 | */ |
||
| 162 | protected $joinRecord; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 166 | */ |
||
| 167 | const CHANGE_NONE = 0; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 171 | * (before !== after && before == after) |
||
| 172 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 173 | * Value changes are by nature also considered strict changes. |
||
| 174 | */ |
||
| 175 | const CHANGE_STRICT = 1; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Represents a field that has changed the loosely defined value |
||
| 179 | * (before != after, thus, before !== after)) |
||
| 180 | * E.g. change false to true, but not false to 0 |
||
| 181 | */ |
||
| 182 | const CHANGE_VALUE = 2; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * An array indexed by fieldname, true if the field has been changed. |
||
| 186 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 187 | * the changed state. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $changed; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The database record (in the same format as $record), before |
||
| 195 | * any changes. |
||
| 196 | * @var array |
||
| 197 | */ |
||
| 198 | protected $original; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 202 | * @var boolean |
||
| 203 | */ |
||
| 204 | protected $brokenOnDelete = false; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 208 | * @var boolean |
||
| 209 | */ |
||
| 210 | protected $brokenOnWrite = false; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @config |
||
| 214 | * @var boolean Should dataobjects be validated before they are written? |
||
| 215 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 216 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 217 | * to only disable validation for very specific use cases. |
||
| 218 | */ |
||
| 219 | private static $validation_enabled = true; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Static caches used by relevant functions. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | protected static $_cache_get_one; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Cache of field labels |
||
| 230 | * |
||
| 231 | * @var array |
||
| 232 | */ |
||
| 233 | protected static $_cache_field_labels = array(); |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Base fields which are not defined in static $db |
||
| 237 | * |
||
| 238 | * @config |
||
| 239 | * @var array |
||
| 240 | */ |
||
| 241 | private static $fixed_fields = array( |
||
| 242 | 'ID' => 'PrimaryKey', |
||
| 243 | 'ClassName' => 'DBClassName', |
||
| 244 | 'LastEdited' => 'DBDatetime', |
||
| 245 | 'Created' => 'DBDatetime', |
||
| 246 | ); |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Override table name for this class. If ignored will default to FQN of class. |
||
| 250 | * This option is not inheritable, and must be set on each class. |
||
| 251 | * If left blank naming will default to the legacy (3.x) behaviour. |
||
| 252 | * |
||
| 253 | * @var string |
||
| 254 | */ |
||
| 255 | private static $table_name = null; |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Non-static relationship cache, indexed by component name. |
||
| 259 | * |
||
| 260 | * @var DataObject[] |
||
| 261 | */ |
||
| 262 | protected $components; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 266 | * |
||
| 267 | * @var UnsavedRelationList[] |
||
| 268 | */ |
||
| 269 | protected $unsavedRelations; |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get schema object |
||
| 273 | * |
||
| 274 | * @return DataObjectSchema |
||
| 275 | */ |
||
| 276 | public static function getSchema() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Construct a new DataObject. |
||
| 283 | * |
||
| 284 | |||
| 285 | * @param array|null $record Used internally for rehydrating an object from database content. |
||
| 286 | * Bypasses setters on this class, and hence should not be used |
||
| 287 | * for populating data on new records. |
||
| 288 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 289 | * Singletons don't have their defaults set. |
||
| 290 | * @param DataModel $model |
||
| 291 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 292 | */ |
||
| 293 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Set the DataModel |
||
| 381 | * @param DataModel $model |
||
| 382 | * @return DataObject $this |
||
| 383 | */ |
||
| 384 | public function setDataModel(DataModel $model) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Destroy all of this objects dependant objects and local caches. |
||
| 392 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 393 | */ |
||
| 394 | public function destroy() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Create a duplicate of this node. Can duplicate many_many relations |
||
| 403 | * |
||
| 404 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 405 | * If this is true, it will create the duplicate in the database. |
||
| 406 | * @param bool|string $manyMany Which many-many to duplicate. Set to true to duplicate all, false to duplicate none. |
||
| 407 | * Alternatively set to the string of the relation config to duplicate |
||
| 408 | * (supports 'many_many', or 'belongs_many_many') |
||
| 409 | * @return static A duplicate of this node. The exact type will be the type of this node. |
||
| 410 | */ |
||
| 411 | public function duplicate($doWrite = true, $manyMany = 'many_many') |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object. |
||
| 433 | * |
||
| 434 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 435 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 436 | * @param bool|string $filter |
||
| 437 | */ |
||
| 438 | protected function duplicateManyManyRelations($sourceObject, $destinationObject, $filter) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Duplicates a single many_many relation from one object to another |
||
| 455 | * |
||
| 456 | * @param DataObject $sourceObject |
||
| 457 | * @param DataObject $destinationObject |
||
| 458 | * @param string $manyManyName |
||
| 459 | */ |
||
| 460 | protected function duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Return obsolete class name, if this is no longer a valid class |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function getObsoleteClassName() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Gets name of this class |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getClassName() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 505 | * Warning: This will produce an inconsistent record, as the object |
||
| 506 | * instance will not automatically switch to the new subclass. |
||
| 507 | * Please use {@link newClassInstance()} for this purpose, |
||
| 508 | * or destroy and reinstanciate the record. |
||
| 509 | * |
||
| 510 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 511 | * @return $this |
||
| 512 | */ |
||
| 513 | public function setClassName($className) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Create a new instance of a different class from this object's record. |
||
| 528 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 529 | * it ensures that the instance of the class is a match for the className of the |
||
| 530 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 531 | * property manually before calling this method, as it will confuse change detection. |
||
| 532 | * |
||
| 533 | * If the new class is different to the original class, defaults are populated again |
||
| 534 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 535 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 536 | * we still need to repopulate the defaults. |
||
| 537 | * |
||
| 538 | * @param string $newClassName The name of the new class |
||
| 539 | * |
||
| 540 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 541 | */ |
||
| 542 | public function newClassInstance($newClassName) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Adds methods from the extensions. |
||
| 565 | * Called by Object::__construct() once per class. |
||
| 566 | */ |
||
| 567 | public function defineMethods() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 614 | * The default behaviour for a DataObject is to return true if |
||
| 615 | * the object exists in the database, you can override this in subclasses. |
||
| 616 | * |
||
| 617 | * @return boolean true if this object exists |
||
| 618 | */ |
||
| 619 | public function exists() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Returns TRUE if all values (other than "ID") are |
||
| 626 | * considered empty (by weak boolean comparison). |
||
| 627 | * |
||
| 628 | * @return boolean |
||
| 629 | */ |
||
| 630 | public function isEmpty() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Pluralise this item given a specific count. |
||
| 652 | * |
||
| 653 | * E.g. "0 Pages", "1 File", "3 Images" |
||
| 654 | * |
||
| 655 | * @param string $count |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | public function i18n_pluralise($count) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Get the user friendly singular name of this DataObject. |
||
| 670 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 671 | * this returns the class name. |
||
| 672 | * |
||
| 673 | * @return string User friendly singular name of this DataObject |
||
| 674 | */ |
||
| 675 | public function singular_name() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Get the translated user friendly singular name of this DataObject |
||
| 690 | * same as singular_name() but runs it through the translating function |
||
| 691 | * |
||
| 692 | * Translating string is in the form: |
||
| 693 | * $this->class.SINGULARNAME |
||
| 694 | * Example: |
||
| 695 | * Page.SINGULARNAME |
||
| 696 | * |
||
| 697 | * @return string User friendly translated singular name of this DataObject |
||
| 698 | */ |
||
| 699 | public function i18n_singular_name() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Get the user friendly plural name of this DataObject |
||
| 706 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 707 | * this returns a pluralised version of the class name. |
||
| 708 | * |
||
| 709 | * @return string User friendly plural name of this DataObject |
||
| 710 | */ |
||
| 711 | public function plural_name() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Get the translated user friendly plural name of this DataObject |
||
| 726 | * Same as plural_name but runs it through the translation function |
||
| 727 | * Translation string is in the form: |
||
| 728 | * $this->class.PLURALNAME |
||
| 729 | * Example: |
||
| 730 | * Page.PLURALNAME |
||
| 731 | * |
||
| 732 | * @return string User friendly translated plural name of this DataObject |
||
| 733 | */ |
||
| 734 | public function i18n_plural_name() |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Standard implementation of a title/label for a specific |
||
| 741 | * record. Tries to find properties 'Title' or 'Name', |
||
| 742 | * and falls back to the 'ID'. Useful to provide |
||
| 743 | * user-friendly identification of a record, e.g. in errormessages |
||
| 744 | * or UI-selections. |
||
| 745 | * |
||
| 746 | * Overload this method to have a more specialized implementation, |
||
| 747 | * e.g. for an Address record this could be: |
||
| 748 | * <code> |
||
| 749 | * function getTitle() { |
||
| 750 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 751 | * } |
||
| 752 | * </code> |
||
| 753 | * |
||
| 754 | * @return string |
||
| 755 | */ |
||
| 756 | public function getTitle() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Returns the associated database record - in this case, the object itself. |
||
| 771 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 772 | * |
||
| 773 | * @return DataObject Associated database record |
||
| 774 | */ |
||
| 775 | public function data() |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Convert this object to a map. |
||
| 782 | * |
||
| 783 | * @return array The data as a map. |
||
| 784 | */ |
||
| 785 | public function toMap() |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Return all currently fetched database fields. |
||
| 793 | * |
||
| 794 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 795 | * Obviously, this makes it a lot faster. |
||
| 796 | * |
||
| 797 | * @return array The data as a map. |
||
| 798 | */ |
||
| 799 | public function getQueriedDatabaseFields() |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 806 | * |
||
| 807 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 808 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 809 | * |
||
| 810 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 811 | * the related objects that it alters. |
||
| 812 | * |
||
| 813 | * @param array $data A map of field name to data values to update. |
||
| 814 | * @return DataObject $this |
||
| 815 | */ |
||
| 816 | public function update($data) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Pass changes as a map, and try to |
||
| 868 | * get automatic casting for these fields. |
||
| 869 | * Doesn't write to the database. To write the data, |
||
| 870 | * use the write() method. |
||
| 871 | * |
||
| 872 | * @param array $data A map of field name to data values to update. |
||
| 873 | * @return DataObject $this |
||
| 874 | */ |
||
| 875 | public function castedUpdate($data) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Merges data and relations from another object of same class, |
||
| 885 | * without conflict resolution. Allows to specify which |
||
| 886 | * dataset takes priority in case its not empty. |
||
| 887 | * has_one-relations are just transferred with priority 'right'. |
||
| 888 | * has_many and many_many-relations are added regardless of priority. |
||
| 889 | * |
||
| 890 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 891 | * meaning they are not connected to the merged object any longer. |
||
| 892 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 893 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 894 | * Caution: Does not delete the merged object. |
||
| 895 | * Caution: Does now overwrite Created date on the original object. |
||
| 896 | * |
||
| 897 | * @param DataObject $rightObj |
||
| 898 | * @param string $priority left|right Determines who wins in case of a conflict (optional) |
||
| 899 | * @param bool $includeRelations Merge any existing relations (optional) |
||
| 900 | * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values. |
||
| 901 | * Only applicable with $priority='right'. (optional) |
||
| 902 | * @return Boolean |
||
| 903 | */ |
||
| 904 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Forces the record to think that all its data has changed. |
||
| 978 | * Doesn't write to the database. Only sets fields as changed |
||
| 979 | * if they are not already marked as changed. |
||
| 980 | * |
||
| 981 | * @return $this |
||
| 982 | */ |
||
| 983 | public function forceChange() |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Validate the current object. |
||
| 1014 | * |
||
| 1015 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1016 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1017 | * |
||
| 1018 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1019 | * and onAfterWrite() won't get called either. |
||
| 1020 | * |
||
| 1021 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1022 | * attempting a write, and respond appropriately if it isn't. |
||
| 1023 | * |
||
| 1024 | * @see {@link ValidationResult} |
||
| 1025 | * @return ValidationResult |
||
| 1026 | */ |
||
| 1027 | public function validate() |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Public accessor for {@see DataObject::validate()} |
||
| 1036 | * |
||
| 1037 | * @return ValidationResult |
||
| 1038 | */ |
||
| 1039 | public function doValidate() |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Event handler called before writing to the database. |
||
| 1047 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1048 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1049 | * |
||
| 1050 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1051 | * |
||
| 1052 | * @uses DataExtension->onBeforeWrite() |
||
| 1053 | */ |
||
| 1054 | protected function onBeforeWrite() |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Event handler called after writing to the database. |
||
| 1064 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1065 | * $this->changed will have a record |
||
| 1066 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1067 | * |
||
| 1068 | * @uses DataExtension->onAfterWrite() |
||
| 1069 | */ |
||
| 1070 | protected function onAfterWrite() |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Event handler called before deleting from the database. |
||
| 1078 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1079 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1080 | * |
||
| 1081 | * @uses DataExtension->onBeforeDelete() |
||
| 1082 | */ |
||
| 1083 | protected function onBeforeDelete() |
||
| 1090 | |||
| 1091 | protected function onAfterDelete() |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Load the default values in from the self::$defaults array. |
||
| 1098 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1099 | * Called by the constructor when creating new records. |
||
| 1100 | * |
||
| 1101 | * @uses DataExtension->populateDefaults() |
||
| 1102 | * @return DataObject $this |
||
| 1103 | */ |
||
| 1104 | public function populateDefaults() |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Determine validation of this object prior to write |
||
| 1144 | * |
||
| 1145 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1146 | */ |
||
| 1147 | protected function validateWrite() |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Prepare an object prior to write |
||
| 1168 | * |
||
| 1169 | * @throws ValidationException |
||
| 1170 | */ |
||
| 1171 | protected function preWrite() |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Detects and updates all changes made to this object |
||
| 1191 | * |
||
| 1192 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1193 | * @return bool True if any changes are detected |
||
| 1194 | */ |
||
| 1195 | protected function updateChanges($forceChanges = false) |
||
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1209 | * |
||
| 1210 | * @param string $baseTable Base table |
||
| 1211 | * @param string $now Timestamp to use for the current time |
||
| 1212 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1213 | * @param array $manipulation Manipulation to write to |
||
| 1214 | * @param string $class Class of table to manipulate |
||
| 1215 | */ |
||
| 1216 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) |
||
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1267 | * |
||
| 1268 | * Does nothing if an ID is already assigned for this record |
||
| 1269 | * |
||
| 1270 | * @param string $baseTable Base table |
||
| 1271 | * @param string $now Timestamp to use for the current time |
||
| 1272 | */ |
||
| 1273 | protected function writeBaseRecord($baseTable, $now) |
||
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Generate and write the database manipulation for all changed fields |
||
| 1291 | * |
||
| 1292 | * @param string $baseTable Base table |
||
| 1293 | * @param string $now Timestamp to use for the current time |
||
| 1294 | * @param bool $isNewRecord If this is a new record |
||
| 1295 | */ |
||
| 1296 | protected function writeManipulation($baseTable, $now, $isNewRecord) |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Writes all changes to this object to the database. |
||
| 1319 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1320 | * - All relevant tables will be updated. |
||
| 1321 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1322 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1323 | * |
||
| 1324 | * @uses DataExtension->augmentWrite() |
||
| 1325 | * |
||
| 1326 | * @param boolean $showDebug Show debugging information |
||
| 1327 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1328 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1329 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1330 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1331 | * {@link getManyManyComponents()} (Default: false) |
||
| 1332 | * @return int The ID of the record |
||
| 1333 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1334 | */ |
||
| 1335 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Writes cached relation lists to the database, if possible |
||
| 1388 | */ |
||
| 1389 | public function writeRelations() |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1406 | * same record. |
||
| 1407 | * |
||
| 1408 | * @param bool $recursive Recursively write components |
||
| 1409 | * @return DataObject $this |
||
| 1410 | */ |
||
| 1411 | public function writeComponents($recursive = false) |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * Delete this data object. |
||
| 1428 | * $this->onBeforeDelete() gets called. |
||
| 1429 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1430 | * @uses DataExtension->augmentSQL() |
||
| 1431 | */ |
||
| 1432 | public function delete() |
||
| 1463 | |||
| 1464 | /** |
||
| 1465 | * Delete the record with the given ID. |
||
| 1466 | * |
||
| 1467 | * @param string $className The class name of the record to be deleted |
||
| 1468 | * @param int $id ID of record to be deleted |
||
| 1469 | */ |
||
| 1470 | public static function delete_by_id($className, $id) |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Get the class ancestry, including the current class name. |
||
| 1482 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1483 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1484 | * will be the current class. |
||
| 1485 | * |
||
| 1486 | * @return array Class ancestry |
||
| 1487 | */ |
||
| 1488 | public function getClassAncestry() |
||
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1495 | * If no component is available, an 'empty component' will be returned for |
||
| 1496 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1497 | * |
||
| 1498 | * @param string $componentName Name of the component |
||
| 1499 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1500 | * @throws Exception |
||
| 1501 | */ |
||
| 1502 | public function getComponent($componentName) |
||
| 1574 | |||
| 1575 | /** |
||
| 1576 | * Returns a one-to-many relation as a HasManyList |
||
| 1577 | * |
||
| 1578 | * @param string $componentName Name of the component |
||
| 1579 | * @return HasManyList|UnsavedRelationList The components of the one-to-many relationship. |
||
| 1580 | */ |
||
| 1581 | public function getComponents($componentName) |
||
| 1621 | |||
| 1622 | /** |
||
| 1623 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1624 | * |
||
| 1625 | * @param string $relationName Relation name. |
||
| 1626 | * @return string Class name, or null if not found. |
||
| 1627 | */ |
||
| 1628 | public function getRelationClass($relationName) |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Given a relation name, determine the relation type |
||
| 1665 | * |
||
| 1666 | * @param string $component Name of component |
||
| 1667 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 1668 | */ |
||
| 1669 | public function getRelationType($component) |
||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 1684 | * side of the relation. |
||
| 1685 | * |
||
| 1686 | * Notes on behaviour: |
||
| 1687 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 1688 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 1689 | * - Cannot be used on polymorphic relationships |
||
| 1690 | * - Cannot be used on unsaved objects. |
||
| 1691 | * |
||
| 1692 | * @param string $remoteClass |
||
| 1693 | * @param string $remoteRelation |
||
| 1694 | * @return DataList|DataObject The component, either as a list or single object |
||
| 1695 | * @throws BadMethodCallException |
||
| 1696 | * @throws InvalidArgumentException |
||
| 1697 | */ |
||
| 1698 | public function inferReciprocalComponent($remoteClass, $remoteRelation) |
||
| 1805 | |||
| 1806 | /** |
||
| 1807 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1808 | * @param string $componentName Name of the many-many component |
||
| 1809 | * @return RelationList|UnsavedRelationList The set of components |
||
| 1810 | */ |
||
| 1811 | public function getManyManyComponents($componentName) |
||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1868 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1869 | * |
||
| 1870 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1871 | * their classes. |
||
| 1872 | */ |
||
| 1873 | public function hasOne() |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1880 | * their class name will be returned. |
||
| 1881 | * |
||
| 1882 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1883 | * the field data stripped off. It defaults to TRUE. |
||
| 1884 | * @return string|array |
||
| 1885 | */ |
||
| 1886 | public function belongsTo($classOnly = true) |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 1898 | * relationships and their classes will be returned. |
||
| 1899 | * |
||
| 1900 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1901 | * the field data stripped off. It defaults to TRUE. |
||
| 1902 | * @return string|array|false |
||
| 1903 | */ |
||
| 1904 | public function hasMany($classOnly = true) |
||
| 1913 | |||
| 1914 | /** |
||
| 1915 | * Return the many-to-many extra fields specification. |
||
| 1916 | * |
||
| 1917 | * If you don't specify a component name, it returns all |
||
| 1918 | * extra fields for all components available. |
||
| 1919 | * |
||
| 1920 | * @return array|null |
||
| 1921 | */ |
||
| 1922 | public function manyManyExtraFields() |
||
| 1926 | |||
| 1927 | /** |
||
| 1928 | * Return information about a many-to-many component. |
||
| 1929 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 1930 | * components are returned. |
||
| 1931 | * |
||
| 1932 | * @see DataObjectSchema::manyManyComponent() |
||
| 1933 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 1934 | */ |
||
| 1935 | public function manyMany() |
||
| 1943 | |||
| 1944 | /** |
||
| 1945 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 1946 | * |
||
| 1947 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 1948 | * |
||
| 1949 | * @param string $class |
||
| 1950 | * @return array|false |
||
| 1951 | */ |
||
| 1952 | public function database_extensions($class) |
||
| 1961 | |||
| 1962 | /** |
||
| 1963 | * Generates a SearchContext to be used for building and processing |
||
| 1964 | * a generic search form for properties on this object. |
||
| 1965 | * |
||
| 1966 | * @return SearchContext |
||
| 1967 | */ |
||
| 1968 | public function getDefaultSearchContext() |
||
| 1976 | |||
| 1977 | /** |
||
| 1978 | * Determine which properties on the DataObject are |
||
| 1979 | * searchable, and map them to their default {@link FormField} |
||
| 1980 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 1981 | * |
||
| 1982 | * Some additional logic is included for switching field labels, based on |
||
| 1983 | * how generic or specific the field type is. |
||
| 1984 | * |
||
| 1985 | * Used by {@link SearchContext}. |
||
| 1986 | * |
||
| 1987 | * @param array $_params |
||
| 1988 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 1989 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 1990 | * @return FieldList |
||
| 1991 | */ |
||
| 1992 | public function scaffoldSearchFields($_params = null) |
||
| 2048 | |||
| 2049 | /** |
||
| 2050 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2051 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2052 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2053 | * |
||
| 2054 | * @uses FormScaffolder |
||
| 2055 | * |
||
| 2056 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2057 | * @return FieldList |
||
| 2058 | */ |
||
| 2059 | public function scaffoldFormFields($_params = null) |
||
| 2081 | |||
| 2082 | /** |
||
| 2083 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2084 | * being called on extensions |
||
| 2085 | * |
||
| 2086 | * @param callable $callback The callback to execute |
||
| 2087 | */ |
||
| 2088 | protected function beforeUpdateCMSFields($callback) |
||
| 2092 | |||
| 2093 | /** |
||
| 2094 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2095 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2096 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2097 | * generate this set. To customize, overload this method in a subclass |
||
| 2098 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2099 | * |
||
| 2100 | * <code> |
||
| 2101 | * class MyCustomClass extends DataObject { |
||
| 2102 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2103 | * |
||
| 2104 | * function getCMSFields() { |
||
| 2105 | * $fields = parent::getCMSFields(); |
||
| 2106 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2107 | * return $fields; |
||
| 2108 | * } |
||
| 2109 | * } |
||
| 2110 | * </code> |
||
| 2111 | * |
||
| 2112 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2113 | * |
||
| 2114 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2115 | */ |
||
| 2116 | public function getCMSFields() |
||
| 2129 | |||
| 2130 | /** |
||
| 2131 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2132 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2133 | * |
||
| 2134 | * @return FieldList an Empty FieldList(); need to be overload by solid subclass |
||
| 2135 | */ |
||
| 2136 | public function getCMSActions() |
||
| 2142 | |||
| 2143 | |||
| 2144 | /** |
||
| 2145 | * Used for simple frontend forms without relation editing |
||
| 2146 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2147 | * by default. To customize, either overload this method in your |
||
| 2148 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2149 | * |
||
| 2150 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2151 | * |
||
| 2152 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2153 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2154 | */ |
||
| 2155 | public function getFrontEndFields($params = null) |
||
| 2162 | |||
| 2163 | /** |
||
| 2164 | * Gets the value of a field. |
||
| 2165 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2166 | * |
||
| 2167 | * @param string $field The name of the field |
||
| 2168 | * @return mixed The field value |
||
| 2169 | */ |
||
| 2170 | public function getField($field) |
||
| 2190 | |||
| 2191 | /** |
||
| 2192 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2193 | * |
||
| 2194 | * @param string $class Class to load the values from. Others are joined as required. |
||
| 2195 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2196 | * @return bool Flag if lazy loading succeeded |
||
| 2197 | */ |
||
| 2198 | protected function loadLazyFields($class = null) |
||
| 2274 | |||
| 2275 | /** |
||
| 2276 | * Return the fields that have changed. |
||
| 2277 | * |
||
| 2278 | * The change level affects what the functions defines as "changed": |
||
| 2279 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2280 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2281 | * for example a change from 0 to null would not be included. |
||
| 2282 | * |
||
| 2283 | * Example return: |
||
| 2284 | * <code> |
||
| 2285 | * array( |
||
| 2286 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2287 | * ) |
||
| 2288 | * </code> |
||
| 2289 | * |
||
| 2290 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2291 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2292 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2293 | * @return array |
||
| 2294 | */ |
||
| 2295 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) |
||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2345 | * since loading them from the database. |
||
| 2346 | * |
||
| 2347 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2348 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2349 | * @return boolean |
||
| 2350 | */ |
||
| 2351 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) |
||
| 2361 | |||
| 2362 | /** |
||
| 2363 | * Set the value of the field |
||
| 2364 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2365 | * |
||
| 2366 | * @param string $fieldName Name of the field |
||
| 2367 | * @param mixed $val New field value |
||
| 2368 | * @return $this |
||
| 2369 | */ |
||
| 2370 | public function setField($fieldName, $val) |
||
| 2422 | |||
| 2423 | /** |
||
| 2424 | * Set the value of the field, using a casting object. |
||
| 2425 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2426 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2427 | * can be saved into the Image table. |
||
| 2428 | * |
||
| 2429 | * @param string $fieldName Name of the field |
||
| 2430 | * @param mixed $value New field value |
||
| 2431 | * @return $this |
||
| 2432 | */ |
||
| 2433 | public function setCastedField($fieldName, $value) |
||
| 2447 | |||
| 2448 | /** |
||
| 2449 | * {@inheritdoc} |
||
| 2450 | */ |
||
| 2451 | public function castingHelper($field) |
||
| 2471 | |||
| 2472 | /** |
||
| 2473 | * Returns true if the given field exists in a database column on any of |
||
| 2474 | * the objects tables and optionally look up a dynamic getter with |
||
| 2475 | * get<fieldName>(). |
||
| 2476 | * |
||
| 2477 | * @param string $field Name of the field |
||
| 2478 | * @return boolean True if the given field exists |
||
| 2479 | */ |
||
| 2480 | public function hasField($field) |
||
| 2490 | |||
| 2491 | /** |
||
| 2492 | * Returns true if the given field exists as a database column |
||
| 2493 | * |
||
| 2494 | * @param string $field Name of the field |
||
| 2495 | * |
||
| 2496 | * @return boolean |
||
| 2497 | */ |
||
| 2498 | public function hasDatabaseField($field) |
||
| 2503 | |||
| 2504 | /** |
||
| 2505 | * Returns true if the member is allowed to do the given action. |
||
| 2506 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2507 | * |
||
| 2508 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2509 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2510 | * in user. |
||
| 2511 | * @param array $context Additional $context to pass to extendedCan() |
||
| 2512 | * |
||
| 2513 | * @return boolean True if the the member is allowed to do the given action |
||
| 2514 | */ |
||
| 2515 | public function can($perm, $member = null, $context = array()) |
||
| 2537 | |||
| 2538 | /** |
||
| 2539 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2540 | * expected to return one of three values: |
||
| 2541 | * |
||
| 2542 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2543 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2544 | * - NULL: Don't affect the outcome |
||
| 2545 | * |
||
| 2546 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2547 | * |
||
| 2548 | * <code> |
||
| 2549 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2550 | * if($extended !== null) return $extended; |
||
| 2551 | * else return $normalValue; |
||
| 2552 | * </code> |
||
| 2553 | * |
||
| 2554 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2555 | * @param Member|int $member |
||
| 2556 | * @param array $context Optional context |
||
| 2557 | * @return boolean|null |
||
| 2558 | */ |
||
| 2559 | public function extendedCan($methodName, $member, $context = array()) |
||
| 2575 | |||
| 2576 | /** |
||
| 2577 | * @param Member $member |
||
| 2578 | * @return boolean |
||
| 2579 | */ |
||
| 2580 | public function canView($member = null) |
||
| 2588 | |||
| 2589 | /** |
||
| 2590 | * @param Member $member |
||
| 2591 | * @return boolean |
||
| 2592 | */ |
||
| 2593 | public function canEdit($member = null) |
||
| 2601 | |||
| 2602 | /** |
||
| 2603 | * @param Member $member |
||
| 2604 | * @return boolean |
||
| 2605 | */ |
||
| 2606 | public function canDelete($member = null) |
||
| 2614 | |||
| 2615 | /** |
||
| 2616 | * @param Member $member |
||
| 2617 | * @param array $context Additional context-specific data which might |
||
| 2618 | * affect whether (or where) this object could be created. |
||
| 2619 | * @return boolean |
||
| 2620 | */ |
||
| 2621 | public function canCreate($member = null, $context = array()) |
||
| 2629 | |||
| 2630 | /** |
||
| 2631 | * Debugging used by Debug::show() |
||
| 2632 | * |
||
| 2633 | * @return string HTML data representing this object |
||
| 2634 | */ |
||
| 2635 | public function debug() |
||
| 2646 | |||
| 2647 | /** |
||
| 2648 | * Return the DBField object that represents the given field. |
||
| 2649 | * This works similarly to obj() with 2 key differences: |
||
| 2650 | * - it still returns an object even when the field has no value. |
||
| 2651 | * - it only matches fields and not methods |
||
| 2652 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 2653 | * |
||
| 2654 | * @param string $fieldName Name of the field |
||
| 2655 | * @return DBField The field as a DBField object |
||
| 2656 | */ |
||
| 2657 | public function dbObject($fieldName) |
||
| 2688 | |||
| 2689 | /** |
||
| 2690 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 2691 | * |
||
| 2692 | * The path to the related field is specified with dot separated syntax |
||
| 2693 | * (eg: Parent.Child.Child.FieldName). |
||
| 2694 | * |
||
| 2695 | * @param string $fieldPath |
||
| 2696 | * |
||
| 2697 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 2698 | */ |
||
| 2699 | public function relObject($fieldPath) |
||
| 2729 | |||
| 2730 | /** |
||
| 2731 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 2732 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 2733 | * |
||
| 2734 | * @param $fieldName string |
||
| 2735 | * @return string | null - will return null on a missing value |
||
| 2736 | */ |
||
| 2737 | public function relField($fieldName) |
||
| 2773 | |||
| 2774 | /** |
||
| 2775 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 2776 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 2777 | * |
||
| 2778 | * @param string $className |
||
| 2779 | * @return string |
||
| 2780 | */ |
||
| 2781 | public function getReverseAssociation($className) |
||
| 2804 | |||
| 2805 | /** |
||
| 2806 | * Return all objects matching the filter |
||
| 2807 | * sub-classes are automatically selected and included |
||
| 2808 | * |
||
| 2809 | * @param string $callerClass The class of objects to be returned |
||
| 2810 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 2811 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 2812 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 2813 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 2814 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 2815 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 2816 | * @param string $containerClass The container class to return the results in. |
||
| 2817 | * |
||
| 2818 | * @todo $containerClass is Ignored, why? |
||
| 2819 | * |
||
| 2820 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 2821 | */ |
||
| 2822 | public static function get( |
||
| 2865 | |||
| 2866 | |||
| 2867 | /** |
||
| 2868 | * Return the first item matching the given query. |
||
| 2869 | * All calls to get_one() are cached. |
||
| 2870 | * |
||
| 2871 | * @param string $callerClass The class of objects to be returned |
||
| 2872 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 2873 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 2874 | * @param boolean $cache Use caching |
||
| 2875 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 2876 | * |
||
| 2877 | * @return DataObject The first item matching the query |
||
| 2878 | */ |
||
| 2879 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") |
||
| 2906 | |||
| 2907 | /** |
||
| 2908 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 2909 | * Also clears any cached aggregate data. |
||
| 2910 | * |
||
| 2911 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 2912 | * When false will just clear session-local cached data |
||
| 2913 | * @return DataObject $this |
||
| 2914 | */ |
||
| 2915 | public function flushCache($persistent = true) |
||
| 2934 | |||
| 2935 | /** |
||
| 2936 | * Flush the get_one global cache and destroy associated objects. |
||
| 2937 | */ |
||
| 2938 | public static function flush_and_destroy_cache() |
||
| 2953 | |||
| 2954 | /** |
||
| 2955 | * Reset all global caches associated with DataObject. |
||
| 2956 | */ |
||
| 2957 | public static function reset() |
||
| 2966 | |||
| 2967 | /** |
||
| 2968 | * Return the given element, searching by ID |
||
| 2969 | * |
||
| 2970 | * @param string $callerClass The class of the object to be returned |
||
| 2971 | * @param int $id The id of the element |
||
| 2972 | * @param boolean $cache See {@link get_one()} |
||
| 2973 | * |
||
| 2974 | * @return DataObject The element |
||
| 2975 | */ |
||
| 2976 | public static function get_by_id($callerClass, $id, $cache = true) |
||
| 2986 | |||
| 2987 | /** |
||
| 2988 | * Get the name of the base table for this object |
||
| 2989 | * |
||
| 2990 | * @return string |
||
| 2991 | */ |
||
| 2992 | public function baseTable() |
||
| 2996 | |||
| 2997 | /** |
||
| 2998 | * Get the base class for this object |
||
| 2999 | * |
||
| 3000 | * @return string |
||
| 3001 | */ |
||
| 3002 | public function baseClass() |
||
| 3006 | |||
| 3007 | /** |
||
| 3008 | * @var array Parameters used in the query that built this object. |
||
| 3009 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3010 | * run additional queries using the same context. |
||
| 3011 | */ |
||
| 3012 | protected $sourceQueryParams; |
||
| 3013 | |||
| 3014 | /** |
||
| 3015 | * @see $sourceQueryParams |
||
| 3016 | * @return array |
||
| 3017 | */ |
||
| 3018 | public function getSourceQueryParams() |
||
| 3022 | |||
| 3023 | /** |
||
| 3024 | * Get list of parameters that should be inherited to relations on this object |
||
| 3025 | * |
||
| 3026 | * @return array |
||
| 3027 | */ |
||
| 3028 | public function getInheritableQueryParams() |
||
| 3034 | |||
| 3035 | /** |
||
| 3036 | * @see $sourceQueryParams |
||
| 3037 | * @param array |
||
| 3038 | */ |
||
| 3039 | public function setSourceQueryParams($array) |
||
| 3043 | |||
| 3044 | /** |
||
| 3045 | * @see $sourceQueryParams |
||
| 3046 | * @param string $key |
||
| 3047 | * @param string $value |
||
| 3048 | */ |
||
| 3049 | public function setSourceQueryParam($key, $value) |
||
| 3053 | |||
| 3054 | /** |
||
| 3055 | * @see $sourceQueryParams |
||
| 3056 | * @param string $key |
||
| 3057 | * @return string |
||
| 3058 | */ |
||
| 3059 | public function getSourceQueryParam($key) |
||
| 3066 | |||
| 3067 | //-------------------------------------------------------------------------------------------// |
||
| 3068 | |||
| 3069 | /** |
||
| 3070 | * Return the database indexes on this table. |
||
| 3071 | * This array is indexed by the name of the field with the index, and |
||
| 3072 | * the value is the type of index. |
||
| 3073 | */ |
||
| 3074 | public function databaseIndexes() |
||
| 3100 | |||
| 3101 | /** |
||
| 3102 | * Check the database schema and update it as necessary. |
||
| 3103 | * |
||
| 3104 | * @uses DataExtension->augmentDatabase() |
||
| 3105 | */ |
||
| 3106 | public function requireTable() |
||
| 3173 | |||
| 3174 | /** |
||
| 3175 | * Add default records to database. This function is called whenever the |
||
| 3176 | * database is built, after the database tables have all been created. Overload |
||
| 3177 | * this to add default records when the database is built, but make sure you |
||
| 3178 | * call parent::requireDefaultRecords(). |
||
| 3179 | * |
||
| 3180 | * @uses DataExtension->requireDefaultRecords() |
||
| 3181 | */ |
||
| 3182 | public function requireDefaultRecords() |
||
| 3201 | |||
| 3202 | /** |
||
| 3203 | * Get the default searchable fields for this object, as defined in the |
||
| 3204 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3205 | * data object, uses a default selection of summary fields. |
||
| 3206 | * |
||
| 3207 | * @return array |
||
| 3208 | */ |
||
| 3209 | public function searchableFields() |
||
| 3286 | |||
| 3287 | /** |
||
| 3288 | * Get any user defined searchable fields labels that |
||
| 3289 | * exist. Allows overriding of default field names in the form |
||
| 3290 | * interface actually presented to the user. |
||
| 3291 | * |
||
| 3292 | * The reason for keeping this separate from searchable_fields, |
||
| 3293 | * which would be a logical place for this functionality, is to |
||
| 3294 | * avoid bloating and complicating the configuration array. Currently |
||
| 3295 | * much of this system is based on sensible defaults, and this property |
||
| 3296 | * would generally only be set in the case of more complex relationships |
||
| 3297 | * between data object being required in the search interface. |
||
| 3298 | * |
||
| 3299 | * Generates labels based on name of the field itself, if no static property |
||
| 3300 | * {@link self::field_labels} exists. |
||
| 3301 | * |
||
| 3302 | * @uses $field_labels |
||
| 3303 | * @uses FormField::name_to_label() |
||
| 3304 | * |
||
| 3305 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3306 | * |
||
| 3307 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3308 | */ |
||
| 3309 | public function fieldLabels($includerelations = true) |
||
| 3349 | |||
| 3350 | /** |
||
| 3351 | * Get a human-readable label for a single field, |
||
| 3352 | * see {@link fieldLabels()} for more details. |
||
| 3353 | * |
||
| 3354 | * @uses fieldLabels() |
||
| 3355 | * @uses FormField::name_to_label() |
||
| 3356 | * |
||
| 3357 | * @param string $name Name of the field |
||
| 3358 | * @return string Label of the field |
||
| 3359 | */ |
||
| 3360 | public function fieldLabel($name) |
||
| 3365 | |||
| 3366 | /** |
||
| 3367 | * Get the default summary fields for this object. |
||
| 3368 | * |
||
| 3369 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3370 | * |
||
| 3371 | * @return array |
||
| 3372 | */ |
||
| 3373 | public function summaryFields() |
||
| 3417 | |||
| 3418 | /** |
||
| 3419 | * Defines a default list of filters for the search context. |
||
| 3420 | * |
||
| 3421 | * If a filter class mapping is defined on the data object, |
||
| 3422 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3423 | * {@link DBField} is used. |
||
| 3424 | * |
||
| 3425 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3426 | * |
||
| 3427 | * @return array |
||
| 3428 | */ |
||
| 3429 | public function defaultSearchFilters() |
||
| 3446 | |||
| 3447 | /** |
||
| 3448 | * @return boolean True if the object is in the database |
||
| 3449 | */ |
||
| 3450 | public function isInDB() |
||
| 3454 | |||
| 3455 | /* |
||
| 3456 | * @ignore |
||
| 3457 | */ |
||
| 3458 | private static $subclass_access = true; |
||
| 3459 | |||
| 3460 | /** |
||
| 3461 | * Temporarily disable subclass access in data object qeur |
||
| 3462 | */ |
||
| 3463 | public static function disable_subclass_access() |
||
| 3471 | |||
| 3472 | //-------------------------------------------------------------------------------------------// |
||
| 3473 | |||
| 3474 | /** |
||
| 3475 | * Database field definitions. |
||
| 3476 | * This is a map from field names to field type. The field |
||
| 3477 | * type should be a class that extends . |
||
| 3478 | * @var array |
||
| 3479 | * @config |
||
| 3480 | */ |
||
| 3481 | private static $db = []; |
||
| 3482 | |||
| 3483 | /** |
||
| 3484 | * Use a casting object for a field. This is a map from |
||
| 3485 | * field name to class name of the casting object. |
||
| 3486 | * |
||
| 3487 | * @var array |
||
| 3488 | */ |
||
| 3489 | private static $casting = array( |
||
| 3490 | "Title" => 'Text', |
||
| 3491 | ); |
||
| 3492 | |||
| 3493 | /** |
||
| 3494 | * Specify custom options for a CREATE TABLE call. |
||
| 3495 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3496 | * All options have to be keyed for a specific database implementation, |
||
| 3497 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3498 | * |
||
| 3499 | * <code> |
||
| 3500 | * array( |
||
| 3501 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3502 | * ) |
||
| 3503 | * </code> |
||
| 3504 | * |
||
| 3505 | * Caution: This API is experimental, and might not be |
||
| 3506 | * included in the next major release. Please use with care. |
||
| 3507 | * |
||
| 3508 | * @var array |
||
| 3509 | * @config |
||
| 3510 | */ |
||
| 3511 | private static $create_table_options = array( |
||
| 3512 | 'SilverStripe\ORM\Connect\MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3513 | ); |
||
| 3514 | |||
| 3515 | /** |
||
| 3516 | * If a field is in this array, then create a database index |
||
| 3517 | * on that field. This is a map from fieldname to index type. |
||
| 3518 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3519 | * |
||
| 3520 | * @var array |
||
| 3521 | * @config |
||
| 3522 | */ |
||
| 3523 | private static $indexes = null; |
||
| 3524 | |||
| 3525 | /** |
||
| 3526 | * Inserts standard column-values when a DataObject |
||
| 3527 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3528 | * This is a map from fieldname to default value. |
||
| 3529 | * |
||
| 3530 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3531 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3532 | * or false in your subclass. Setting it to null won't work. |
||
| 3533 | * |
||
| 3534 | * @var array |
||
| 3535 | * @config |
||
| 3536 | */ |
||
| 3537 | private static $defaults = []; |
||
| 3538 | |||
| 3539 | /** |
||
| 3540 | * Multidimensional array which inserts default data into the database |
||
| 3541 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3542 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3543 | * behaviour such as publishing and ParentNodes. |
||
| 3544 | * |
||
| 3545 | * Example: |
||
| 3546 | * array( |
||
| 3547 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3548 | * array('Title' => "DefaultPage2") |
||
| 3549 | * ). |
||
| 3550 | * |
||
| 3551 | * @var array |
||
| 3552 | * @config |
||
| 3553 | */ |
||
| 3554 | private static $default_records = null; |
||
| 3555 | |||
| 3556 | /** |
||
| 3557 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3558 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3559 | * |
||
| 3560 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3561 | * |
||
| 3562 | * @var array |
||
| 3563 | * @config |
||
| 3564 | */ |
||
| 3565 | private static $has_one = []; |
||
| 3566 | |||
| 3567 | /** |
||
| 3568 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3569 | * |
||
| 3570 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3571 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3572 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3573 | * |
||
| 3574 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3575 | * |
||
| 3576 | * @var array |
||
| 3577 | * @config |
||
| 3578 | */ |
||
| 3579 | private static $belongs_to = []; |
||
| 3580 | |||
| 3581 | /** |
||
| 3582 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3583 | * |
||
| 3584 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3585 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3586 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3587 | * which foreign key to use. |
||
| 3588 | * |
||
| 3589 | * @var array |
||
| 3590 | * @config |
||
| 3591 | */ |
||
| 3592 | private static $has_many = []; |
||
| 3593 | |||
| 3594 | /** |
||
| 3595 | * many-many relationship definitions. |
||
| 3596 | * This is a map from component name to data type. |
||
| 3597 | * @var array |
||
| 3598 | * @config |
||
| 3599 | */ |
||
| 3600 | private static $many_many = []; |
||
| 3601 | |||
| 3602 | /** |
||
| 3603 | * Extra fields to include on the connecting many-many table. |
||
| 3604 | * This is a map from field name to field type. |
||
| 3605 | * |
||
| 3606 | * Example code: |
||
| 3607 | * <code> |
||
| 3608 | * public static $many_many_extraFields = array( |
||
| 3609 | * 'Members' => array( |
||
| 3610 | * 'Role' => 'Varchar(100)' |
||
| 3611 | * ) |
||
| 3612 | * ); |
||
| 3613 | * </code> |
||
| 3614 | * |
||
| 3615 | * @var array |
||
| 3616 | * @config |
||
| 3617 | */ |
||
| 3618 | private static $many_many_extraFields = []; |
||
| 3619 | |||
| 3620 | /** |
||
| 3621 | * The inverse side of a many-many relationship. |
||
| 3622 | * This is a map from component name to data type. |
||
| 3623 | * @var array |
||
| 3624 | * @config |
||
| 3625 | */ |
||
| 3626 | private static $belongs_many_many = []; |
||
| 3627 | |||
| 3628 | /** |
||
| 3629 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3630 | * clause of a SQL query if no other sort expression is provided. |
||
| 3631 | * @var string |
||
| 3632 | * @config |
||
| 3633 | */ |
||
| 3634 | private static $default_sort = null; |
||
| 3635 | |||
| 3636 | /** |
||
| 3637 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3638 | * search interface. |
||
| 3639 | * |
||
| 3640 | * Overriding the default filter, with a custom defined filter: |
||
| 3641 | * <code> |
||
| 3642 | * static $searchable_fields = array( |
||
| 3643 | * "Name" => "PartialMatchFilter" |
||
| 3644 | * ); |
||
| 3645 | * </code> |
||
| 3646 | * |
||
| 3647 | * Overriding the default form fields, with a custom defined field. |
||
| 3648 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3649 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3650 | * <code> |
||
| 3651 | * static $searchable_fields = array( |
||
| 3652 | * "Name" => array( |
||
| 3653 | * "field" => "TextField" |
||
| 3654 | * ) |
||
| 3655 | * ); |
||
| 3656 | * </code> |
||
| 3657 | * |
||
| 3658 | * Overriding the default form field, filter and title: |
||
| 3659 | * <code> |
||
| 3660 | * static $searchable_fields = array( |
||
| 3661 | * "Organisation.ZipCode" => array( |
||
| 3662 | * "field" => "TextField", |
||
| 3663 | * "filter" => "PartialMatchFilter", |
||
| 3664 | * "title" => 'Organisation ZIP' |
||
| 3665 | * ) |
||
| 3666 | * ); |
||
| 3667 | * </code> |
||
| 3668 | * @config |
||
| 3669 | */ |
||
| 3670 | private static $searchable_fields = null; |
||
| 3671 | |||
| 3672 | /** |
||
| 3673 | * User defined labels for searchable_fields, used to override |
||
| 3674 | * default display in the search form. |
||
| 3675 | * @config |
||
| 3676 | */ |
||
| 3677 | private static $field_labels = []; |
||
| 3678 | |||
| 3679 | /** |
||
| 3680 | * Provides a default list of fields to be used by a 'summary' |
||
| 3681 | * view of this object. |
||
| 3682 | * @config |
||
| 3683 | */ |
||
| 3684 | private static $summary_fields = []; |
||
| 3685 | |||
| 3686 | public function provideI18nEntities() |
||
| 3702 | |||
| 3703 | /** |
||
| 3704 | * Returns true if the given method/parameter has a value |
||
| 3705 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 3706 | * |
||
| 3707 | * @param string $field The field name |
||
| 3708 | * @param array $arguments |
||
| 3709 | * @param bool $cache |
||
| 3710 | * @return boolean |
||
| 3711 | */ |
||
| 3712 | public function hasValue($field, $arguments = null, $cache = true) |
||
| 3722 | |||
| 3723 | /** |
||
| 3724 | * If selected through a many_many through relation, this is the instance of the joined record |
||
| 3725 | * |
||
| 3726 | * @return DataObject |
||
| 3727 | */ |
||
| 3728 | public function getJoin() |
||
| 3732 | |||
| 3733 | /** |
||
| 3734 | * Set joining object |
||
| 3735 | * |
||
| 3736 | * @param DataObject $object |
||
| 3737 | * @param string $alias Alias |
||
| 3738 | * @return $this |
||
| 3739 | */ |
||
| 3740 | public function setJoin(DataObject $object, $alias = null) |
||
| 3753 | } |
||
| 3754 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.