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 |
||
| 82 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Human-readable singular name. |
||
| 86 | * @var string |
||
| 87 | * @config |
||
| 88 | */ |
||
| 89 | private static $singular_name = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Human-readable plural name |
||
| 93 | * @var string |
||
| 94 | * @config |
||
| 95 | */ |
||
| 96 | private static $plural_name = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Allow API access to this object? |
||
| 100 | * @todo Define the options that can be set here |
||
| 101 | * @config |
||
| 102 | */ |
||
| 103 | private static $api_access = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Allows specification of a default value for the ClassName field. |
||
| 107 | * Configure this value only in subclasses of DataObject. |
||
| 108 | * |
||
| 109 | * @config |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private static $default_classname = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * True if this DataObject has been destroyed. |
||
| 116 | * @var boolean |
||
| 117 | */ |
||
| 118 | public $destroyed = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The DataModel from this this object comes |
||
| 122 | */ |
||
| 123 | protected $model; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 127 | * |
||
| 128 | * Use {@link toMap()} if you want an array representation |
||
| 129 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $record; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 137 | */ |
||
| 138 | const CHANGE_NONE = 0; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 142 | * (before !== after && before == after) |
||
| 143 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 144 | * Value changes are by nature also considered strict changes. |
||
| 145 | */ |
||
| 146 | const CHANGE_STRICT = 1; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Represents a field that has changed the loosely defined value |
||
| 150 | * (before != after, thus, before !== after)) |
||
| 151 | * E.g. change false to true, but not false to 0 |
||
| 152 | */ |
||
| 153 | const CHANGE_VALUE = 2; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * An array indexed by fieldname, true if the field has been changed. |
||
| 157 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 158 | * the changed state. |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private $changed; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The database record (in the same format as $record), before |
||
| 166 | * any changes. |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | protected $original; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 173 | * @var boolean |
||
| 174 | */ |
||
| 175 | protected $brokenOnDelete = false; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 179 | * @var boolean |
||
| 180 | */ |
||
| 181 | protected $brokenOnWrite = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @config |
||
| 185 | * @var boolean Should dataobjects be validated before they are written? |
||
| 186 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 187 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 188 | * to only disable validation for very specific use cases. |
||
| 189 | */ |
||
| 190 | private static $validation_enabled = true; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Static caches used by relevant functions. |
||
| 194 | */ |
||
| 195 | protected static $_cache_has_own_table = array(); |
||
| 196 | protected static $_cache_get_one; |
||
| 197 | protected static $_cache_get_class_ancestry; |
||
| 198 | protected static $_cache_composite_fields = array(); |
||
| 199 | protected static $_cache_database_fields = array(); |
||
| 200 | protected static $_cache_field_labels = array(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Base fields which are not defined in static $db |
||
| 204 | * |
||
| 205 | * @config |
||
| 206 | * @var array |
||
| 207 | */ |
||
| 208 | private static $fixed_fields = array( |
||
| 209 | 'ID' => 'PrimaryKey', |
||
| 210 | 'ClassName' => 'DBClassName', |
||
| 211 | 'LastEdited' => 'SS_Datetime', |
||
| 212 | 'Created' => 'SS_Datetime', |
||
| 213 | ); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Core dataobject extensions |
||
| 217 | * |
||
| 218 | * @config |
||
| 219 | * @var array |
||
| 220 | */ |
||
| 221 | private static $extensions = array( |
||
| 222 | 'AssetControl' => '\\SilverStripe\\Filesystem\\AssetControlExtension' |
||
| 223 | ); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Non-static relationship cache, indexed by component name. |
||
| 227 | */ |
||
| 228 | protected $components; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 232 | */ |
||
| 233 | protected $unsavedRelations; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
| 237 | * "ID" will be included on every table. |
||
| 238 | * |
||
| 239 | * Composite DB field specifications are returned by reference if necessary, but not in the return |
||
| 240 | * array. |
||
| 241 | * |
||
| 242 | * Can be called directly on an object. E.g. Member::database_fields() |
||
| 243 | * |
||
| 244 | * @param string $class Class name to query from |
||
| 245 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 246 | */ |
||
| 247 | public static function database_fields($class = null) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Cache all database and composite fields for the given class. |
||
| 261 | * Will do nothing if already cached |
||
| 262 | * |
||
| 263 | * @param string $class Class name to cache |
||
| 264 | */ |
||
| 265 | protected static function cache_database_fields($class) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
| 323 | * and {@link DataObject::$has_one}. Resolves instances of {@link DBComposite} |
||
| 324 | * into the actual database fields, rather than the name of the field which |
||
| 325 | * might not equate a database column. |
||
| 326 | * |
||
| 327 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
| 328 | * see {@link database_fields()}. |
||
| 329 | * |
||
| 330 | * Can be called directly on an object. E.g. Member::custom_database_fields() |
||
| 331 | * |
||
| 332 | * @uses DBComposite->compositeDatabaseFields() |
||
| 333 | * |
||
| 334 | * @param string $class Class name to query from |
||
| 335 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 336 | */ |
||
| 337 | public static function custom_database_fields($class = null) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns the field class if the given db field on the class is a composite field. |
||
| 352 | * Will check all applicable ancestor classes and aggregate results. |
||
| 353 | * |
||
| 354 | * @param string $class Class to check |
||
| 355 | * @param string $name Field to check |
||
| 356 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
| 357 | * @return string|false Class spec name of composite field if it exists, or false if not |
||
| 358 | */ |
||
| 359 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 366 | * Will check all applicable ancestor classes and aggregate results. |
||
| 367 | * |
||
| 368 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
| 369 | * to aggregate. |
||
| 370 | * |
||
| 371 | * Includes composite has_one (Polymorphic) fields |
||
| 372 | * |
||
| 373 | * @param string $class Name of class to check |
||
| 374 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 375 | * @return array List of composite fields and their class spec |
||
| 376 | */ |
||
| 377 | public static function composite_fields($class = null, $aggregated = true) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Construct a new DataObject. |
||
| 404 | * |
||
| 405 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
| 406 | * field values. Normally this contructor is only used by the internal systems that get objects from the database. |
||
| 407 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 408 | * Singletons don't have their defaults set. |
||
| 409 | * @param DataModel $model |
||
| 410 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 411 | */ |
||
| 412 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set the DataModel |
||
| 488 | * @param DataModel $model |
||
| 489 | * @return DataObject $this |
||
| 490 | */ |
||
| 491 | public function setDataModel(DataModel $model) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Destroy all of this objects dependant objects and local caches. |
||
| 498 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 499 | */ |
||
| 500 | public function destroy() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create a duplicate of this node. |
||
| 508 | * Note: now also duplicates relations. |
||
| 509 | * |
||
| 510 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 511 | * If this is true, it will create the duplicate in the database. |
||
| 512 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 513 | */ |
||
| 514 | public function duplicate($doWrite = true) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
| 531 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
| 532 | * automatically when adding the new relations. |
||
| 533 | * |
||
| 534 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 535 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 536 | * @return DataObject with the new many_many relations copied in |
||
| 537 | */ |
||
| 538 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Helper function to duplicate relations from one object to another |
||
| 561 | * @param $sourceObject the source object to duplicate from |
||
| 562 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
| 563 | * @param $name the name of the relation to duplicate (e.g. members) |
||
| 564 | */ |
||
| 565 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
| 579 | |||
| 580 | public function getObsoleteClassName() { |
||
| 584 | |||
| 585 | public function getClassName() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 593 | * Warning: This will produce an inconsistent record, as the object |
||
| 594 | * instance will not automatically switch to the new subclass. |
||
| 595 | * Please use {@link newClassInstance()} for this purpose, |
||
| 596 | * or destroy and reinstanciate the record. |
||
| 597 | * |
||
| 598 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 599 | * @return DataObject $this |
||
| 600 | */ |
||
| 601 | public function setClassName($className) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Create a new instance of a different class from this object's record. |
||
| 612 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 613 | * it ensures that the instance of the class is a match for the className of the |
||
| 614 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 615 | * property manually before calling this method, as it will confuse change detection. |
||
| 616 | * |
||
| 617 | * If the new class is different to the original class, defaults are populated again |
||
| 618 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 619 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 620 | * we still need to repopulate the defaults. |
||
| 621 | * |
||
| 622 | * @param string $newClassName The name of the new class |
||
| 623 | * |
||
| 624 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 625 | */ |
||
| 626 | public function newClassInstance($newClassName) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Adds methods from the extensions. |
||
| 647 | * Called by Object::__construct() once per class. |
||
| 648 | */ |
||
| 649 | public function defineMethods() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 691 | * The default behaviour for a DataObject is to return true if |
||
| 692 | * the object exists in the database, you can override this in subclasses. |
||
| 693 | * |
||
| 694 | * @return boolean true if this object exists |
||
| 695 | */ |
||
| 696 | public function exists() { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns TRUE if all values (other than "ID") are |
||
| 702 | * considered empty (by weak boolean comparison). |
||
| 703 | * |
||
| 704 | * @return boolean |
||
| 705 | */ |
||
| 706 | public function isEmpty() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Get the user friendly singular name of this DataObject. |
||
| 727 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 728 | * this returns the class name. |
||
| 729 | * |
||
| 730 | * @return string User friendly singular name of this DataObject |
||
| 731 | */ |
||
| 732 | public function singular_name() { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Get the translated user friendly singular name of this DataObject |
||
| 742 | * same as singular_name() but runs it through the translating function |
||
| 743 | * |
||
| 744 | * Translating string is in the form: |
||
| 745 | * $this->class.SINGULARNAME |
||
| 746 | * Example: |
||
| 747 | * Page.SINGULARNAME |
||
| 748 | * |
||
| 749 | * @return string User friendly translated singular name of this DataObject |
||
| 750 | */ |
||
| 751 | public function i18n_singular_name() { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Get the user friendly plural name of this DataObject |
||
| 757 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 758 | * this returns a pluralised version of the class name. |
||
| 759 | * |
||
| 760 | * @return string User friendly plural name of this DataObject |
||
| 761 | */ |
||
| 762 | public function plural_name() { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get the translated user friendly plural name of this DataObject |
||
| 777 | * Same as plural_name but runs it through the translation function |
||
| 778 | * Translation string is in the form: |
||
| 779 | * $this->class.PLURALNAME |
||
| 780 | * Example: |
||
| 781 | * Page.PLURALNAME |
||
| 782 | * |
||
| 783 | * @return string User friendly translated plural name of this DataObject |
||
| 784 | */ |
||
| 785 | public function i18n_plural_name() |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Standard implementation of a title/label for a specific |
||
| 793 | * record. Tries to find properties 'Title' or 'Name', |
||
| 794 | * and falls back to the 'ID'. Useful to provide |
||
| 795 | * user-friendly identification of a record, e.g. in errormessages |
||
| 796 | * or UI-selections. |
||
| 797 | * |
||
| 798 | * Overload this method to have a more specialized implementation, |
||
| 799 | * e.g. for an Address record this could be: |
||
| 800 | * <code> |
||
| 801 | * function getTitle() { |
||
| 802 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 803 | * } |
||
| 804 | * </code> |
||
| 805 | * |
||
| 806 | * @return string |
||
| 807 | */ |
||
| 808 | public function getTitle() { |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Returns the associated database record - in this case, the object itself. |
||
| 817 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 818 | * |
||
| 819 | * @return DataObject Associated database record |
||
| 820 | */ |
||
| 821 | public function data() { |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Convert this object to a map. |
||
| 827 | * |
||
| 828 | * @return array The data as a map. |
||
| 829 | */ |
||
| 830 | public function toMap() { |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Return all currently fetched database fields. |
||
| 837 | * |
||
| 838 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 839 | * Obviously, this makes it a lot faster. |
||
| 840 | * |
||
| 841 | * @return array The data as a map. |
||
| 842 | */ |
||
| 843 | public function getQueriedDatabaseFields() { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 849 | * |
||
| 850 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 851 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 852 | * |
||
| 853 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 854 | * the related objects that it alters. |
||
| 855 | * |
||
| 856 | * @param array $data A map of field name to data values to update. |
||
| 857 | * @return DataObject $this |
||
| 858 | */ |
||
| 859 | public function update($data) { |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Pass changes as a map, and try to |
||
| 909 | * get automatic casting for these fields. |
||
| 910 | * Doesn't write to the database. To write the data, |
||
| 911 | * use the write() method. |
||
| 912 | * |
||
| 913 | * @param array $data A map of field name to data values to update. |
||
| 914 | * @return DataObject $this |
||
| 915 | */ |
||
| 916 | public function castedUpdate($data) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Merges data and relations from another object of same class, |
||
| 925 | * without conflict resolution. Allows to specify which |
||
| 926 | * dataset takes priority in case its not empty. |
||
| 927 | * has_one-relations are just transferred with priority 'right'. |
||
| 928 | * has_many and many_many-relations are added regardless of priority. |
||
| 929 | * |
||
| 930 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 931 | * meaning they are not connected to the merged object any longer. |
||
| 932 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 933 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 934 | * Caution: Does not delete the merged object. |
||
| 935 | * Caution: Does now overwrite Created date on the original object. |
||
| 936 | * |
||
| 937 | * @param $obj DataObject |
||
| 938 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
| 939 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
| 940 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
| 941 | * Only applicable with $priority='right'. (optional) |
||
| 942 | * @return Boolean |
||
| 943 | */ |
||
| 944 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * Forces the record to think that all its data has changed. |
||
| 1020 | * Doesn't write to the database. Only sets fields as changed |
||
| 1021 | * if they are not already marked as changed. |
||
| 1022 | * |
||
| 1023 | * @return DataObject $this |
||
| 1024 | */ |
||
| 1025 | public function forceChange() { |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Validate the current object. |
||
| 1048 | * |
||
| 1049 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1050 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1051 | * |
||
| 1052 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1053 | * and onAfterWrite() won't get called either. |
||
| 1054 | * |
||
| 1055 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1056 | * attempting a write, and respond appropriately if it isn't. |
||
| 1057 | * |
||
| 1058 | * @see {@link ValidationResult} |
||
| 1059 | * @return ValidationResult |
||
| 1060 | */ |
||
| 1061 | public function validate() { |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Public accessor for {@see DataObject::validate()} |
||
| 1069 | * |
||
| 1070 | * @return ValidationResult |
||
| 1071 | */ |
||
| 1072 | public function doValidate() { |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Event handler called before writing to the database. |
||
| 1079 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1080 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1081 | * |
||
| 1082 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1083 | * |
||
| 1084 | * @uses DataExtension->onBeforeWrite() |
||
| 1085 | */ |
||
| 1086 | protected function onBeforeWrite() { |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Event handler called after writing to the database. |
||
| 1095 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1096 | * $this->changed will have a record |
||
| 1097 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1098 | * |
||
| 1099 | * @uses DataExtension->onAfterWrite() |
||
| 1100 | */ |
||
| 1101 | protected function onAfterWrite() { |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Event handler called before deleting from the database. |
||
| 1108 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1109 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1110 | * |
||
| 1111 | * @uses DataExtension->onBeforeDelete() |
||
| 1112 | */ |
||
| 1113 | protected function onBeforeDelete() { |
||
| 1119 | |||
| 1120 | protected function onAfterDelete() { |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Load the default values in from the self::$defaults array. |
||
| 1126 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1127 | * Called by the constructor when creating new records. |
||
| 1128 | * |
||
| 1129 | * @uses DataExtension->populateDefaults() |
||
| 1130 | * @return DataObject $this |
||
| 1131 | */ |
||
| 1132 | public function populateDefaults() { |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Determine validation of this object prior to write |
||
| 1166 | * |
||
| 1167 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1168 | */ |
||
| 1169 | protected function validateWrite() { |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Prepare an object prior to write |
||
| 1192 | * |
||
| 1193 | * @throws ValidationException |
||
| 1194 | */ |
||
| 1195 | protected function preWrite() { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Detects and updates all changes made to this object |
||
| 1214 | * |
||
| 1215 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1216 | * @return bool True if any changes are detected |
||
| 1217 | */ |
||
| 1218 | protected function updateChanges($forceChanges = false) { |
||
| 1235 | |||
| 1236 | /** |
||
| 1237 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1238 | * |
||
| 1239 | * @param string $baseTable Base table |
||
| 1240 | * @param string $now Timestamp to use for the current time |
||
| 1241 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1242 | * @param array $manipulation Manipulation to write to |
||
| 1243 | * @param string $class Table and Class to select and write to |
||
| 1244 | */ |
||
| 1245 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1292 | * |
||
| 1293 | * Does nothing if an ID is already assigned for this record |
||
| 1294 | * |
||
| 1295 | * @param string $baseTable Base table |
||
| 1296 | * @param string $now Timestamp to use for the current time |
||
| 1297 | */ |
||
| 1298 | protected function writeBaseRecord($baseTable, $now) { |
||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * Generate and write the database manipulation for all changed fields |
||
| 1313 | * |
||
| 1314 | * @param string $baseTable Base table |
||
| 1315 | * @param string $now Timestamp to use for the current time |
||
| 1316 | * @param bool $isNewRecord If this is a new record |
||
| 1317 | */ |
||
| 1318 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Writes all changes to this object to the database. |
||
| 1342 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1343 | * - All relevant tables will be updated. |
||
| 1344 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1345 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1346 | * |
||
| 1347 | * @uses DataExtension->augmentWrite() |
||
| 1348 | * |
||
| 1349 | * @param boolean $showDebug Show debugging information |
||
| 1350 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1351 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1352 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1353 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1354 | * {@link getManyManyComponents()} (Default: false) |
||
| 1355 | * @return int The ID of the record |
||
| 1356 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1357 | */ |
||
| 1358 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * Writes cached relation lists to the database, if possible |
||
| 1406 | */ |
||
| 1407 | public function writeRelations() { |
||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1421 | * same record. |
||
| 1422 | * |
||
| 1423 | * @param $recursive Recursively write components |
||
| 1424 | * @return DataObject $this |
||
| 1425 | */ |
||
| 1426 | public function writeComponents($recursive = false) { |
||
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Delete this data object. |
||
| 1437 | * $this->onBeforeDelete() gets called. |
||
| 1438 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1439 | * @uses DataExtension->augmentSQL() |
||
| 1440 | */ |
||
| 1441 | public function delete() { |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * Delete the record with the given ID. |
||
| 1472 | * |
||
| 1473 | * @param string $className The class name of the record to be deleted |
||
| 1474 | * @param int $id ID of record to be deleted |
||
| 1475 | */ |
||
| 1476 | public static function delete_by_id($className, $id) { |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Get the class ancestry, including the current class name. |
||
| 1487 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1488 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1489 | * will be the current class. |
||
| 1490 | * |
||
| 1491 | * @return array Class ancestry |
||
| 1492 | */ |
||
| 1493 | public function getClassAncestry() { |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1505 | * If no component is available, an 'empty component' will be returned for |
||
| 1506 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1507 | * |
||
| 1508 | * @param string $componentName Name of the component |
||
| 1509 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1510 | * @throws Exception |
||
| 1511 | */ |
||
| 1512 | public function getComponent($componentName) { |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Returns a one-to-many relation as a HasManyList |
||
| 1583 | * |
||
| 1584 | * @param string $componentName Name of the component |
||
| 1585 | * @return HasManyList The components of the one-to-many relationship. |
||
| 1586 | */ |
||
| 1587 | public function getComponents($componentName) { |
||
| 1625 | |||
| 1626 | /** |
||
| 1627 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1628 | * |
||
| 1629 | * @param string $relationName Relation name. |
||
| 1630 | * @return string Class name, or null if not found. |
||
| 1631 | */ |
||
| 1632 | public function getRelationClass($relationName) { |
||
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Given a relation name, determine the relation type |
||
| 1659 | * |
||
| 1660 | * @param string $component Name of component |
||
| 1661 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 1662 | */ |
||
| 1663 | public function getRelationType($component) { |
||
| 1673 | |||
| 1674 | /** |
||
| 1675 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 1676 | * side of the relation. |
||
| 1677 | * |
||
| 1678 | * Notes on behaviour: |
||
| 1679 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 1680 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 1681 | * - Cannot be used on polymorphic relationships |
||
| 1682 | * - Cannot be used on unsaved objects. |
||
| 1683 | * |
||
| 1684 | * @param string $remoteClass |
||
| 1685 | * @param string $remoteRelation |
||
| 1686 | * @return DataList|DataObject The component, either as a list or single object |
||
| 1687 | * @throws BadMethodCallException |
||
| 1688 | * @throws InvalidArgumentException |
||
| 1689 | */ |
||
| 1690 | public function inferReciprocalComponent($remoteClass, $remoteRelation) { |
||
| 1691 | /** @var DataObject $remote */ |
||
| 1692 | $remote = $remoteClass::singleton(); |
||
| 1693 | $class = $remote->getRelationClass($remoteRelation); |
||
| 1694 | |||
| 1695 | // Validate arguments |
||
| 1696 | if(!$this->isInDB()) { |
||
| 1697 | throw new BadMethodCallException(__METHOD__ . " cannot be called on unsaved objects"); |
||
| 1698 | } |
||
| 1699 | if(empty($class)) { |
||
| 1700 | throw new InvalidArgumentException(sprintf( |
||
| 1701 | "%s invoked with invalid relation %s.%s", |
||
| 1702 | __METHOD__, |
||
| 1703 | $remoteClass, |
||
| 1704 | $remoteRelation |
||
| 1705 | )); |
||
| 1706 | } |
||
| 1707 | if($class === 'DataObject') { |
||
| 1708 | throw new InvalidArgumentException(sprintf( |
||
| 1709 | "%s cannot generate opposite component of relation %s.%s as it is polymorphic. " . |
||
| 1710 | "This method does not support polymorphic relationships", |
||
| 1711 | __METHOD__, |
||
| 1712 | $remoteClass, |
||
| 1713 | $remoteRelation |
||
| 1714 | )); |
||
| 1715 | } |
||
| 1716 | if(!is_a($this, $class, true)) { |
||
| 1717 | throw new InvalidArgumentException(sprintf( |
||
| 1718 | "Relation %s on %s does not refer to objects of type %s", |
||
| 1719 | $remoteRelation, $remoteClass, get_class($this) |
||
| 1720 | )); |
||
| 1721 | } |
||
| 1722 | |||
| 1723 | // Check the relation type to mock |
||
| 1724 | $relationType = $remote->getRelationType($remoteRelation); |
||
| 1725 | switch($relationType) { |
||
| 1726 | case 'has_one': { |
||
| 1727 | // Mock has_many |
||
| 1728 | $joinField = "{$remoteRelation}ID"; |
||
| 1729 | $componentClass = ClassInfo::table_for_object_field($remoteClass, $joinField); |
||
| 1730 | $result = HasManyList::create($componentClass, $joinField); |
||
| 1731 | if ($this->model) { |
||
| 1732 | $result->setDataModel($this->model); |
||
| 1733 | } |
||
| 1734 | return $result |
||
| 1735 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 1736 | ->forForeignID($this->ID); |
||
| 1737 | } |
||
| 1738 | case 'belongs_to': |
||
| 1739 | case 'has_many': { |
||
| 1740 | // These relations must have a has_one on the other end, so find it |
||
| 1741 | $joinField = $remote->getRemoteJoinField($remoteRelation, $relationType, $polymorphic); |
||
| 1742 | if ($polymorphic) { |
||
| 1743 | throw new InvalidArgumentException(sprintf( |
||
| 1744 | "%s cannot generate opposite component of relation %s.%s, as the other end appears" . |
||
| 1745 | "to be a has_one polymorphic. This method does not support polymorphic relationships", |
||
| 1746 | __METHOD__, |
||
| 1747 | $remoteClass, |
||
| 1748 | $remoteRelation |
||
| 1749 | )); |
||
| 1750 | } |
||
| 1751 | $joinID = $this->getField($joinField); |
||
| 1752 | if (empty($joinID)) { |
||
| 1753 | return null; |
||
| 1754 | } |
||
| 1755 | // Get object by joined ID |
||
| 1756 | return DataObject::get($remoteClass) |
||
| 1757 | ->filter('ID', $joinID) |
||
| 1758 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 1759 | ->first(); |
||
| 1760 | } |
||
| 1761 | case 'many_many': |
||
| 1762 | case 'belongs_many_many': { |
||
| 1763 | // Get components and extra fields from parent |
||
| 1764 | list($componentClass, $parentClass, $componentField, $parentField, $table) |
||
| 1765 | = $remote->manyManyComponent($remoteRelation); |
||
| 1766 | $extraFields = $remote->manyManyExtraFieldsForComponent($remoteRelation) ?: array(); |
||
| 1767 | |||
| 1768 | // Reverse parent and component fields and create an inverse ManyManyList |
||
| 1769 | /** @var ManyManyList $result */ |
||
| 1770 | $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields); |
||
| 1771 | if($this->model) { |
||
| 1772 | $result->setDataModel($this->model); |
||
| 1773 | } |
||
| 1774 | $this->extend('updateManyManyComponents', $result); |
||
| 1775 | |||
| 1776 | // If this is called on a singleton, then we return an 'orphaned relation' that can have the |
||
| 1777 | // foreignID set elsewhere. |
||
| 1778 | return $result |
||
| 1779 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 1780 | ->forForeignID($this->ID); |
||
| 1781 | } |
||
| 1782 | default: { |
||
| 1783 | return null; |
||
| 1784 | } |
||
| 1785 | } |
||
| 1786 | } |
||
| 1787 | |||
| 1788 | /** |
||
| 1789 | * Tries to find the database key on another object that is used to store a |
||
| 1790 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
| 1791 | * |
||
| 1792 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
| 1793 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
| 1794 | * |
||
| 1795 | * @param string $component Name of the relation on the current object pointing to the |
||
| 1796 | * remote object. |
||
| 1797 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
| 1798 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
| 1799 | * @return string |
||
| 1800 | * @throws Exception |
||
| 1801 | */ |
||
| 1802 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
| 1870 | |||
| 1871 | /** |
||
| 1872 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1873 | * @param string $componentName Name of the many-many component |
||
| 1874 | * @return ManyManyList The set of components |
||
| 1875 | */ |
||
| 1876 | public function getManyManyComponents($componentName) { |
||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1915 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1916 | * |
||
| 1917 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1918 | * their classes. |
||
| 1919 | */ |
||
| 1920 | public function hasOne() { |
||
| 1923 | |||
| 1924 | /** |
||
| 1925 | * Return data for a specific has_one component. |
||
| 1926 | * @param string $component |
||
| 1927 | * @return string|null |
||
| 1928 | */ |
||
| 1929 | public function hasOneComponent($component) { |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1942 | * their class name will be returned. |
||
| 1943 | * |
||
| 1944 | * @param string $component - Name of component |
||
| 1945 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1946 | * the field data stripped off. It defaults to TRUE. |
||
| 1947 | * @return string|array |
||
| 1948 | */ |
||
| 1949 | public function belongsTo($component = null, $classOnly = true) { |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * Return data for a specific belongs_to component. |
||
| 1969 | * @param string $component |
||
| 1970 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1971 | * the field data stripped off. It defaults to TRUE. |
||
| 1972 | * @return string|null |
||
| 1973 | */ |
||
| 1974 | public function belongsToComponent($component, $classOnly = true) { |
||
| 1985 | |||
| 1986 | /** |
||
| 1987 | * Return all of the database fields in this object |
||
| 1988 | * |
||
| 1989 | * @param string $fieldName Limit the output to a specific field name |
||
| 1990 | * @param string $includeTable If returning a single column, prefix the column with the table name |
||
| 1991 | * in Table.Column(spec) format |
||
| 1992 | * @return array|string|null The database fields, or if searching a single field, just this one field if found |
||
| 1993 | * Field will be a string in ClassName(args) format, or Table.ClassName(args) format if $includeTable is true |
||
| 1994 | */ |
||
| 1995 | public function db($fieldName = null, $includeTable = false) { |
||
| 2034 | |||
| 2035 | /** |
||
| 2036 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 2037 | * relationships and their classes will be returned. |
||
| 2038 | * |
||
| 2039 | * @param string $component Deprecated - Name of component |
||
| 2040 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2041 | * the field data stripped off. It defaults to TRUE. |
||
| 2042 | * @return string|array|false |
||
| 2043 | */ |
||
| 2044 | public function hasMany($component = null, $classOnly = true) { |
||
| 2061 | |||
| 2062 | /** |
||
| 2063 | * Return data for a specific has_many component. |
||
| 2064 | * @param string $component |
||
| 2065 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2066 | * the field data stripped off. It defaults to TRUE. |
||
| 2067 | * @return string|null |
||
| 2068 | */ |
||
| 2069 | public function hasManyComponent($component, $classOnly = true) { |
||
| 2080 | |||
| 2081 | /** |
||
| 2082 | * Return the many-to-many extra fields specification. |
||
| 2083 | * |
||
| 2084 | * If you don't specify a component name, it returns all |
||
| 2085 | * extra fields for all components available. |
||
| 2086 | * |
||
| 2087 | * @return array|null |
||
| 2088 | */ |
||
| 2089 | public function manyManyExtraFields() { |
||
| 2092 | |||
| 2093 | /** |
||
| 2094 | * Return the many-to-many extra fields specification for a specific component. |
||
| 2095 | * @param string $component |
||
| 2096 | * @return array|null |
||
| 2097 | */ |
||
| 2098 | public function manyManyExtraFieldsForComponent($component) { |
||
| 2138 | |||
| 2139 | /** |
||
| 2140 | * Return information about a many-to-many component. |
||
| 2141 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 2142 | * components are returned. |
||
| 2143 | * |
||
| 2144 | * @see DataObject::manyManyComponent() |
||
| 2145 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 2146 | */ |
||
| 2147 | public function manyMany() { |
||
| 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) { |
||
| 2319 | |||
| 2320 | /** |
||
| 2321 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2322 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2323 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2324 | * |
||
| 2325 | * @uses FormScaffolder |
||
| 2326 | * |
||
| 2327 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2328 | * @return FieldList |
||
| 2329 | */ |
||
| 2330 | public function scaffoldFormFields($_params = null) { |
||
| 2351 | |||
| 2352 | /** |
||
| 2353 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2354 | * being called on extensions |
||
| 2355 | * |
||
| 2356 | * @param callable $callback The callback to execute |
||
| 2357 | */ |
||
| 2358 | protected function beforeUpdateCMSFields($callback) { |
||
| 2361 | |||
| 2362 | /** |
||
| 2363 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2364 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2365 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2366 | * generate this set. To customize, overload this method in a subclass |
||
| 2367 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2368 | * |
||
| 2369 | * <code> |
||
| 2370 | * class MyCustomClass extends DataObject { |
||
| 2371 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2372 | * |
||
| 2373 | * function getCMSFields() { |
||
| 2374 | * $fields = parent::getCMSFields(); |
||
| 2375 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2376 | * return $fields; |
||
| 2377 | * } |
||
| 2378 | * } |
||
| 2379 | * </code> |
||
| 2380 | * |
||
| 2381 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2382 | * |
||
| 2383 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2384 | */ |
||
| 2385 | public function getCMSFields() { |
||
| 2397 | |||
| 2398 | /** |
||
| 2399 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2400 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2401 | * |
||
| 2402 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
| 2403 | */ |
||
| 2404 | public function getCMSActions() { |
||
| 2409 | |||
| 2410 | |||
| 2411 | /** |
||
| 2412 | * Used for simple frontend forms without relation editing |
||
| 2413 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2414 | * by default. To customize, either overload this method in your |
||
| 2415 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2416 | * |
||
| 2417 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2418 | * |
||
| 2419 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2420 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2421 | */ |
||
| 2422 | public function getFrontEndFields($params = null) { |
||
| 2428 | |||
| 2429 | /** |
||
| 2430 | * Gets the value of a field. |
||
| 2431 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2432 | * |
||
| 2433 | * @param string $field The name of the field |
||
| 2434 | * |
||
| 2435 | * @return mixed The field value |
||
| 2436 | */ |
||
| 2437 | public function getField($field) { |
||
| 2456 | |||
| 2457 | /** |
||
| 2458 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2459 | * |
||
| 2460 | * @param string $tableClass Base table to load the values from. Others are joined as required. |
||
| 2461 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2462 | */ |
||
| 2463 | protected function loadLazyFields($tableClass = null) { |
||
| 2537 | |||
| 2538 | /** |
||
| 2539 | * Return the fields that have changed. |
||
| 2540 | * |
||
| 2541 | * The change level affects what the functions defines as "changed": |
||
| 2542 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2543 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2544 | * for example a change from 0 to null would not be included. |
||
| 2545 | * |
||
| 2546 | * Example return: |
||
| 2547 | * <code> |
||
| 2548 | * array( |
||
| 2549 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2550 | * ) |
||
| 2551 | * </code> |
||
| 2552 | * |
||
| 2553 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2554 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2555 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2556 | * @return array |
||
| 2557 | */ |
||
| 2558 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
| 2599 | |||
| 2600 | /** |
||
| 2601 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2602 | * since loading them from the database. |
||
| 2603 | * |
||
| 2604 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2605 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2606 | * @return boolean |
||
| 2607 | */ |
||
| 2608 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
| 2618 | |||
| 2619 | /** |
||
| 2620 | * Set the value of the field |
||
| 2621 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2622 | * |
||
| 2623 | * @param string $fieldName Name of the field |
||
| 2624 | * @param mixed $val New field value |
||
| 2625 | * @return DataObject $this |
||
| 2626 | */ |
||
| 2627 | public function setField($fieldName, $val) { |
||
| 2677 | |||
| 2678 | /** |
||
| 2679 | * Set the value of the field, using a casting object. |
||
| 2680 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2681 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2682 | * can be saved into the Image table. |
||
| 2683 | * |
||
| 2684 | * @param string $fieldName Name of the field |
||
| 2685 | * @param mixed $value New field value |
||
| 2686 | * @return $this |
||
| 2687 | */ |
||
| 2688 | public function setCastedField($fieldName, $value) { |
||
| 2701 | |||
| 2702 | public function castingHelper($field) { |
||
| 2709 | |||
| 2710 | /** |
||
| 2711 | * Returns true if the given field exists in a database column on any of |
||
| 2712 | * the objects tables and optionally look up a dynamic getter with |
||
| 2713 | * get<fieldName>(). |
||
| 2714 | * |
||
| 2715 | * @param string $field Name of the field |
||
| 2716 | * @return boolean True if the given field exists |
||
| 2717 | */ |
||
| 2718 | public function hasField($field) { |
||
| 2726 | |||
| 2727 | /** |
||
| 2728 | * Returns true if the given field exists as a database column |
||
| 2729 | * |
||
| 2730 | * @param string $field Name of the field |
||
| 2731 | * |
||
| 2732 | * @return boolean |
||
| 2733 | */ |
||
| 2734 | public function hasDatabaseField($field) { |
||
| 2738 | |||
| 2739 | /** |
||
| 2740 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2741 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2742 | * |
||
| 2743 | * @param string $field Name of the field |
||
| 2744 | * @return string The field type of the given field |
||
| 2745 | */ |
||
| 2746 | public function hasOwnTableDatabaseField($field) { |
||
| 2749 | |||
| 2750 | /** |
||
| 2751 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2752 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2753 | * |
||
| 2754 | * @param string $class Class name to check |
||
| 2755 | * @param string $field Name of the field |
||
| 2756 | * @return string The field type of the given field |
||
| 2757 | */ |
||
| 2758 | public static function has_own_table_database_field($class, $field) { |
||
| 2768 | |||
| 2769 | /** |
||
| 2770 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
| 2771 | * actually looking in the database. |
||
| 2772 | * |
||
| 2773 | * @param string $dataClass |
||
| 2774 | * @return bool |
||
| 2775 | */ |
||
| 2776 | public static function has_own_table($dataClass) { |
||
| 2791 | |||
| 2792 | /** |
||
| 2793 | * Returns true if the member is allowed to do the given action. |
||
| 2794 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2795 | * |
||
| 2796 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2797 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2798 | * in user. |
||
| 2799 | * |
||
| 2800 | * @return boolean True if the the member is allowed to do the given action |
||
| 2801 | */ |
||
| 2802 | public function can($perm, $member = null) { |
||
| 2861 | |||
| 2862 | /** |
||
| 2863 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2864 | * expected to return one of three values: |
||
| 2865 | * |
||
| 2866 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2867 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2868 | * - NULL: Don't affect the outcome |
||
| 2869 | * |
||
| 2870 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2871 | * |
||
| 2872 | * <code> |
||
| 2873 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2874 | * if($extended !== null) return $extended; |
||
| 2875 | * else return $normalValue; |
||
| 2876 | * </code> |
||
| 2877 | * |
||
| 2878 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2879 | * @param Member|int $member |
||
| 2880 | * @param array $context Optional context |
||
| 2881 | * @return boolean|null |
||
| 2882 | */ |
||
| 2883 | public function extendedCan($methodName, $member, $context = array()) { |
||
| 2894 | |||
| 2895 | /** |
||
| 2896 | * @param Member $member |
||
| 2897 | * @return boolean |
||
| 2898 | */ |
||
| 2899 | public function canView($member = null) { |
||
| 2906 | |||
| 2907 | /** |
||
| 2908 | * @param Member $member |
||
| 2909 | * @return boolean |
||
| 2910 | */ |
||
| 2911 | public function canEdit($member = null) { |
||
| 2918 | |||
| 2919 | /** |
||
| 2920 | * @param Member $member |
||
| 2921 | * @return boolean |
||
| 2922 | */ |
||
| 2923 | public function canDelete($member = null) { |
||
| 2930 | |||
| 2931 | /** |
||
| 2932 | * @param Member $member |
||
| 2933 | * @param array $context Additional context-specific data which might |
||
| 2934 | * affect whether (or where) this object could be created. |
||
| 2935 | * @return boolean |
||
| 2936 | */ |
||
| 2937 | public function canCreate($member = null, $context = array()) { |
||
| 2944 | |||
| 2945 | /** |
||
| 2946 | * Debugging used by Debug::show() |
||
| 2947 | * |
||
| 2948 | * @return string HTML data representing this object |
||
| 2949 | */ |
||
| 2950 | public function debug() { |
||
| 2958 | |||
| 2959 | /** |
||
| 2960 | * Return the DBField object that represents the given field. |
||
| 2961 | * This works similarly to obj() with 2 key differences: |
||
| 2962 | * - it still returns an object even when the field has no value. |
||
| 2963 | * - it only matches fields and not methods |
||
| 2964 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 2965 | * |
||
| 2966 | * @param string $fieldName Name of the field |
||
| 2967 | * @return DBField The field as a DBField object |
||
| 2968 | */ |
||
| 2969 | public function dbObject($fieldName) { |
||
| 2989 | |||
| 2990 | /** |
||
| 2991 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 2992 | * |
||
| 2993 | * The path to the related field is specified with dot separated syntax |
||
| 2994 | * (eg: Parent.Child.Child.FieldName). |
||
| 2995 | * |
||
| 2996 | * @param string $fieldPath |
||
| 2997 | * |
||
| 2998 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 2999 | */ |
||
| 3000 | public function relObject($fieldPath) { |
||
| 3030 | |||
| 3031 | /** |
||
| 3032 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 3033 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 3034 | * |
||
| 3035 | * @param $fieldName string |
||
| 3036 | * @return string | null - will return null on a missing value |
||
| 3037 | */ |
||
| 3038 | public function relField($fieldName) { |
||
| 3073 | |||
| 3074 | /** |
||
| 3075 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 3076 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 3077 | * |
||
| 3078 | * @return String |
||
| 3079 | */ |
||
| 3080 | public function getReverseAssociation($className) { |
||
| 3096 | |||
| 3097 | /** |
||
| 3098 | * Return all objects matching the filter |
||
| 3099 | * sub-classes are automatically selected and included |
||
| 3100 | * |
||
| 3101 | * @param string $callerClass The class of objects to be returned |
||
| 3102 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3103 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3104 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 3105 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 3106 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 3107 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 3108 | * @param string $containerClass The container class to return the results in. |
||
| 3109 | * |
||
| 3110 | * @todo $containerClass is Ignored, why? |
||
| 3111 | * |
||
| 3112 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 3113 | */ |
||
| 3114 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
| 3151 | |||
| 3152 | |||
| 3153 | /** |
||
| 3154 | * Return the first item matching the given query. |
||
| 3155 | * All calls to get_one() are cached. |
||
| 3156 | * |
||
| 3157 | * @param string $callerClass The class of objects to be returned |
||
| 3158 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3159 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3160 | * @param boolean $cache Use caching |
||
| 3161 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 3162 | * |
||
| 3163 | * @return DataObject The first item matching the query |
||
| 3164 | */ |
||
| 3165 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
| 3191 | |||
| 3192 | /** |
||
| 3193 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 3194 | * Also clears any cached aggregate data. |
||
| 3195 | * |
||
| 3196 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 3197 | * When false will just clear session-local cached data |
||
| 3198 | * @return DataObject $this |
||
| 3199 | */ |
||
| 3200 | public function flushCache($persistent = true) { |
||
| 3216 | |||
| 3217 | /** |
||
| 3218 | * Flush the get_one global cache and destroy associated objects. |
||
| 3219 | */ |
||
| 3220 | public static function flush_and_destroy_cache() { |
||
| 3228 | |||
| 3229 | /** |
||
| 3230 | * Reset all global caches associated with DataObject. |
||
| 3231 | */ |
||
| 3232 | public static function reset() { |
||
| 3241 | |||
| 3242 | /** |
||
| 3243 | * Return the given element, searching by ID |
||
| 3244 | * |
||
| 3245 | * @param string $callerClass The class of the object to be returned |
||
| 3246 | * @param int $id The id of the element |
||
| 3247 | * @param boolean $cache See {@link get_one()} |
||
| 3248 | * |
||
| 3249 | * @return DataObject The element |
||
| 3250 | */ |
||
| 3251 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
| 3268 | |||
| 3269 | /** |
||
| 3270 | * Get the name of the base table for this object |
||
| 3271 | */ |
||
| 3272 | public function baseTable() { |
||
| 3276 | |||
| 3277 | /** |
||
| 3278 | * @var Array Parameters used in the query that built this object. |
||
| 3279 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3280 | * run additional queries using the same context. |
||
| 3281 | */ |
||
| 3282 | protected $sourceQueryParams; |
||
| 3283 | |||
| 3284 | /** |
||
| 3285 | * @see $sourceQueryParams |
||
| 3286 | * @return array |
||
| 3287 | */ |
||
| 3288 | public function getSourceQueryParams() { |
||
| 3291 | |||
| 3292 | /** |
||
| 3293 | * Get list of parameters that should be inherited to relations on this object |
||
| 3294 | * |
||
| 3295 | * @return array |
||
| 3296 | */ |
||
| 3297 | public function getInheritableQueryParams() { |
||
| 3302 | |||
| 3303 | /** |
||
| 3304 | * @see $sourceQueryParams |
||
| 3305 | * @param array |
||
| 3306 | */ |
||
| 3307 | public function setSourceQueryParams($array) { |
||
| 3310 | |||
| 3311 | /** |
||
| 3312 | * @see $sourceQueryParams |
||
| 3313 | * @param array |
||
| 3314 | */ |
||
| 3315 | public function setSourceQueryParam($key, $value) { |
||
| 3318 | |||
| 3319 | /** |
||
| 3320 | * @see $sourceQueryParams |
||
| 3321 | * @return Mixed |
||
| 3322 | */ |
||
| 3323 | public function getSourceQueryParam($key) { |
||
| 3327 | |||
| 3328 | //-------------------------------------------------------------------------------------------// |
||
| 3329 | |||
| 3330 | /** |
||
| 3331 | * Return the database indexes on this table. |
||
| 3332 | * This array is indexed by the name of the field with the index, and |
||
| 3333 | * the value is the type of index. |
||
| 3334 | */ |
||
| 3335 | public function databaseIndexes() { |
||
| 3360 | |||
| 3361 | /** |
||
| 3362 | * Check the database schema and update it as necessary. |
||
| 3363 | * |
||
| 3364 | * @uses DataExtension->augmentDatabase() |
||
| 3365 | */ |
||
| 3366 | public function requireTable() { |
||
| 3411 | |||
| 3412 | /** |
||
| 3413 | * Validate that the configured relations for this class use the correct syntaxes |
||
| 3414 | * @throws LogicException |
||
| 3415 | */ |
||
| 3416 | protected function validateModelDefinitions() { |
||
| 3447 | |||
| 3448 | /** |
||
| 3449 | * Add default records to database. This function is called whenever the |
||
| 3450 | * database is built, after the database tables have all been created. Overload |
||
| 3451 | * this to add default records when the database is built, but make sure you |
||
| 3452 | * call parent::requireDefaultRecords(). |
||
| 3453 | * |
||
| 3454 | * @uses DataExtension->requireDefaultRecords() |
||
| 3455 | */ |
||
| 3456 | public function requireDefaultRecords() { |
||
| 3474 | |||
| 3475 | /** |
||
| 3476 | * Get the default searchable fields for this object, as defined in the |
||
| 3477 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3478 | * data object, uses a default selection of summary fields. |
||
| 3479 | * |
||
| 3480 | * @return array |
||
| 3481 | */ |
||
| 3482 | public function searchableFields() { |
||
| 3556 | |||
| 3557 | /** |
||
| 3558 | * Get any user defined searchable fields labels that |
||
| 3559 | * exist. Allows overriding of default field names in the form |
||
| 3560 | * interface actually presented to the user. |
||
| 3561 | * |
||
| 3562 | * The reason for keeping this separate from searchable_fields, |
||
| 3563 | * which would be a logical place for this functionality, is to |
||
| 3564 | * avoid bloating and complicating the configuration array. Currently |
||
| 3565 | * much of this system is based on sensible defaults, and this property |
||
| 3566 | * would generally only be set in the case of more complex relationships |
||
| 3567 | * between data object being required in the search interface. |
||
| 3568 | * |
||
| 3569 | * Generates labels based on name of the field itself, if no static property |
||
| 3570 | * {@link self::field_labels} exists. |
||
| 3571 | * |
||
| 3572 | * @uses $field_labels |
||
| 3573 | * @uses FormField::name_to_label() |
||
| 3574 | * |
||
| 3575 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3576 | * |
||
| 3577 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3578 | */ |
||
| 3579 | public function fieldLabels($includerelations = true) { |
||
| 3614 | |||
| 3615 | /** |
||
| 3616 | * Get a human-readable label for a single field, |
||
| 3617 | * see {@link fieldLabels()} for more details. |
||
| 3618 | * |
||
| 3619 | * @uses fieldLabels() |
||
| 3620 | * @uses FormField::name_to_label() |
||
| 3621 | * |
||
| 3622 | * @param string $name Name of the field |
||
| 3623 | * @return string Label of the field |
||
| 3624 | */ |
||
| 3625 | public function fieldLabel($name) { |
||
| 3629 | |||
| 3630 | /** |
||
| 3631 | * Get the default summary fields for this object. |
||
| 3632 | * |
||
| 3633 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3634 | * |
||
| 3635 | * @return array |
||
| 3636 | */ |
||
| 3637 | public function summaryFields() { |
||
| 3670 | |||
| 3671 | /** |
||
| 3672 | * Defines a default list of filters for the search context. |
||
| 3673 | * |
||
| 3674 | * If a filter class mapping is defined on the data object, |
||
| 3675 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3676 | * {@link DBField} is used. |
||
| 3677 | * |
||
| 3678 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3679 | * |
||
| 3680 | * @return array |
||
| 3681 | */ |
||
| 3682 | public function defaultSearchFilters() { |
||
| 3703 | |||
| 3704 | /** |
||
| 3705 | * @return boolean True if the object is in the database |
||
| 3706 | */ |
||
| 3707 | public function isInDB() { |
||
| 3710 | |||
| 3711 | /* |
||
| 3712 | * @ignore |
||
| 3713 | */ |
||
| 3714 | private static $subclass_access = true; |
||
| 3715 | |||
| 3716 | /** |
||
| 3717 | * Temporarily disable subclass access in data object qeur |
||
| 3718 | */ |
||
| 3719 | public static function disable_subclass_access() { |
||
| 3725 | |||
| 3726 | //-------------------------------------------------------------------------------------------// |
||
| 3727 | |||
| 3728 | /** |
||
| 3729 | * Database field definitions. |
||
| 3730 | * This is a map from field names to field type. The field |
||
| 3731 | * type should be a class that extends . |
||
| 3732 | * @var array |
||
| 3733 | * @config |
||
| 3734 | */ |
||
| 3735 | private static $db = null; |
||
| 3736 | |||
| 3737 | /** |
||
| 3738 | * Use a casting object for a field. This is a map from |
||
| 3739 | * field name to class name of the casting object. |
||
| 3740 | * |
||
| 3741 | * @var array |
||
| 3742 | */ |
||
| 3743 | private static $casting = array( |
||
| 3744 | "Title" => 'Text', |
||
| 3745 | ); |
||
| 3746 | |||
| 3747 | /** |
||
| 3748 | * Specify custom options for a CREATE TABLE call. |
||
| 3749 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3750 | * All options have to be keyed for a specific database implementation, |
||
| 3751 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3752 | * |
||
| 3753 | * <code> |
||
| 3754 | * array( |
||
| 3755 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3756 | * ) |
||
| 3757 | * </code> |
||
| 3758 | * |
||
| 3759 | * Caution: This API is experimental, and might not be |
||
| 3760 | * included in the next major release. Please use with care. |
||
| 3761 | * |
||
| 3762 | * @var array |
||
| 3763 | * @config |
||
| 3764 | */ |
||
| 3765 | private static $create_table_options = array( |
||
| 3766 | 'MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3767 | ); |
||
| 3768 | |||
| 3769 | /** |
||
| 3770 | * If a field is in this array, then create a database index |
||
| 3771 | * on that field. This is a map from fieldname to index type. |
||
| 3772 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3773 | * |
||
| 3774 | * @var array |
||
| 3775 | * @config |
||
| 3776 | */ |
||
| 3777 | private static $indexes = null; |
||
| 3778 | |||
| 3779 | /** |
||
| 3780 | * Inserts standard column-values when a DataObject |
||
| 3781 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3782 | * This is a map from fieldname to default value. |
||
| 3783 | * |
||
| 3784 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3785 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3786 | * or false in your subclass. Setting it to null won't work. |
||
| 3787 | * |
||
| 3788 | * @var array |
||
| 3789 | * @config |
||
| 3790 | */ |
||
| 3791 | private static $defaults = null; |
||
| 3792 | |||
| 3793 | /** |
||
| 3794 | * Multidimensional array which inserts default data into the database |
||
| 3795 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3796 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3797 | * behaviour such as publishing and ParentNodes. |
||
| 3798 | * |
||
| 3799 | * Example: |
||
| 3800 | * array( |
||
| 3801 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3802 | * array('Title' => "DefaultPage2") |
||
| 3803 | * ). |
||
| 3804 | * |
||
| 3805 | * @var array |
||
| 3806 | * @config |
||
| 3807 | */ |
||
| 3808 | private static $default_records = null; |
||
| 3809 | |||
| 3810 | /** |
||
| 3811 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3812 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3813 | * |
||
| 3814 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3815 | * |
||
| 3816 | * @var array |
||
| 3817 | * @config |
||
| 3818 | */ |
||
| 3819 | private static $has_one = null; |
||
| 3820 | |||
| 3821 | /** |
||
| 3822 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3823 | * |
||
| 3824 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3825 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3826 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3827 | * |
||
| 3828 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3829 | * |
||
| 3830 | * @var array |
||
| 3831 | * @config |
||
| 3832 | */ |
||
| 3833 | private static $belongs_to; |
||
| 3834 | |||
| 3835 | /** |
||
| 3836 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3837 | * |
||
| 3838 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3839 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3840 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3841 | * which foreign key to use. |
||
| 3842 | * |
||
| 3843 | * @var array |
||
| 3844 | * @config |
||
| 3845 | */ |
||
| 3846 | private static $has_many = null; |
||
| 3847 | |||
| 3848 | /** |
||
| 3849 | * many-many relationship definitions. |
||
| 3850 | * This is a map from component name to data type. |
||
| 3851 | * @var array |
||
| 3852 | * @config |
||
| 3853 | */ |
||
| 3854 | private static $many_many = null; |
||
| 3855 | |||
| 3856 | /** |
||
| 3857 | * Extra fields to include on the connecting many-many table. |
||
| 3858 | * This is a map from field name to field type. |
||
| 3859 | * |
||
| 3860 | * Example code: |
||
| 3861 | * <code> |
||
| 3862 | * public static $many_many_extraFields = array( |
||
| 3863 | * 'Members' => array( |
||
| 3864 | * 'Role' => 'Varchar(100)' |
||
| 3865 | * ) |
||
| 3866 | * ); |
||
| 3867 | * </code> |
||
| 3868 | * |
||
| 3869 | * @var array |
||
| 3870 | * @config |
||
| 3871 | */ |
||
| 3872 | private static $many_many_extraFields = null; |
||
| 3873 | |||
| 3874 | /** |
||
| 3875 | * The inverse side of a many-many relationship. |
||
| 3876 | * This is a map from component name to data type. |
||
| 3877 | * @var array |
||
| 3878 | * @config |
||
| 3879 | */ |
||
| 3880 | private static $belongs_many_many = null; |
||
| 3881 | |||
| 3882 | /** |
||
| 3883 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3884 | * clause of a SQL query if no other sort expression is provided. |
||
| 3885 | * @var string |
||
| 3886 | * @config |
||
| 3887 | */ |
||
| 3888 | private static $default_sort = null; |
||
| 3889 | |||
| 3890 | /** |
||
| 3891 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3892 | * search interface. |
||
| 3893 | * |
||
| 3894 | * Overriding the default filter, with a custom defined filter: |
||
| 3895 | * <code> |
||
| 3896 | * static $searchable_fields = array( |
||
| 3897 | * "Name" => "PartialMatchFilter" |
||
| 3898 | * ); |
||
| 3899 | * </code> |
||
| 3900 | * |
||
| 3901 | * Overriding the default form fields, with a custom defined field. |
||
| 3902 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3903 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3904 | * <code> |
||
| 3905 | * static $searchable_fields = array( |
||
| 3906 | * "Name" => array( |
||
| 3907 | * "field" => "TextField" |
||
| 3908 | * ) |
||
| 3909 | * ); |
||
| 3910 | * </code> |
||
| 3911 | * |
||
| 3912 | * Overriding the default form field, filter and title: |
||
| 3913 | * <code> |
||
| 3914 | * static $searchable_fields = array( |
||
| 3915 | * "Organisation.ZipCode" => array( |
||
| 3916 | * "field" => "TextField", |
||
| 3917 | * "filter" => "PartialMatchFilter", |
||
| 3918 | * "title" => 'Organisation ZIP' |
||
| 3919 | * ) |
||
| 3920 | * ); |
||
| 3921 | * </code> |
||
| 3922 | * @config |
||
| 3923 | */ |
||
| 3924 | private static $searchable_fields = null; |
||
| 3925 | |||
| 3926 | /** |
||
| 3927 | * User defined labels for searchable_fields, used to override |
||
| 3928 | * default display in the search form. |
||
| 3929 | * @config |
||
| 3930 | */ |
||
| 3931 | private static $field_labels = null; |
||
| 3932 | |||
| 3933 | /** |
||
| 3934 | * Provides a default list of fields to be used by a 'summary' |
||
| 3935 | * view of this object. |
||
| 3936 | * @config |
||
| 3937 | */ |
||
| 3938 | private static $summary_fields = null; |
||
| 3939 | |||
| 3940 | /** |
||
| 3941 | * Collect all static properties on the object |
||
| 3942 | * which contain natural language, and need to be translated. |
||
| 3943 | * The full entity name is composed from the class name and a custom identifier. |
||
| 3944 | * |
||
| 3945 | * @return array A numerical array which contains one or more entities in array-form. |
||
| 3946 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
| 3947 | * $entity, $string, $priority, $context. |
||
| 3948 | */ |
||
| 3949 | public function provideI18nEntities() { |
||
| 3967 | |||
| 3968 | /** |
||
| 3969 | * Returns true if the given method/parameter has a value |
||
| 3970 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 3971 | * |
||
| 3972 | * @param string $field The field name |
||
| 3973 | * @param array $arguments |
||
| 3974 | * @param bool $cache |
||
| 3975 | * @return boolean |
||
| 3976 | */ |
||
| 3977 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 3985 | |||
| 3986 | } |
||
| 3987 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.