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 |
||
| 74 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Human-readable singular name. |
||
| 78 | * @var string |
||
| 79 | * @config |
||
| 80 | */ |
||
| 81 | private static $singular_name = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Human-readable plural name |
||
| 85 | * @var string |
||
| 86 | * @config |
||
| 87 | */ |
||
| 88 | private static $plural_name = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Allow API access to this object? |
||
| 92 | * @todo Define the options that can be set here |
||
| 93 | * @config |
||
| 94 | */ |
||
| 95 | private static $api_access = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * True if this DataObject has been destroyed. |
||
| 99 | * @var boolean |
||
| 100 | */ |
||
| 101 | public $destroyed = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The DataModel from this this object comes |
||
| 105 | */ |
||
| 106 | protected $model; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 110 | * |
||
| 111 | * Use {@link toMap()} if you want an array representation |
||
| 112 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $record; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 120 | */ |
||
| 121 | const CHANGE_NONE = 0; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 125 | * (before !== after && before == after) |
||
| 126 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 127 | * Value changes are by nature also considered strict changes. |
||
| 128 | */ |
||
| 129 | const CHANGE_STRICT = 1; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Represents a field that has changed the loosely defined value |
||
| 133 | * (before != after, thus, before !== after)) |
||
| 134 | * E.g. change false to true, but not false to 0 |
||
| 135 | */ |
||
| 136 | const CHANGE_VALUE = 2; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * An array indexed by fieldname, true if the field has been changed. |
||
| 140 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 141 | * the changed state. |
||
| 142 | * |
||
| 143 | * @var array |
||
| 144 | */ |
||
| 145 | private $changed; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The database record (in the same format as $record), before |
||
| 149 | * any changes. |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $original; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 156 | * @var boolean |
||
| 157 | */ |
||
| 158 | protected $brokenOnDelete = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 162 | * @var boolean |
||
| 163 | */ |
||
| 164 | protected $brokenOnWrite = false; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @config |
||
| 168 | * @var boolean Should dataobjects be validated before they are written? |
||
| 169 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 170 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 171 | * to only disable validation for very specific use cases. |
||
| 172 | */ |
||
| 173 | private static $validation_enabled = true; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Static caches used by relevant functions. |
||
| 177 | */ |
||
| 178 | public static $cache_has_own_table = array(); |
||
| 179 | protected static $_cache_db = array(); |
||
| 180 | protected static $_cache_get_one; |
||
| 181 | protected static $_cache_get_class_ancestry; |
||
| 182 | protected static $_cache_composite_fields = array(); |
||
| 183 | protected static $_cache_is_composite_field = array(); |
||
| 184 | protected static $_cache_custom_database_fields = array(); |
||
| 185 | protected static $_cache_field_labels = array(); |
||
| 186 | |||
| 187 | // base fields which are not defined in static $db |
||
| 188 | private static $fixed_fields = array( |
||
| 189 | 'ID' => 'Int', |
||
| 190 | 'ClassName' => 'Enum', |
||
| 191 | 'LastEdited' => 'SS_Datetime', |
||
| 192 | 'Created' => 'SS_Datetime', |
||
| 193 | ); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Non-static relationship cache, indexed by component name. |
||
| 197 | */ |
||
| 198 | protected $components; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 202 | */ |
||
| 203 | protected $unsavedRelations; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns when validation on DataObjects is enabled. |
||
| 207 | * |
||
| 208 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public static function get_validation_enabled() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set whether DataObjects should be validated before they are written. |
||
| 218 | * |
||
| 219 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 220 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 221 | * to only disable validation for very specific use cases. |
||
| 222 | * |
||
| 223 | * @param $enable bool |
||
| 224 | * @see DataObject::validate() |
||
| 225 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
| 226 | */ |
||
| 227 | public static function set_validation_enabled($enable) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @var [string] - class => ClassName field definition cache for self::database_fields |
||
| 234 | */ |
||
| 235 | private static $classname_spec_cache = array(); |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Clear all cached classname specs. It's necessary to clear all cached subclassed names |
||
| 239 | * for any classes if a new class manifest is generated. |
||
| 240 | */ |
||
| 241 | public static function clear_classname_spec_cache() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Determines the specification for the ClassName field for the given class |
||
| 248 | * |
||
| 249 | * @param string $class |
||
| 250 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
| 251 | * @return string Resulting ClassName spec. If $queryDB is true this will include all |
||
| 252 | * legacy types that no longer have concrete classes in PHP |
||
| 253 | */ |
||
| 254 | public static function get_classname_spec($class, $queryDB = true) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName". |
||
| 275 | * See {@link custom_database_fields()} for a getter that excludes these "base fields". |
||
| 276 | * |
||
| 277 | * @param string $class |
||
| 278 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public static function database_fields($class, $queryDB = true) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
| 298 | * and {@link DataObject::$has_one}. Resolves instances of {@link CompositeDBField} |
||
| 299 | * into the actual database fields, rather than the name of the field which |
||
| 300 | * might not equate a database column. |
||
| 301 | * |
||
| 302 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
| 303 | * see {@link database_fields()}. |
||
| 304 | * |
||
| 305 | * @uses CompositeDBField->compositeDatabaseFields() |
||
| 306 | * |
||
| 307 | * @param string $class |
||
| 308 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 309 | */ |
||
| 310 | public static function custom_database_fields($class) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the field class if the given db field on the class is a composite field. |
||
| 356 | * Will check all applicable ancestor classes and aggregate results. |
||
| 357 | * |
||
| 358 | * @param string $class Class to check |
||
| 359 | * @param string $name Field to check |
||
| 360 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
| 361 | * @return string Class name of composite field if it exists |
||
| 362 | */ |
||
| 363 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 387 | * Will check all applicable ancestor classes and aggregate results. |
||
| 388 | */ |
||
| 389 | public static function composite_fields($class, $aggregated = true) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Internal cacher for the composite field information |
||
| 404 | */ |
||
| 405 | private static function cache_composite_fields($class) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Construct a new DataObject. |
||
| 427 | * |
||
| 428 | * @param array|null $record Used internally for rehydrating an object from database content. |
||
| 429 | * Bypasses setters on this class, and hence should not be used |
||
| 430 | * for populating data on new records. |
||
| 431 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 432 | * Singletons don't have their defaults set. |
||
| 433 | */ |
||
| 434 | public function __construct($record = null, $isSingleton = false, $model = null) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Set the DataModel |
||
| 507 | * @param DataModel $model |
||
| 508 | * @return DataObject $this |
||
| 509 | */ |
||
| 510 | public function setDataModel(DataModel $model) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Destroy all of this objects dependant objects and local caches. |
||
| 517 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 518 | */ |
||
| 519 | public function destroy() { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Create a duplicate of this node. |
||
| 527 | * Note: now also duplicates relations. |
||
| 528 | * |
||
| 529 | * @param $doWrite Perform a write() operation before returning the object. If this is true, it will create the |
||
| 530 | * duplicate in the database. |
||
| 531 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 532 | */ |
||
| 533 | public function duplicate($doWrite = true) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
| 552 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
| 553 | * automatically when adding the new relations. |
||
| 554 | * |
||
| 555 | * @param $sourceObject the source object to duplicate from |
||
| 556 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
| 557 | * @return DataObject with the new many_many relations copied in |
||
| 558 | */ |
||
| 559 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Helper function to duplicate relations from one object to another |
||
| 582 | * @param $sourceObject the source object to duplicate from |
||
| 583 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
| 584 | * @param $name the name of the relation to duplicate (e.g. members) |
||
| 585 | */ |
||
| 586 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
| 613 | |||
| 614 | public function getObsoleteClassName() { |
||
| 618 | |||
| 619 | public function getClassName() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 627 | * Warning: This will produce an inconsistent record, as the object |
||
| 628 | * instance will not automatically switch to the new subclass. |
||
| 629 | * Please use {@link newClassInstance()} for this purpose, |
||
| 630 | * or destroy and reinstanciate the record. |
||
| 631 | * |
||
| 632 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 633 | * @return DataObject $this |
||
| 634 | */ |
||
| 635 | public function setClassName($className) { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Create a new instance of a different class from this object's record. |
||
| 646 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 647 | * it ensures that the instance of the class is a match for the className of the |
||
| 648 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 649 | * property manually before calling this method, as it will confuse change detection. |
||
| 650 | * |
||
| 651 | * If the new class is different to the original class, defaults are populated again |
||
| 652 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 653 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 654 | * we still need to repopulate the defaults. |
||
| 655 | * |
||
| 656 | * @param string $newClassName The name of the new class |
||
| 657 | * |
||
| 658 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 659 | */ |
||
| 660 | public function newClassInstance($newClassName) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Adds methods from the extensions. |
||
| 681 | * Called by Object::__construct() once per class. |
||
| 682 | */ |
||
| 683 | public function defineMethods() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 725 | * The default behaviour for a DataObject is to return true if |
||
| 726 | * the object exists in the database, you can override this in subclasses. |
||
| 727 | * |
||
| 728 | * @return boolean true if this object exists |
||
| 729 | */ |
||
| 730 | public function exists() { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Returns TRUE if all values (other than "ID") are |
||
| 736 | * considered empty (by weak boolean comparison). |
||
| 737 | * Only checks for fields listed in {@link custom_database_fields()} |
||
| 738 | * |
||
| 739 | * @todo Use DBField->hasValue() |
||
| 740 | * |
||
| 741 | * @return boolean |
||
| 742 | */ |
||
| 743 | public function isEmpty(){ |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Get the user friendly singular name of this DataObject. |
||
| 760 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 761 | * this returns the class name. |
||
| 762 | * |
||
| 763 | * @return string User friendly singular name of this DataObject |
||
| 764 | */ |
||
| 765 | public function singular_name() { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Get the translated user friendly singular name of this DataObject |
||
| 775 | * same as singular_name() but runs it through the translating function |
||
| 776 | * |
||
| 777 | * Translating string is in the form: |
||
| 778 | * $this->class.SINGULARNAME |
||
| 779 | * Example: |
||
| 780 | * Page.SINGULARNAME |
||
| 781 | * |
||
| 782 | * @return string User friendly translated singular name of this DataObject |
||
| 783 | */ |
||
| 784 | public function i18n_singular_name() { |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Get the user friendly plural name of this DataObject |
||
| 790 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 791 | * this returns a pluralised version of the class name. |
||
| 792 | * |
||
| 793 | * @return string User friendly plural name of this DataObject |
||
| 794 | */ |
||
| 795 | public function plural_name() { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * Get the translated user friendly plural name of this DataObject |
||
| 810 | * Same as plural_name but runs it through the translation function |
||
| 811 | * Translation string is in the form: |
||
| 812 | * $this->class.PLURALNAME |
||
| 813 | * Example: |
||
| 814 | * Page.PLURALNAME |
||
| 815 | * |
||
| 816 | * @return string User friendly translated plural name of this DataObject |
||
| 817 | */ |
||
| 818 | public function i18n_plural_name() |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Standard implementation of a title/label for a specific |
||
| 826 | * record. Tries to find properties 'Title' or 'Name', |
||
| 827 | * and falls back to the 'ID'. Useful to provide |
||
| 828 | * user-friendly identification of a record, e.g. in errormessages |
||
| 829 | * or UI-selections. |
||
| 830 | * |
||
| 831 | * Overload this method to have a more specialized implementation, |
||
| 832 | * e.g. for an Address record this could be: |
||
| 833 | * <code> |
||
| 834 | * function getTitle() { |
||
| 835 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 836 | * } |
||
| 837 | * </code> |
||
| 838 | * |
||
| 839 | * @return string |
||
| 840 | */ |
||
| 841 | public function getTitle() { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Returns the associated database record - in this case, the object itself. |
||
| 850 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 851 | * |
||
| 852 | * @return DataObject Associated database record |
||
| 853 | */ |
||
| 854 | public function data() { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Convert this object to a map. |
||
| 860 | * |
||
| 861 | * @return array The data as a map. |
||
| 862 | */ |
||
| 863 | public function toMap() { |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Return all currently fetched database fields. |
||
| 870 | * |
||
| 871 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 872 | * Obviously, this makes it a lot faster. |
||
| 873 | * |
||
| 874 | * @return array The data as a map. |
||
| 875 | */ |
||
| 876 | public function getQueriedDatabaseFields() { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 882 | * |
||
| 883 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 884 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 885 | * |
||
| 886 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 887 | * the related objects that it alters. |
||
| 888 | * |
||
| 889 | * @param array $data A map of field name to data values to update. |
||
| 890 | * @return DataObject $this |
||
| 891 | */ |
||
| 892 | public function update($data) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Pass changes as a map, and try to |
||
| 942 | * get automatic casting for these fields. |
||
| 943 | * Doesn't write to the database. To write the data, |
||
| 944 | * use the write() method. |
||
| 945 | * |
||
| 946 | * @param array $data A map of field name to data values to update. |
||
| 947 | * @return DataObject $this |
||
| 948 | */ |
||
| 949 | public function castedUpdate($data) { |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Merges data and relations from another object of same class, |
||
| 958 | * without conflict resolution. Allows to specify which |
||
| 959 | * dataset takes priority in case its not empty. |
||
| 960 | * has_one-relations are just transferred with priority 'right'. |
||
| 961 | * has_many and many_many-relations are added regardless of priority. |
||
| 962 | * |
||
| 963 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 964 | * meaning they are not connected to the merged object any longer. |
||
| 965 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 966 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 967 | * Caution: Does not delete the merged object. |
||
| 968 | * Caution: Does now overwrite Created date on the original object. |
||
| 969 | * |
||
| 970 | * @param $obj DataObject |
||
| 971 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
| 972 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
| 973 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
| 974 | * Only applicable with $priority='right'. (optional) |
||
| 975 | * @return Boolean |
||
| 976 | */ |
||
| 977 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Forces the record to think that all its data has changed. |
||
| 1049 | * Doesn't write to the database. Only sets fields as changed |
||
| 1050 | * if they are not already marked as changed. |
||
| 1051 | * |
||
| 1052 | * @return $this |
||
| 1053 | */ |
||
| 1054 | public function forceChange() { |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Validate the current object. |
||
| 1077 | * |
||
| 1078 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1079 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1080 | * |
||
| 1081 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1082 | * and onAfterWrite() won't get called either. |
||
| 1083 | * |
||
| 1084 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1085 | * attempting a write, and respond appropriately if it isn't. |
||
| 1086 | * |
||
| 1087 | * @see {@link ValidationResult} |
||
| 1088 | * @return ValidationResult |
||
| 1089 | */ |
||
| 1090 | protected function validate() { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Public accessor for {@see DataObject::validate()} |
||
| 1098 | * |
||
| 1099 | * @return ValidationResult |
||
| 1100 | */ |
||
| 1101 | public function doValidate() { |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Event handler called before writing to the database. |
||
| 1108 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1109 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1110 | * |
||
| 1111 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1112 | * |
||
| 1113 | * @uses DataExtension->onBeforeWrite() |
||
| 1114 | */ |
||
| 1115 | protected function onBeforeWrite() { |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Event handler called after writing to the database. |
||
| 1124 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1125 | * $this->changed will have a record |
||
| 1126 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1127 | * |
||
| 1128 | * @uses DataExtension->onAfterWrite() |
||
| 1129 | */ |
||
| 1130 | protected function onAfterWrite() { |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Event handler called before deleting from the database. |
||
| 1137 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1138 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1139 | * |
||
| 1140 | * @uses DataExtension->onBeforeDelete() |
||
| 1141 | */ |
||
| 1142 | protected function onBeforeDelete() { |
||
| 1148 | |||
| 1149 | protected function onAfterDelete() { |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Load the default values in from the self::$defaults array. |
||
| 1155 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1156 | * Called by the constructor when creating new records. |
||
| 1157 | * |
||
| 1158 | * @uses DataExtension->populateDefaults() |
||
| 1159 | * @return DataObject $this |
||
| 1160 | */ |
||
| 1161 | public function populateDefaults() { |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Determine validation of this object prior to write |
||
| 1195 | * |
||
| 1196 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1197 | */ |
||
| 1198 | protected function validateWrite() { |
||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Prepare an object prior to write |
||
| 1221 | * |
||
| 1222 | * @throws ValidationException |
||
| 1223 | */ |
||
| 1224 | protected function preWrite() { |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Detects and updates all changes made to this object |
||
| 1243 | * |
||
| 1244 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1245 | * @return bool True if any changes are detected |
||
| 1246 | */ |
||
| 1247 | protected function updateChanges($forceChanges = false) |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1261 | * |
||
| 1262 | * @param string $baseTable Base table |
||
| 1263 | * @param string $now Timestamp to use for the current time |
||
| 1264 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1265 | * @param array $manipulation Manipulation to write to |
||
| 1266 | * @param string $class Table and Class to select and write to |
||
| 1267 | */ |
||
| 1268 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1316 | * |
||
| 1317 | * Does nothing if an ID is already assigned for this record |
||
| 1318 | * |
||
| 1319 | * @param string $baseTable Base table |
||
| 1320 | * @param string $now Timestamp to use for the current time |
||
| 1321 | */ |
||
| 1322 | protected function writeBaseRecord($baseTable, $now) { |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * Generate and write the database manipulation for all changed fields |
||
| 1337 | * |
||
| 1338 | * @param string $baseTable Base table |
||
| 1339 | * @param string $now Timestamp to use for the current time |
||
| 1340 | * @param bool $isNewRecord If this is a new record |
||
| 1341 | */ |
||
| 1342 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Writes all changes to this object to the database. |
||
| 1366 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1367 | * - All relevant tables will be updated. |
||
| 1368 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1369 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1370 | * |
||
| 1371 | * @uses DataExtension->augmentWrite() |
||
| 1372 | * |
||
| 1373 | * @param boolean $showDebug Show debugging information |
||
| 1374 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1375 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1376 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1377 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1378 | * {@link getManyManyComponents()} (Default: false) |
||
| 1379 | * @return int The ID of the record |
||
| 1380 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1381 | */ |
||
| 1382 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Writes cached relation lists to the database, if possible |
||
| 1430 | */ |
||
| 1431 | public function writeRelations() { |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1445 | * same record. |
||
| 1446 | * |
||
| 1447 | * @param $recursive Recursively write components |
||
| 1448 | * @return DataObject $this |
||
| 1449 | */ |
||
| 1450 | public function writeComponents($recursive = false) { |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | * Delete this data object. |
||
| 1461 | * $this->onBeforeDelete() gets called. |
||
| 1462 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1463 | * @uses DataExtension->augmentSQL() |
||
| 1464 | */ |
||
| 1465 | public function delete() { |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Delete the record with the given ID. |
||
| 1496 | * |
||
| 1497 | * @param string $className The class name of the record to be deleted |
||
| 1498 | * @param int $id ID of record to be deleted |
||
| 1499 | */ |
||
| 1500 | public static function delete_by_id($className, $id) { |
||
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Get the class ancestry, including the current class name. |
||
| 1511 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1512 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1513 | * will be the current class. |
||
| 1514 | * |
||
| 1515 | * @return array Class ancestry |
||
| 1516 | */ |
||
| 1517 | public function getClassAncestry() { |
||
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1529 | * If no component is available, an 'empty component' will be returned for |
||
| 1530 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1531 | * |
||
| 1532 | * @param string $componentName Name of the component |
||
| 1533 | * |
||
| 1534 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1535 | */ |
||
| 1536 | public function getComponent($componentName) { |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Returns a one-to-many relation as a HasManyList |
||
| 1595 | * |
||
| 1596 | * @param string $componentName Name of the component |
||
| 1597 | * @param string|null $filter Deprecated. A filter to be inserted into the WHERE clause |
||
| 1598 | * @param string|null|array $sort Deprecated. A sort expression to be inserted into the ORDER BY clause. If omitted, |
||
| 1599 | * the static field $default_sort on the component class will be used. |
||
| 1600 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
| 1601 | * @param string|null|array $limit Deprecated. A limit expression to be inserted into the LIMIT clause |
||
| 1602 | * |
||
| 1603 | * @return HasManyList The components of the one-to-many relationship. |
||
| 1604 | */ |
||
| 1605 | public function getComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * @deprecated |
||
| 1652 | */ |
||
| 1653 | public function getComponentsQuery($componentName, $filter = "", $sort = "", $join = "", $limit = "") { |
||
| 1657 | |||
| 1658 | /** |
||
| 1659 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1660 | * |
||
| 1661 | * @param $relationName Relation name. |
||
| 1662 | * @return string Class name, or null if not found. |
||
| 1663 | */ |
||
| 1664 | public function getRelationClass($relationName) { |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * Tries to find the database key on another object that is used to store a |
||
| 1691 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
| 1692 | * |
||
| 1693 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
| 1694 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
| 1695 | * |
||
| 1696 | * @param string $component Name of the relation on the current object pointing to the |
||
| 1697 | * remote object. |
||
| 1698 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
| 1699 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
| 1700 | * @return string |
||
| 1701 | */ |
||
| 1702 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1773 | * @param string $componentName Name of the many-many component |
||
| 1774 | * @return ManyManyList The set of components |
||
| 1775 | * |
||
| 1776 | * @todo Implement query-params |
||
| 1777 | */ |
||
| 1778 | public function getManyManyComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * @deprecated 4.0 Method has been replaced by hasOne() and hasOneComponent() |
||
| 1821 | * @param string $component |
||
| 1822 | * @return array|null |
||
| 1823 | */ |
||
| 1824 | public function has_one($component = null) { |
||
| 1833 | |||
| 1834 | /** |
||
| 1835 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1836 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1837 | * |
||
| 1838 | * @param string $component Deprecated - Name of component |
||
| 1839 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1840 | * their classes. |
||
| 1841 | */ |
||
| 1842 | public function hasOne($component = null) { |
||
| 1854 | |||
| 1855 | /** |
||
| 1856 | * Return data for a specific has_one component. |
||
| 1857 | * @param string $component |
||
| 1858 | * @return string|null |
||
| 1859 | */ |
||
| 1860 | public function hasOneComponent($component) { |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * @deprecated 4.0 Method has been replaced by belongsTo() and belongsToComponent() |
||
| 1870 | * @param string $component |
||
| 1871 | * @param bool $classOnly |
||
| 1872 | * @return array|null |
||
| 1873 | */ |
||
| 1874 | public function belongs_to($component = null, $classOnly = true) { |
||
| 1883 | |||
| 1884 | /** |
||
| 1885 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1886 | * their class name will be returned. |
||
| 1887 | * |
||
| 1888 | * @param string $component - Name of component |
||
| 1889 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1890 | * the field data stripped off. It defaults to TRUE. |
||
| 1891 | * @return string|array |
||
| 1892 | */ |
||
| 1893 | public function belongsTo($component = null, $classOnly = true) { |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Return data for a specific belongs_to component. |
||
| 1913 | * @param string $component |
||
| 1914 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1915 | * the field data stripped off. It defaults to TRUE. |
||
| 1916 | * @return string|false |
||
| 1917 | */ |
||
| 1918 | public function belongsToComponent($component, $classOnly = true) { |
||
| 1929 | |||
| 1930 | /** |
||
| 1931 | * Return all of the database fields defined in self::$db and all the parent classes. |
||
| 1932 | * Doesn't include any fields specified by self::$has_one. Use $this->hasOne() to get these fields |
||
| 1933 | * |
||
| 1934 | * @param string $fieldName Limit the output to a specific field name |
||
| 1935 | * @return array The database fields |
||
| 1936 | */ |
||
| 1937 | public function db($fieldName = null) { |
||
| 1965 | |||
| 1966 | /** |
||
| 1967 | * @deprecated 4.0 Method has been replaced by hasMany() and hasManyComponent() |
||
| 1968 | * @param string $component |
||
| 1969 | * @param bool $classOnly |
||
| 1970 | * @return array|null |
||
| 1971 | */ |
||
| 1972 | public function has_many($component = null, $classOnly = true) { |
||
| 1981 | |||
| 1982 | /** |
||
| 1983 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 1984 | * relationships and their classes will be returned. |
||
| 1985 | * |
||
| 1986 | * @param string $component Deprecated - Name of component |
||
| 1987 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1988 | * the field data stripped off. It defaults to TRUE. |
||
| 1989 | * @return string|array|false |
||
| 1990 | */ |
||
| 1991 | public function hasMany($component = null, $classOnly = true) { |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Return data for a specific has_many component. |
||
| 2011 | * @param string $component |
||
| 2012 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2013 | * the field data stripped off. It defaults to TRUE. |
||
| 2014 | * @return string|false |
||
| 2015 | */ |
||
| 2016 | public function hasManyComponent($component, $classOnly = true) { |
||
| 2027 | |||
| 2028 | /** |
||
| 2029 | * @deprecated 4.0 Method has been replaced by manyManyExtraFields() and |
||
| 2030 | * manyManyExtraFieldsForComponent() |
||
| 2031 | * @param string $component |
||
| 2032 | * @return array |
||
| 2033 | */ |
||
| 2034 | public function many_many_extraFields($component = null) { |
||
| 2043 | |||
| 2044 | /** |
||
| 2045 | * Return the many-to-many extra fields specification. |
||
| 2046 | * |
||
| 2047 | * If you don't specify a component name, it returns all |
||
| 2048 | * extra fields for all components available. |
||
| 2049 | * |
||
| 2050 | * @param string $component Deprecated - Name of component |
||
| 2051 | * @return array|null |
||
| 2052 | */ |
||
| 2053 | public function manyManyExtraFields($component = null) { |
||
| 2066 | |||
| 2067 | /** |
||
| 2068 | * Return the many-to-many extra fields specification for a specific component. |
||
| 2069 | * @param string $component |
||
| 2070 | * @return array|null |
||
| 2071 | */ |
||
| 2072 | public function manyManyExtraFieldsForComponent($component) { |
||
| 2112 | |||
| 2113 | /** |
||
| 2114 | * @deprecated 4.0 Method has been renamed to manyMany() |
||
| 2115 | * @param string $component |
||
| 2116 | * @return array|null |
||
| 2117 | */ |
||
| 2118 | public function many_many($component = null) { |
||
| 2127 | |||
| 2128 | /** |
||
| 2129 | * Return information about a many-to-many component. |
||
| 2130 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 2131 | * components are returned. |
||
| 2132 | * |
||
| 2133 | * @see DataObject::manyManyComponent() |
||
| 2134 | * @param string $component Deprecated - Name of component |
||
| 2135 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 2136 | */ |
||
| 2137 | public function manyMany($component = null) { |
||
| 2153 | |||
| 2154 | /** |
||
| 2155 | * Return information about a specific many_many component. Returns a numeric array of: |
||
| 2156 | * array( |
||
| 2157 | * <classname>, The class that relation is defined in e.g. "Product" |
||
| 2158 | * <candidateName>, The target class of the relation e.g. "Category" |
||
| 2159 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
| 2160 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
| 2161 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
| 2162 | * ) |
||
| 2163 | * @param string $component The component name |
||
| 2164 | * @return array|null |
||
| 2165 | */ |
||
| 2166 | public function manyManyComponent($component) { |
||
| 2221 | |||
| 2222 | /** |
||
| 2223 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 2224 | * |
||
| 2225 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 2226 | * |
||
| 2227 | * @return array or false |
||
| 2228 | */ |
||
| 2229 | public function database_extensions($class){ |
||
| 2237 | |||
| 2238 | /** |
||
| 2239 | * Generates a SearchContext to be used for building and processing |
||
| 2240 | * a generic search form for properties on this object. |
||
| 2241 | * |
||
| 2242 | * @return SearchContext |
||
| 2243 | */ |
||
| 2244 | public function getDefaultSearchContext() { |
||
| 2251 | |||
| 2252 | /** |
||
| 2253 | * Determine which properties on the DataObject are |
||
| 2254 | * searchable, and map them to their default {@link FormField} |
||
| 2255 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 2256 | * |
||
| 2257 | * Some additional logic is included for switching field labels, based on |
||
| 2258 | * how generic or specific the field type is. |
||
| 2259 | * |
||
| 2260 | * Used by {@link SearchContext}. |
||
| 2261 | * |
||
| 2262 | * @param array $_params |
||
| 2263 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 2264 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 2265 | * @return FieldList |
||
| 2266 | */ |
||
| 2267 | public function scaffoldSearchFields($_params = null) { |
||
| 2314 | |||
| 2315 | /** |
||
| 2316 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2317 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2318 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2319 | * |
||
| 2320 | * @uses FormScaffolder |
||
| 2321 | * |
||
| 2322 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2323 | * @return FieldList |
||
| 2324 | */ |
||
| 2325 | public function scaffoldFormFields($_params = null) { |
||
| 2346 | |||
| 2347 | /** |
||
| 2348 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2349 | * being called on extensions |
||
| 2350 | * |
||
| 2351 | * @param callable $callback The callback to execute |
||
| 2352 | */ |
||
| 2353 | protected function beforeUpdateCMSFields($callback) { |
||
| 2356 | |||
| 2357 | /** |
||
| 2358 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2359 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2360 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2361 | * generate this set. To customize, overload this method in a subclass |
||
| 2362 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2363 | * |
||
| 2364 | * <code> |
||
| 2365 | * class MyCustomClass extends DataObject { |
||
| 2366 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2367 | * |
||
| 2368 | * function getCMSFields() { |
||
| 2369 | * $fields = parent::getCMSFields(); |
||
| 2370 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2371 | * return $fields; |
||
| 2372 | * } |
||
| 2373 | * } |
||
| 2374 | * </code> |
||
| 2375 | * |
||
| 2376 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2377 | * |
||
| 2378 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2379 | */ |
||
| 2380 | public function getCMSFields() { |
||
| 2392 | |||
| 2393 | /** |
||
| 2394 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2395 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2396 | * |
||
| 2397 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
| 2398 | */ |
||
| 2399 | public function getCMSActions() { |
||
| 2404 | |||
| 2405 | |||
| 2406 | /** |
||
| 2407 | * Used for simple frontend forms without relation editing |
||
| 2408 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2409 | * by default. To customize, either overload this method in your |
||
| 2410 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2411 | * |
||
| 2412 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2413 | * |
||
| 2414 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2415 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2416 | */ |
||
| 2417 | public function getFrontEndFields($params = null) { |
||
| 2423 | |||
| 2424 | /** |
||
| 2425 | * Gets the value of a field. |
||
| 2426 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2427 | * |
||
| 2428 | * @param string $field The name of the field |
||
| 2429 | * |
||
| 2430 | * @return mixed The field value |
||
| 2431 | */ |
||
| 2432 | public function getField($field) { |
||
| 2467 | |||
| 2468 | /** |
||
| 2469 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2470 | * |
||
| 2471 | * @param string $tableClass Base table to load the values from. Others are joined as required. |
||
| 2472 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2473 | * @return bool Flag if lazy loading succeeded |
||
| 2474 | */ |
||
| 2475 | protected function loadLazyFields($tableClass = null) { |
||
| 2546 | |||
| 2547 | /** |
||
| 2548 | * Return the fields that have changed. |
||
| 2549 | * |
||
| 2550 | * The change level affects what the functions defines as "changed": |
||
| 2551 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2552 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2553 | * for example a change from 0 to null would not be included. |
||
| 2554 | * |
||
| 2555 | * Example return: |
||
| 2556 | * <code> |
||
| 2557 | * array( |
||
| 2558 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2559 | * ) |
||
| 2560 | * </code> |
||
| 2561 | * |
||
| 2562 | * @param boolean $databaseFieldsOnly Get only database fields that have changed |
||
| 2563 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2564 | * @return array |
||
| 2565 | */ |
||
| 2566 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
| 2610 | |||
| 2611 | /** |
||
| 2612 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2613 | * since loading them from the database. |
||
| 2614 | * |
||
| 2615 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2616 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2617 | * @return boolean |
||
| 2618 | */ |
||
| 2619 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
| 2630 | |||
| 2631 | /** |
||
| 2632 | * Set the value of the field |
||
| 2633 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2634 | * |
||
| 2635 | * @param string $fieldName Name of the field |
||
| 2636 | * @param mixed $val New field value |
||
| 2637 | * @return DataObject $this |
||
| 2638 | */ |
||
| 2639 | public function setField($fieldName, $val) { |
||
| 2686 | |||
| 2687 | /** |
||
| 2688 | * Set the value of the field, using a casting object. |
||
| 2689 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2690 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2691 | * can be saved into the Image table. |
||
| 2692 | * |
||
| 2693 | * @param string $fieldName Name of the field |
||
| 2694 | * @param mixed $value New field value |
||
| 2695 | * @return DataObject $this |
||
| 2696 | */ |
||
| 2697 | public function setCastedField($fieldName, $val) { |
||
| 2711 | |||
| 2712 | /** |
||
| 2713 | * {@inheritdoc} |
||
| 2714 | */ |
||
| 2715 | public function castingHelper($field) { |
||
| 2733 | |||
| 2734 | /** |
||
| 2735 | * Returns true if the given field exists in a database column on any of |
||
| 2736 | * the objects tables and optionally look up a dynamic getter with |
||
| 2737 | * get<fieldName>(). |
||
| 2738 | * |
||
| 2739 | * @param string $field Name of the field |
||
| 2740 | * @return boolean True if the given field exists |
||
| 2741 | */ |
||
| 2742 | public function hasField($field) { |
||
| 2750 | |||
| 2751 | /** |
||
| 2752 | * Returns true if the given field exists as a database column |
||
| 2753 | * |
||
| 2754 | * @param string $field Name of the field |
||
| 2755 | * |
||
| 2756 | * @return boolean |
||
| 2757 | */ |
||
| 2758 | public function hasDatabaseField($field) { |
||
| 2763 | |||
| 2764 | /** |
||
| 2765 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2766 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2767 | * |
||
| 2768 | * @param string $field Name of the field |
||
| 2769 | * @return string The field type of the given field |
||
| 2770 | */ |
||
| 2771 | public function hasOwnTableDatabaseField($field) { |
||
| 2774 | |||
| 2775 | /** |
||
| 2776 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2777 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2778 | * |
||
| 2779 | * @param string $class Class name to check |
||
| 2780 | * @param string $field Name of the field |
||
| 2781 | * @return string The field type of the given field |
||
| 2782 | */ |
||
| 2783 | public static function has_own_table_database_field($class, $field) { |
||
| 2796 | |||
| 2797 | /** |
||
| 2798 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
| 2799 | * actually looking in the database. |
||
| 2800 | * |
||
| 2801 | * @param string $dataClass |
||
| 2802 | * @return bool |
||
| 2803 | */ |
||
| 2804 | public static function has_own_table($dataClass) { |
||
| 2819 | |||
| 2820 | /** |
||
| 2821 | * Returns true if the member is allowed to do the given action. |
||
| 2822 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2823 | * |
||
| 2824 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2825 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2826 | * in user. |
||
| 2827 | * |
||
| 2828 | * @return boolean True if the the member is allowed to do the given action |
||
| 2829 | */ |
||
| 2830 | public function can($perm, $member = null) { |
||
| 2889 | |||
| 2890 | /** |
||
| 2891 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2892 | * expected to return one of three values: |
||
| 2893 | * |
||
| 2894 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2895 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2896 | * - NULL: Don't affect the outcome |
||
| 2897 | * |
||
| 2898 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2899 | * |
||
| 2900 | * <code> |
||
| 2901 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2902 | * if($extended !== null) return $extended; |
||
| 2903 | * else return $normalValue; |
||
| 2904 | * </code> |
||
| 2905 | * |
||
| 2906 | * @param String $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2907 | * @param Member|int $member |
||
| 2908 | * @return boolean|null |
||
| 2909 | */ |
||
| 2910 | public function extendedCan($methodName, $member) { |
||
| 2921 | |||
| 2922 | /** |
||
| 2923 | * @param Member $member |
||
| 2924 | * @return boolean |
||
| 2925 | */ |
||
| 2926 | public function canView($member = null) { |
||
| 2933 | |||
| 2934 | /** |
||
| 2935 | * @param Member $member |
||
| 2936 | * @return boolean |
||
| 2937 | */ |
||
| 2938 | public function canEdit($member = null) { |
||
| 2945 | |||
| 2946 | /** |
||
| 2947 | * @param Member $member |
||
| 2948 | * @return boolean |
||
| 2949 | */ |
||
| 2950 | public function canDelete($member = null) { |
||
| 2957 | |||
| 2958 | /** |
||
| 2959 | * @todo Should canCreate be a static method? |
||
| 2960 | * |
||
| 2961 | * @param Member $member |
||
| 2962 | * @return boolean |
||
| 2963 | */ |
||
| 2964 | public function canCreate($member = null) { |
||
| 2971 | |||
| 2972 | /** |
||
| 2973 | * Debugging used by Debug::show() |
||
| 2974 | * |
||
| 2975 | * @return string HTML data representing this object |
||
| 2976 | */ |
||
| 2977 | public function debug() { |
||
| 2985 | |||
| 2986 | /** |
||
| 2987 | * Return the DBField object that represents the given field. |
||
| 2988 | * This works similarly to obj() with 2 key differences: |
||
| 2989 | * - it still returns an object even when the field has no value. |
||
| 2990 | * - it only matches fields and not methods |
||
| 2991 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 2992 | * |
||
| 2993 | * @param string $fieldName Name of the field |
||
| 2994 | * @return DBField The field as a DBField object |
||
| 2995 | */ |
||
| 2996 | public function dbObject($fieldName) { |
||
| 3031 | |||
| 3032 | /** |
||
| 3033 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 3034 | * |
||
| 3035 | * The path to the related field is specified with dot separated syntax |
||
| 3036 | * (eg: Parent.Child.Child.FieldName). |
||
| 3037 | * |
||
| 3038 | * @param string $fieldPath |
||
| 3039 | * |
||
| 3040 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 3041 | */ |
||
| 3042 | public function relObject($fieldPath) { |
||
| 3072 | |||
| 3073 | /** |
||
| 3074 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 3075 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 3076 | * |
||
| 3077 | * @param $fieldPath string |
||
| 3078 | * @return string | null - will return null on a missing value |
||
| 3079 | */ |
||
| 3080 | public function relField($fieldName) { |
||
| 3118 | |||
| 3119 | /** |
||
| 3120 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 3121 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 3122 | * |
||
| 3123 | * @return String |
||
| 3124 | */ |
||
| 3125 | public function getReverseAssociation($className) { |
||
| 3141 | |||
| 3142 | /** |
||
| 3143 | * Return all objects matching the filter |
||
| 3144 | * sub-classes are automatically selected and included |
||
| 3145 | * |
||
| 3146 | * @param string $callerClass The class of objects to be returned |
||
| 3147 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3148 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
| 3149 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 3150 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 3151 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 3152 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 3153 | * @param string $containerClass The container class to return the results in. |
||
| 3154 | * |
||
| 3155 | * @todo $containerClass is Ignored, why? |
||
| 3156 | * |
||
| 3157 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 3158 | */ |
||
| 3159 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
| 3196 | |||
| 3197 | |||
| 3198 | /** |
||
| 3199 | * @deprecated |
||
| 3200 | */ |
||
| 3201 | public function Aggregate($class = null) { |
||
| 3218 | |||
| 3219 | /** |
||
| 3220 | * @deprecated |
||
| 3221 | */ |
||
| 3222 | public function RelationshipAggregate($relationship) { |
||
| 3227 | |||
| 3228 | /** |
||
| 3229 | * Return the first item matching the given query. |
||
| 3230 | * All calls to get_one() are cached. |
||
| 3231 | * |
||
| 3232 | * @param string $callerClass The class of objects to be returned |
||
| 3233 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3234 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
| 3235 | * @param boolean $cache Use caching |
||
| 3236 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 3237 | * |
||
| 3238 | * @return DataObject The first item matching the query |
||
| 3239 | */ |
||
| 3240 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
| 3266 | |||
| 3267 | /** |
||
| 3268 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 3269 | * Also clears any cached aggregate data. |
||
| 3270 | * |
||
| 3271 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 3272 | * When false will just clear session-local cached data |
||
| 3273 | * @return DataObject $this |
||
| 3274 | */ |
||
| 3275 | public function flushCache($persistent = true) { |
||
| 3293 | |||
| 3294 | /** |
||
| 3295 | * Flush the get_one global cache and destroy associated objects. |
||
| 3296 | */ |
||
| 3297 | public static function flush_and_destroy_cache() { |
||
| 3305 | |||
| 3306 | /** |
||
| 3307 | * Reset all global caches associated with DataObject. |
||
| 3308 | */ |
||
| 3309 | public static function reset() { |
||
| 3320 | |||
| 3321 | /** |
||
| 3322 | * Return the given element, searching by ID |
||
| 3323 | * |
||
| 3324 | * @param string $callerClass The class of the object to be returned |
||
| 3325 | * @param int $id The id of the element |
||
| 3326 | * @param boolean $cache See {@link get_one()} |
||
| 3327 | * |
||
| 3328 | * @return DataObject The element |
||
| 3329 | */ |
||
| 3330 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
| 3347 | |||
| 3348 | /** |
||
| 3349 | * Get the name of the base table for this object |
||
| 3350 | */ |
||
| 3351 | public function baseTable() { |
||
| 3355 | |||
| 3356 | /** |
||
| 3357 | * @var Array Parameters used in the query that built this object. |
||
| 3358 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3359 | * run additional queries using the same context. |
||
| 3360 | */ |
||
| 3361 | protected $sourceQueryParams; |
||
| 3362 | |||
| 3363 | /** |
||
| 3364 | * @see $sourceQueryParams |
||
| 3365 | * @return array |
||
| 3366 | */ |
||
| 3367 | public function getSourceQueryParams() { |
||
| 3370 | |||
| 3371 | /** |
||
| 3372 | * @see $sourceQueryParams |
||
| 3373 | * @param array |
||
| 3374 | */ |
||
| 3375 | public function setSourceQueryParams($array) { |
||
| 3378 | |||
| 3379 | /** |
||
| 3380 | * @see $sourceQueryParams |
||
| 3381 | * @param array |
||
| 3382 | */ |
||
| 3383 | public function setSourceQueryParam($key, $value) { |
||
| 3386 | |||
| 3387 | /** |
||
| 3388 | * @see $sourceQueryParams |
||
| 3389 | * @return Mixed |
||
| 3390 | */ |
||
| 3391 | public function getSourceQueryParam($key) { |
||
| 3395 | |||
| 3396 | //-------------------------------------------------------------------------------------------// |
||
| 3397 | |||
| 3398 | /** |
||
| 3399 | * Return the database indexes on this table. |
||
| 3400 | * This array is indexed by the name of the field with the index, and |
||
| 3401 | * the value is the type of index. |
||
| 3402 | */ |
||
| 3403 | public function databaseIndexes() { |
||
| 3428 | |||
| 3429 | /** |
||
| 3430 | * Check the database schema and update it as necessary. |
||
| 3431 | * |
||
| 3432 | * @uses DataExtension->augmentDatabase() |
||
| 3433 | */ |
||
| 3434 | public function requireTable() { |
||
| 3479 | |||
| 3480 | /** |
||
| 3481 | * Validate that the configured relations for this class use the correct syntaxes |
||
| 3482 | * @throws LogicException |
||
| 3483 | */ |
||
| 3484 | protected function validateModelDefinitions() { |
||
| 3515 | |||
| 3516 | /** |
||
| 3517 | * Add default records to database. This function is called whenever the |
||
| 3518 | * database is built, after the database tables have all been created. Overload |
||
| 3519 | * this to add default records when the database is built, but make sure you |
||
| 3520 | * call parent::requireDefaultRecords(). |
||
| 3521 | * |
||
| 3522 | * @uses DataExtension->requireDefaultRecords() |
||
| 3523 | */ |
||
| 3524 | public function requireDefaultRecords() { |
||
| 3542 | |||
| 3543 | /** |
||
| 3544 | * Returns fields bu traversing the class heirachy in a bottom-up direction. |
||
| 3545 | * |
||
| 3546 | * Needed to avoid getCMSFields being empty when customDatabaseFields overlooks |
||
| 3547 | * the inheritance chain of the $db array, where a child data object has no $db array, |
||
| 3548 | * but still needs to know the properties of its parent. This should be merged into databaseFields or |
||
| 3549 | * customDatabaseFields. |
||
| 3550 | * |
||
| 3551 | * @todo review whether this is still needed after recent API changes |
||
| 3552 | */ |
||
| 3553 | public function inheritedDatabaseFields() { |
||
| 3564 | |||
| 3565 | /** |
||
| 3566 | * Get the default searchable fields for this object, as defined in the |
||
| 3567 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3568 | * data object, uses a default selection of summary fields. |
||
| 3569 | * |
||
| 3570 | * @return array |
||
| 3571 | */ |
||
| 3572 | public function searchableFields() { |
||
| 3646 | |||
| 3647 | /** |
||
| 3648 | * Get any user defined searchable fields labels that |
||
| 3649 | * exist. Allows overriding of default field names in the form |
||
| 3650 | * interface actually presented to the user. |
||
| 3651 | * |
||
| 3652 | * The reason for keeping this separate from searchable_fields, |
||
| 3653 | * which would be a logical place for this functionality, is to |
||
| 3654 | * avoid bloating and complicating the configuration array. Currently |
||
| 3655 | * much of this system is based on sensible defaults, and this property |
||
| 3656 | * would generally only be set in the case of more complex relationships |
||
| 3657 | * between data object being required in the search interface. |
||
| 3658 | * |
||
| 3659 | * Generates labels based on name of the field itself, if no static property |
||
| 3660 | * {@link self::field_labels} exists. |
||
| 3661 | * |
||
| 3662 | * @uses $field_labels |
||
| 3663 | * @uses FormField::name_to_label() |
||
| 3664 | * |
||
| 3665 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3666 | * |
||
| 3667 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3668 | */ |
||
| 3669 | public function fieldLabels($includerelations = true) { |
||
| 3704 | |||
| 3705 | /** |
||
| 3706 | * Get a human-readable label for a single field, |
||
| 3707 | * see {@link fieldLabels()} for more details. |
||
| 3708 | * |
||
| 3709 | * @uses fieldLabels() |
||
| 3710 | * @uses FormField::name_to_label() |
||
| 3711 | * |
||
| 3712 | * @param string $name Name of the field |
||
| 3713 | * @return string Label of the field |
||
| 3714 | */ |
||
| 3715 | public function fieldLabel($name) { |
||
| 3719 | |||
| 3720 | /** |
||
| 3721 | * Get the default summary fields for this object. |
||
| 3722 | * |
||
| 3723 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3724 | * |
||
| 3725 | * @return array |
||
| 3726 | */ |
||
| 3727 | public function summaryFields() { |
||
| 3760 | |||
| 3761 | /** |
||
| 3762 | * Defines a default list of filters for the search context. |
||
| 3763 | * |
||
| 3764 | * If a filter class mapping is defined on the data object, |
||
| 3765 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3766 | * {@link DBField} is used. |
||
| 3767 | * |
||
| 3768 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3769 | * |
||
| 3770 | * @return array |
||
| 3771 | */ |
||
| 3772 | public function defaultSearchFilters() { |
||
| 3793 | |||
| 3794 | /** |
||
| 3795 | * @return boolean True if the object is in the database |
||
| 3796 | */ |
||
| 3797 | public function isInDB() { |
||
| 3800 | |||
| 3801 | /* |
||
| 3802 | * @ignore |
||
| 3803 | */ |
||
| 3804 | private static $subclass_access = true; |
||
| 3805 | |||
| 3806 | /** |
||
| 3807 | * Temporarily disable subclass access in data object qeur |
||
| 3808 | */ |
||
| 3809 | public static function disable_subclass_access() { |
||
| 3815 | |||
| 3816 | //-------------------------------------------------------------------------------------------// |
||
| 3817 | |||
| 3818 | /** |
||
| 3819 | * Database field definitions. |
||
| 3820 | * This is a map from field names to field type. The field |
||
| 3821 | * type should be a class that extends . |
||
| 3822 | * @var array |
||
| 3823 | * @config |
||
| 3824 | */ |
||
| 3825 | private static $db = null; |
||
| 3826 | |||
| 3827 | /** |
||
| 3828 | * Use a casting object for a field. This is a map from |
||
| 3829 | * field name to class name of the casting object. |
||
| 3830 | * @var array |
||
| 3831 | */ |
||
| 3832 | private static $casting = array( |
||
| 3833 | "ID" => 'Int', |
||
| 3834 | "ClassName" => 'Varchar', |
||
| 3835 | "LastEdited" => "SS_Datetime", |
||
| 3836 | "Created" => "SS_Datetime", |
||
| 3837 | "Title" => 'Text', |
||
| 3838 | ); |
||
| 3839 | |||
| 3840 | /** |
||
| 3841 | * Specify custom options for a CREATE TABLE call. |
||
| 3842 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3843 | * All options have to be keyed for a specific database implementation, |
||
| 3844 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3845 | * |
||
| 3846 | * <code> |
||
| 3847 | * array( |
||
| 3848 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3849 | * ) |
||
| 3850 | * </code> |
||
| 3851 | * |
||
| 3852 | * Caution: This API is experimental, and might not be |
||
| 3853 | * included in the next major release. Please use with care. |
||
| 3854 | * |
||
| 3855 | * @var array |
||
| 3856 | * @config |
||
| 3857 | */ |
||
| 3858 | private static $create_table_options = array( |
||
| 3859 | 'MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3860 | ); |
||
| 3861 | |||
| 3862 | /** |
||
| 3863 | * If a field is in this array, then create a database index |
||
| 3864 | * on that field. This is a map from fieldname to index type. |
||
| 3865 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3866 | * |
||
| 3867 | * @var array |
||
| 3868 | * @config |
||
| 3869 | */ |
||
| 3870 | private static $indexes = null; |
||
| 3871 | |||
| 3872 | /** |
||
| 3873 | * Inserts standard column-values when a DataObject |
||
| 3874 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3875 | * This is a map from fieldname to default value. |
||
| 3876 | * |
||
| 3877 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3878 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3879 | * or false in your subclass. Setting it to null won't work. |
||
| 3880 | * |
||
| 3881 | * @var array |
||
| 3882 | * @config |
||
| 3883 | */ |
||
| 3884 | private static $defaults = null; |
||
| 3885 | |||
| 3886 | /** |
||
| 3887 | * Multidimensional array which inserts default data into the database |
||
| 3888 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3889 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3890 | * behaviour such as publishing and ParentNodes. |
||
| 3891 | * |
||
| 3892 | * Example: |
||
| 3893 | * array( |
||
| 3894 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3895 | * array('Title' => "DefaultPage2") |
||
| 3896 | * ). |
||
| 3897 | * |
||
| 3898 | * @var array |
||
| 3899 | * @config |
||
| 3900 | */ |
||
| 3901 | private static $default_records = null; |
||
| 3902 | |||
| 3903 | /** |
||
| 3904 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3905 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3906 | * |
||
| 3907 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3908 | * |
||
| 3909 | * @var array |
||
| 3910 | * @config |
||
| 3911 | */ |
||
| 3912 | private static $has_one = null; |
||
| 3913 | |||
| 3914 | /** |
||
| 3915 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3916 | * |
||
| 3917 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3918 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3919 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3920 | * |
||
| 3921 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3922 | * |
||
| 3923 | * @var array |
||
| 3924 | * @config |
||
| 3925 | */ |
||
| 3926 | private static $belongs_to; |
||
| 3927 | |||
| 3928 | /** |
||
| 3929 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3930 | * |
||
| 3931 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3932 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3933 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3934 | * which foreign key to use. |
||
| 3935 | * |
||
| 3936 | * @var array |
||
| 3937 | * @config |
||
| 3938 | */ |
||
| 3939 | private static $has_many = null; |
||
| 3940 | |||
| 3941 | /** |
||
| 3942 | * many-many relationship definitions. |
||
| 3943 | * This is a map from component name to data type. |
||
| 3944 | * @var array |
||
| 3945 | * @config |
||
| 3946 | */ |
||
| 3947 | private static $many_many = null; |
||
| 3948 | |||
| 3949 | /** |
||
| 3950 | * Extra fields to include on the connecting many-many table. |
||
| 3951 | * This is a map from field name to field type. |
||
| 3952 | * |
||
| 3953 | * Example code: |
||
| 3954 | * <code> |
||
| 3955 | * public static $many_many_extraFields = array( |
||
| 3956 | * 'Members' => array( |
||
| 3957 | * 'Role' => 'Varchar(100)' |
||
| 3958 | * ) |
||
| 3959 | * ); |
||
| 3960 | * </code> |
||
| 3961 | * |
||
| 3962 | * @var array |
||
| 3963 | * @config |
||
| 3964 | */ |
||
| 3965 | private static $many_many_extraFields = null; |
||
| 3966 | |||
| 3967 | /** |
||
| 3968 | * The inverse side of a many-many relationship. |
||
| 3969 | * This is a map from component name to data type. |
||
| 3970 | * @var array |
||
| 3971 | * @config |
||
| 3972 | */ |
||
| 3973 | private static $belongs_many_many = null; |
||
| 3974 | |||
| 3975 | /** |
||
| 3976 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3977 | * clause of a SQL query if no other sort expression is provided. |
||
| 3978 | * @var string |
||
| 3979 | * @config |
||
| 3980 | */ |
||
| 3981 | private static $default_sort = null; |
||
| 3982 | |||
| 3983 | /** |
||
| 3984 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3985 | * search interface. |
||
| 3986 | * |
||
| 3987 | * Overriding the default filter, with a custom defined filter: |
||
| 3988 | * <code> |
||
| 3989 | * static $searchable_fields = array( |
||
| 3990 | * "Name" => "PartialMatchFilter" |
||
| 3991 | * ); |
||
| 3992 | * </code> |
||
| 3993 | * |
||
| 3994 | * Overriding the default form fields, with a custom defined field. |
||
| 3995 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3996 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3997 | * <code> |
||
| 3998 | * static $searchable_fields = array( |
||
| 3999 | * "Name" => array( |
||
| 4000 | * "field" => "TextField" |
||
| 4001 | * ) |
||
| 4002 | * ); |
||
| 4003 | * </code> |
||
| 4004 | * |
||
| 4005 | * Overriding the default form field, filter and title: |
||
| 4006 | * <code> |
||
| 4007 | * static $searchable_fields = array( |
||
| 4008 | * "Organisation.ZipCode" => array( |
||
| 4009 | * "field" => "TextField", |
||
| 4010 | * "filter" => "PartialMatchFilter", |
||
| 4011 | * "title" => 'Organisation ZIP' |
||
| 4012 | * ) |
||
| 4013 | * ); |
||
| 4014 | * </code> |
||
| 4015 | * @config |
||
| 4016 | */ |
||
| 4017 | private static $searchable_fields = null; |
||
| 4018 | |||
| 4019 | /** |
||
| 4020 | * User defined labels for searchable_fields, used to override |
||
| 4021 | * default display in the search form. |
||
| 4022 | * @config |
||
| 4023 | */ |
||
| 4024 | private static $field_labels = null; |
||
| 4025 | |||
| 4026 | /** |
||
| 4027 | * Provides a default list of fields to be used by a 'summary' |
||
| 4028 | * view of this object. |
||
| 4029 | * @config |
||
| 4030 | */ |
||
| 4031 | private static $summary_fields = null; |
||
| 4032 | |||
| 4033 | /** |
||
| 4034 | * Provides a list of allowed methods that can be called via RESTful api. |
||
| 4035 | */ |
||
| 4036 | public static $allowed_actions = null; |
||
| 4037 | |||
| 4038 | /** |
||
| 4039 | * Collect all static properties on the object |
||
| 4040 | * which contain natural language, and need to be translated. |
||
| 4041 | * The full entity name is composed from the class name and a custom identifier. |
||
| 4042 | * |
||
| 4043 | * @return array A numerical array which contains one or more entities in array-form. |
||
| 4044 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
| 4045 | * $entity, $string, $priority, $context. |
||
| 4046 | */ |
||
| 4047 | public function provideI18nEntities() { |
||
| 4065 | |||
| 4066 | /** |
||
| 4067 | * Returns true if the given method/parameter has a value |
||
| 4068 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 4069 | * |
||
| 4070 | * @param string $field The field name |
||
| 4071 | * @param array $arguments |
||
| 4072 | * @param bool $cache |
||
| 4073 | * @return boolean |
||
| 4074 | */ |
||
| 4075 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 4083 | |||
| 4084 | } |
||
| 4085 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.