Complex classes like DataObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 103 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider |
||
| 104 | { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Human-readable singular name. |
||
| 108 | * @var string |
||
| 109 | * @config |
||
| 110 | */ |
||
| 111 | private static $singular_name = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Human-readable plural name |
||
| 115 | * @var string |
||
| 116 | * @config |
||
| 117 | */ |
||
| 118 | private static $plural_name = null; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Allow API access to this object? |
||
| 122 | * @todo Define the options that can be set here |
||
| 123 | * @config |
||
| 124 | */ |
||
| 125 | private static $api_access = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Allows specification of a default value for the ClassName field. |
||
| 129 | * Configure this value only in subclasses of DataObject. |
||
| 130 | * |
||
| 131 | * @config |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | private static $default_classname = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * True if this DataObject has been destroyed. |
||
| 138 | * @var boolean |
||
| 139 | */ |
||
| 140 | public $destroyed = false; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The DataModel from this this object comes |
||
| 144 | */ |
||
| 145 | protected $model; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 149 | * |
||
| 150 | * Use {@link toMap()} if you want an array representation |
||
| 151 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | protected $record; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * If selected through a many_many through relation, this is the instance of the through record |
||
| 159 | * |
||
| 160 | * @var DataObject |
||
| 161 | */ |
||
| 162 | protected $joinRecord; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 166 | */ |
||
| 167 | const CHANGE_NONE = 0; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 171 | * (before !== after && before == after) |
||
| 172 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 173 | * Value changes are by nature also considered strict changes. |
||
| 174 | */ |
||
| 175 | const CHANGE_STRICT = 1; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Represents a field that has changed the loosely defined value |
||
| 179 | * (before != after, thus, before !== after)) |
||
| 180 | * E.g. change false to true, but not false to 0 |
||
| 181 | */ |
||
| 182 | const CHANGE_VALUE = 2; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * An array indexed by fieldname, true if the field has been changed. |
||
| 186 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 187 | * the changed state. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $changed; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The database record (in the same format as $record), before |
||
| 195 | * any changes. |
||
| 196 | * @var array |
||
| 197 | */ |
||
| 198 | protected $original; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 202 | * @var boolean |
||
| 203 | */ |
||
| 204 | protected $brokenOnDelete = false; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 208 | * @var boolean |
||
| 209 | */ |
||
| 210 | protected $brokenOnWrite = false; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @config |
||
| 214 | * @var boolean Should dataobjects be validated before they are written? |
||
| 215 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 216 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 217 | * to only disable validation for very specific use cases. |
||
| 218 | */ |
||
| 219 | private static $validation_enabled = true; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Static caches used by relevant functions. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | protected static $_cache_get_one; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Cache of field labels |
||
| 230 | * |
||
| 231 | * @var array |
||
| 232 | */ |
||
| 233 | protected static $_cache_field_labels = array(); |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Base fields which are not defined in static $db |
||
| 237 | * |
||
| 238 | * @config |
||
| 239 | * @var array |
||
| 240 | */ |
||
| 241 | private static $fixed_fields = array( |
||
| 242 | 'ID' => 'PrimaryKey', |
||
| 243 | 'ClassName' => 'DBClassName', |
||
| 244 | 'LastEdited' => 'DBDatetime', |
||
| 245 | 'Created' => 'DBDatetime', |
||
| 246 | ); |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Core dataobject extensions |
||
| 250 | * |
||
| 251 | * @config |
||
| 252 | * @var array |
||
| 253 | */ |
||
| 254 | private static $extensions = array( |
||
| 255 | 'AssetControl' => 'SilverStripe\\Assets\\AssetControlExtension' |
||
| 256 | ); |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Override table name for this class. If ignored will default to FQN of class. |
||
| 260 | * This option is not inheritable, and must be set on each class. |
||
| 261 | * If left blank naming will default to the legacy (3.x) behaviour. |
||
| 262 | * |
||
| 263 | * @var string |
||
| 264 | */ |
||
| 265 | private static $table_name = null; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Non-static relationship cache, indexed by component name. |
||
| 269 | * |
||
| 270 | * @var DataObject[] |
||
| 271 | */ |
||
| 272 | protected $components; |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 276 | * |
||
| 277 | * @var UnsavedRelationList[] |
||
| 278 | */ |
||
| 279 | protected $unsavedRelations; |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get schema object |
||
| 283 | * |
||
| 284 | * @return DataObjectSchema |
||
| 285 | */ |
||
| 286 | public static function getSchema() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Construct a new DataObject. |
||
| 293 | * |
||
| 294 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
| 295 | * field values. Normally this constructor is only used by the internal systems that get objects from the database. |
||
| 296 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 297 | * Singletons don't have their defaults set. |
||
| 298 | * @param DataModel $model |
||
| 299 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 300 | */ |
||
| 301 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Set the DataModel |
||
| 389 | * @param DataModel $model |
||
| 390 | * @return DataObject $this |
||
| 391 | */ |
||
| 392 | public function setDataModel(DataModel $model) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Destroy all of this objects dependant objects and local caches. |
||
| 400 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 401 | */ |
||
| 402 | public function destroy() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Create a duplicate of this node. |
||
| 411 | * Note: now also duplicates relations. |
||
| 412 | * |
||
| 413 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 414 | * If this is true, it will create the duplicate in the database. |
||
| 415 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 416 | */ |
||
| 417 | public function duplicate($doWrite = true) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
| 435 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
| 436 | * automatically when adding the new relations. |
||
| 437 | * |
||
| 438 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 439 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 440 | * @return DataObject with the new many_many relations copied in |
||
| 441 | */ |
||
| 442 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Helper function to duplicate relations from one object to another |
||
| 472 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 473 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 474 | * @param string $name the name of the relation to duplicate (e.g. members) |
||
| 475 | */ |
||
| 476 | private function duplicateRelations($sourceObject, $destinationObject, $name) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Return obsolete class name, if this is no longer a valid class |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function getObsoleteClassName() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Gets name of this class |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getClassName() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 522 | * Warning: This will produce an inconsistent record, as the object |
||
| 523 | * instance will not automatically switch to the new subclass. |
||
| 524 | * Please use {@link newClassInstance()} for this purpose, |
||
| 525 | * or destroy and reinstanciate the record. |
||
| 526 | * |
||
| 527 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function setClassName($className) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Create a new instance of a different class from this object's record. |
||
| 545 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 546 | * it ensures that the instance of the class is a match for the className of the |
||
| 547 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 548 | * property manually before calling this method, as it will confuse change detection. |
||
| 549 | * |
||
| 550 | * If the new class is different to the original class, defaults are populated again |
||
| 551 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 552 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 553 | * we still need to repopulate the defaults. |
||
| 554 | * |
||
| 555 | * @param string $newClassName The name of the new class |
||
| 556 | * |
||
| 557 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 558 | */ |
||
| 559 | public function newClassInstance($newClassName) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Adds methods from the extensions. |
||
| 582 | * Called by Object::__construct() once per class. |
||
| 583 | */ |
||
| 584 | public function defineMethods() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 631 | * The default behaviour for a DataObject is to return true if |
||
| 632 | * the object exists in the database, you can override this in subclasses. |
||
| 633 | * |
||
| 634 | * @return boolean true if this object exists |
||
| 635 | */ |
||
| 636 | public function exists() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Returns TRUE if all values (other than "ID") are |
||
| 643 | * considered empty (by weak boolean comparison). |
||
| 644 | * |
||
| 645 | * @return boolean |
||
| 646 | */ |
||
| 647 | public function isEmpty() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Pluralise this item given a specific count. |
||
| 669 | * |
||
| 670 | * E.g. "0 Pages", "1 File", "3 Images" |
||
| 671 | * |
||
| 672 | * @param string $count |
||
| 673 | * @param bool $prependNumber Include number in result. Defaults to true. |
||
| 674 | * @return string |
||
| 675 | */ |
||
| 676 | public function i18n_pluralise($count, $prependNumber = true) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Get the user friendly singular name of this DataObject. |
||
| 688 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 689 | * this returns the class name. |
||
| 690 | * |
||
| 691 | * @return string User friendly singular name of this DataObject |
||
| 692 | */ |
||
| 693 | public function singular_name() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get the translated user friendly singular name of this DataObject |
||
| 705 | * same as singular_name() but runs it through the translating function |
||
| 706 | * |
||
| 707 | * Translating string is in the form: |
||
| 708 | * $this->class.SINGULARNAME |
||
| 709 | * Example: |
||
| 710 | * Page.SINGULARNAME |
||
| 711 | * |
||
| 712 | * @return string User friendly translated singular name of this DataObject |
||
| 713 | */ |
||
| 714 | public function i18n_singular_name() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Get the user friendly plural name of this DataObject |
||
| 723 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 724 | * this returns a pluralised version of the class name. |
||
| 725 | * |
||
| 726 | * @return string User friendly plural name of this DataObject |
||
| 727 | */ |
||
| 728 | public function plural_name() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Get the translated user friendly plural name of this DataObject |
||
| 744 | * Same as plural_name but runs it through the translation function |
||
| 745 | * Translation string is in the form: |
||
| 746 | * $this->class.PLURALNAME |
||
| 747 | * Example: |
||
| 748 | * Page.PLURALNAME |
||
| 749 | * |
||
| 750 | * @return string User friendly translated plural name of this DataObject |
||
| 751 | */ |
||
| 752 | public function i18n_plural_name() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Standard implementation of a title/label for a specific |
||
| 762 | * record. Tries to find properties 'Title' or 'Name', |
||
| 763 | * and falls back to the 'ID'. Useful to provide |
||
| 764 | * user-friendly identification of a record, e.g. in errormessages |
||
| 765 | * or UI-selections. |
||
| 766 | * |
||
| 767 | * Overload this method to have a more specialized implementation, |
||
| 768 | * e.g. for an Address record this could be: |
||
| 769 | * <code> |
||
| 770 | * function getTitle() { |
||
| 771 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 772 | * } |
||
| 773 | * </code> |
||
| 774 | * |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | public function getTitle() |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Returns the associated database record - in this case, the object itself. |
||
| 792 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 793 | * |
||
| 794 | * @return DataObject Associated database record |
||
| 795 | */ |
||
| 796 | public function data() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Convert this object to a map. |
||
| 803 | * |
||
| 804 | * @return array The data as a map. |
||
| 805 | */ |
||
| 806 | public function toMap() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Return all currently fetched database fields. |
||
| 814 | * |
||
| 815 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 816 | * Obviously, this makes it a lot faster. |
||
| 817 | * |
||
| 818 | * @return array The data as a map. |
||
| 819 | */ |
||
| 820 | public function getQueriedDatabaseFields() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 827 | * |
||
| 828 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 829 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 830 | * |
||
| 831 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 832 | * the related objects that it alters. |
||
| 833 | * |
||
| 834 | * @param array $data A map of field name to data values to update. |
||
| 835 | * @return DataObject $this |
||
| 836 | */ |
||
| 837 | public function update($data) |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Pass changes as a map, and try to |
||
| 889 | * get automatic casting for these fields. |
||
| 890 | * Doesn't write to the database. To write the data, |
||
| 891 | * use the write() method. |
||
| 892 | * |
||
| 893 | * @param array $data A map of field name to data values to update. |
||
| 894 | * @return DataObject $this |
||
| 895 | */ |
||
| 896 | public function castedUpdate($data) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Merges data and relations from another object of same class, |
||
| 906 | * without conflict resolution. Allows to specify which |
||
| 907 | * dataset takes priority in case its not empty. |
||
| 908 | * has_one-relations are just transferred with priority 'right'. |
||
| 909 | * has_many and many_many-relations are added regardless of priority. |
||
| 910 | * |
||
| 911 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 912 | * meaning they are not connected to the merged object any longer. |
||
| 913 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 914 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 915 | * Caution: Does not delete the merged object. |
||
| 916 | * Caution: Does now overwrite Created date on the original object. |
||
| 917 | * |
||
| 918 | * @param DataObject $rightObj |
||
| 919 | * @param string $priority left|right Determines who wins in case of a conflict (optional) |
||
| 920 | * @param bool $includeRelations Merge any existing relations (optional) |
||
| 921 | * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values. |
||
| 922 | * Only applicable with $priority='right'. (optional) |
||
| 923 | * @return Boolean |
||
| 924 | */ |
||
| 925 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Forces the record to think that all its data has changed. |
||
| 999 | * Doesn't write to the database. Only sets fields as changed |
||
| 1000 | * if they are not already marked as changed. |
||
| 1001 | * |
||
| 1002 | * @return $this |
||
| 1003 | */ |
||
| 1004 | public function forceChange() |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Validate the current object. |
||
| 1035 | * |
||
| 1036 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1037 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1038 | * |
||
| 1039 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1040 | * and onAfterWrite() won't get called either. |
||
| 1041 | * |
||
| 1042 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1043 | * attempting a write, and respond appropriately if it isn't. |
||
| 1044 | * |
||
| 1045 | * @see {@link ValidationResult} |
||
| 1046 | * @return ValidationResult |
||
| 1047 | */ |
||
| 1048 | public function validate() |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * Public accessor for {@see DataObject::validate()} |
||
| 1057 | * |
||
| 1058 | * @return ValidationResult |
||
| 1059 | */ |
||
| 1060 | public function doValidate() |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Event handler called before writing to the database. |
||
| 1068 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1069 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1070 | * |
||
| 1071 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1072 | * |
||
| 1073 | * @uses DataExtension->onBeforeWrite() |
||
| 1074 | */ |
||
| 1075 | protected function onBeforeWrite() |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Event handler called after writing to the database. |
||
| 1085 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1086 | * $this->changed will have a record |
||
| 1087 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1088 | * |
||
| 1089 | * @uses DataExtension->onAfterWrite() |
||
| 1090 | */ |
||
| 1091 | protected function onAfterWrite() |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Event handler called before deleting from the database. |
||
| 1099 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1100 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1101 | * |
||
| 1102 | * @uses DataExtension->onBeforeDelete() |
||
| 1103 | */ |
||
| 1104 | protected function onBeforeDelete() |
||
| 1111 | |||
| 1112 | protected function onAfterDelete() |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Load the default values in from the self::$defaults array. |
||
| 1119 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1120 | * Called by the constructor when creating new records. |
||
| 1121 | * |
||
| 1122 | * @uses DataExtension->populateDefaults() |
||
| 1123 | * @return DataObject $this |
||
| 1124 | */ |
||
| 1125 | public function populateDefaults() |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * Determine validation of this object prior to write |
||
| 1165 | * |
||
| 1166 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1167 | */ |
||
| 1168 | protected function validateWrite() |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Prepare an object prior to write |
||
| 1193 | * |
||
| 1194 | * @throws ValidationException |
||
| 1195 | */ |
||
| 1196 | protected function preWrite() |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Detects and updates all changes made to this object |
||
| 1216 | * |
||
| 1217 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1218 | * @return bool True if any changes are detected |
||
| 1219 | */ |
||
| 1220 | protected function updateChanges($forceChanges = false) |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1234 | * |
||
| 1235 | * @param string $baseTable Base table |
||
| 1236 | * @param string $now Timestamp to use for the current time |
||
| 1237 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1238 | * @param array $manipulation Manipulation to write to |
||
| 1239 | * @param string $class Class of table to manipulate |
||
| 1240 | */ |
||
| 1241 | 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) |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Generate and write the database manipulation for all changed fields |
||
| 1316 | * |
||
| 1317 | * @param string $baseTable Base table |
||
| 1318 | * @param string $now Timestamp to use for the current time |
||
| 1319 | * @param bool $isNewRecord If this is a new record |
||
| 1320 | */ |
||
| 1321 | protected function writeManipulation($baseTable, $now, $isNewRecord) |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Writes all changes to this object to the database. |
||
| 1344 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1345 | * - All relevant tables will be updated. |
||
| 1346 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1347 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1348 | * |
||
| 1349 | * @uses DataExtension->augmentWrite() |
||
| 1350 | * |
||
| 1351 | * @param boolean $showDebug Show debugging information |
||
| 1352 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1353 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1354 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1355 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1356 | * {@link getManyManyComponents()} (Default: false) |
||
| 1357 | * @return int The ID of the record |
||
| 1358 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1359 | */ |
||
| 1360 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * Writes cached relation lists to the database, if possible |
||
| 1413 | */ |
||
| 1414 | public function writeRelations() |
||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1431 | * same record. |
||
| 1432 | * |
||
| 1433 | * @param bool $recursive Recursively write components |
||
| 1434 | * @return DataObject $this |
||
| 1435 | */ |
||
| 1436 | public function writeComponents($recursive = false) |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * Delete this data object. |
||
| 1453 | * $this->onBeforeDelete() gets called. |
||
| 1454 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1455 | * @uses DataExtension->augmentSQL() |
||
| 1456 | */ |
||
| 1457 | public function delete() |
||
| 1488 | |||
| 1489 | /** |
||
| 1490 | * Delete the record with the given ID. |
||
| 1491 | * |
||
| 1492 | * @param string $className The class name of the record to be deleted |
||
| 1493 | * @param int $id ID of record to be deleted |
||
| 1494 | */ |
||
| 1495 | public static function delete_by_id($className, $id) |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Get the class ancestry, including the current class name. |
||
| 1507 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1508 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1509 | * will be the current class. |
||
| 1510 | * |
||
| 1511 | * @return array Class ancestry |
||
| 1512 | */ |
||
| 1513 | public function getClassAncestry() |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1520 | * If no component is available, an 'empty component' will be returned for |
||
| 1521 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1522 | * |
||
| 1523 | * @param string $componentName Name of the component |
||
| 1524 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1525 | * @throws Exception |
||
| 1526 | */ |
||
| 1527 | public function getComponent($componentName) |
||
| 1599 | |||
| 1600 | /** |
||
| 1601 | * Returns a one-to-many relation as a HasManyList |
||
| 1602 | * |
||
| 1603 | * @param string $componentName Name of the component |
||
| 1604 | * @return HasManyList|UnsavedRelationList The components of the one-to-many relationship. |
||
| 1605 | */ |
||
| 1606 | public function getComponents($componentName) |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1649 | * |
||
| 1650 | * @param string $relationName Relation name. |
||
| 1651 | * @return string Class name, or null if not found. |
||
| 1652 | */ |
||
| 1653 | public function getRelationClass($relationName) |
||
| 1686 | |||
| 1687 | /** |
||
| 1688 | * Given a relation name, determine the relation type |
||
| 1689 | * |
||
| 1690 | * @param string $component Name of component |
||
| 1691 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 1692 | */ |
||
| 1693 | public function getRelationType($component) |
||
| 1704 | |||
| 1705 | /** |
||
| 1706 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 1707 | * side of the relation. |
||
| 1708 | * |
||
| 1709 | * Notes on behaviour: |
||
| 1710 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 1711 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 1712 | * - Cannot be used on polymorphic relationships |
||
| 1713 | * - Cannot be used on unsaved objects. |
||
| 1714 | * |
||
| 1715 | * @param string $remoteClass |
||
| 1716 | * @param string $remoteRelation |
||
| 1717 | * @return DataList|DataObject The component, either as a list or single object |
||
| 1718 | * @throws BadMethodCallException |
||
| 1719 | * @throws InvalidArgumentException |
||
| 1720 | */ |
||
| 1721 | public function inferReciprocalComponent($remoteClass, $remoteRelation) |
||
| 1828 | |||
| 1829 | /** |
||
| 1830 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1831 | * @param string $componentName Name of the many-many component |
||
| 1832 | * @return RelationList|UnsavedRelationList The set of components |
||
| 1833 | */ |
||
| 1834 | public function getManyManyComponents($componentName) |
||
| 1888 | |||
| 1889 | /** |
||
| 1890 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1891 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1892 | * |
||
| 1893 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1894 | * their classes. |
||
| 1895 | */ |
||
| 1896 | public function hasOne() |
||
| 1900 | |||
| 1901 | /** |
||
| 1902 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1903 | * their class name will be returned. |
||
| 1904 | * |
||
| 1905 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1906 | * the field data stripped off. It defaults to TRUE. |
||
| 1907 | * @return string|array |
||
| 1908 | */ |
||
| 1909 | public function belongsTo($classOnly = true) |
||
| 1918 | |||
| 1919 | /** |
||
| 1920 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 1921 | * relationships and their classes will be returned. |
||
| 1922 | * |
||
| 1923 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1924 | * the field data stripped off. It defaults to TRUE. |
||
| 1925 | * @return string|array|false |
||
| 1926 | */ |
||
| 1927 | public function hasMany($classOnly = true) |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Return the many-to-many extra fields specification. |
||
| 1939 | * |
||
| 1940 | * If you don't specify a component name, it returns all |
||
| 1941 | * extra fields for all components available. |
||
| 1942 | * |
||
| 1943 | * @return array|null |
||
| 1944 | */ |
||
| 1945 | public function manyManyExtraFields() |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * Return information about a many-to-many component. |
||
| 1952 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 1953 | * components are returned. |
||
| 1954 | * |
||
| 1955 | * @see DataObjectSchema::manyManyComponent() |
||
| 1956 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 1957 | */ |
||
| 1958 | public function manyMany() |
||
| 1965 | |||
| 1966 | /** |
||
| 1967 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 1968 | * |
||
| 1969 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 1970 | * |
||
| 1971 | * @param string $class |
||
| 1972 | * @return array|false |
||
| 1973 | */ |
||
| 1974 | public function database_extensions($class) |
||
| 1984 | |||
| 1985 | /** |
||
| 1986 | * Generates a SearchContext to be used for building and processing |
||
| 1987 | * a generic search form for properties on this object. |
||
| 1988 | * |
||
| 1989 | * @return SearchContext |
||
| 1990 | */ |
||
| 1991 | public function getDefaultSearchContext() |
||
| 1999 | |||
| 2000 | /** |
||
| 2001 | * Determine which properties on the DataObject are |
||
| 2002 | * searchable, and map them to their default {@link FormField} |
||
| 2003 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 2004 | * |
||
| 2005 | * Some additional logic is included for switching field labels, based on |
||
| 2006 | * how generic or specific the field type is. |
||
| 2007 | * |
||
| 2008 | * Used by {@link SearchContext}. |
||
| 2009 | * |
||
| 2010 | * @param array $_params |
||
| 2011 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 2012 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 2013 | * @return FieldList |
||
| 2014 | */ |
||
| 2015 | public function scaffoldSearchFields($_params = null) |
||
| 2071 | |||
| 2072 | /** |
||
| 2073 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2074 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2075 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2076 | * |
||
| 2077 | * @uses FormScaffolder |
||
| 2078 | * |
||
| 2079 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2080 | * @return FieldList |
||
| 2081 | */ |
||
| 2082 | public function scaffoldFormFields($_params = null) |
||
| 2104 | |||
| 2105 | /** |
||
| 2106 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2107 | * being called on extensions |
||
| 2108 | * |
||
| 2109 | * @param callable $callback The callback to execute |
||
| 2110 | */ |
||
| 2111 | protected function beforeUpdateCMSFields($callback) |
||
| 2115 | |||
| 2116 | /** |
||
| 2117 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2118 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2119 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2120 | * generate this set. To customize, overload this method in a subclass |
||
| 2121 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2122 | * |
||
| 2123 | * <code> |
||
| 2124 | * class MyCustomClass extends DataObject { |
||
| 2125 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2126 | * |
||
| 2127 | * function getCMSFields() { |
||
| 2128 | * $fields = parent::getCMSFields(); |
||
| 2129 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2130 | * return $fields; |
||
| 2131 | * } |
||
| 2132 | * } |
||
| 2133 | * </code> |
||
| 2134 | * |
||
| 2135 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2136 | * |
||
| 2137 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2138 | */ |
||
| 2139 | public function getCMSFields() |
||
| 2152 | |||
| 2153 | /** |
||
| 2154 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2155 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2156 | * |
||
| 2157 | * @return FieldList an Empty FieldList(); need to be overload by solid subclass |
||
| 2158 | */ |
||
| 2159 | public function getCMSActions() |
||
| 2165 | |||
| 2166 | |||
| 2167 | /** |
||
| 2168 | * Used for simple frontend forms without relation editing |
||
| 2169 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2170 | * by default. To customize, either overload this method in your |
||
| 2171 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2172 | * |
||
| 2173 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2174 | * |
||
| 2175 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2176 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2177 | */ |
||
| 2178 | public function getFrontEndFields($params = null) |
||
| 2185 | |||
| 2186 | /** |
||
| 2187 | * Gets the value of a field. |
||
| 2188 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2189 | * |
||
| 2190 | * @param string $field The name of the field |
||
| 2191 | * @return mixed The field value |
||
| 2192 | */ |
||
| 2193 | public function getField($field) |
||
| 2213 | |||
| 2214 | /** |
||
| 2215 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2216 | * |
||
| 2217 | * @param string $class Class to load the values from. Others are joined as required. |
||
| 2218 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2219 | * @return bool Flag if lazy loading succeeded |
||
| 2220 | */ |
||
| 2221 | protected function loadLazyFields($class = null) |
||
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Return the fields that have changed. |
||
| 2300 | * |
||
| 2301 | * The change level affects what the functions defines as "changed": |
||
| 2302 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2303 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2304 | * for example a change from 0 to null would not be included. |
||
| 2305 | * |
||
| 2306 | * Example return: |
||
| 2307 | * <code> |
||
| 2308 | * array( |
||
| 2309 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2310 | * ) |
||
| 2311 | * </code> |
||
| 2312 | * |
||
| 2313 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2314 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2315 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2316 | * @return array |
||
| 2317 | */ |
||
| 2318 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) |
||
| 2365 | |||
| 2366 | /** |
||
| 2367 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2368 | * since loading them from the database. |
||
| 2369 | * |
||
| 2370 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2371 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2372 | * @return boolean |
||
| 2373 | */ |
||
| 2374 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) |
||
| 2384 | |||
| 2385 | /** |
||
| 2386 | * Set the value of the field |
||
| 2387 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2388 | * |
||
| 2389 | * @param string $fieldName Name of the field |
||
| 2390 | * @param mixed $val New field value |
||
| 2391 | * @return $this |
||
| 2392 | */ |
||
| 2393 | public function setField($fieldName, $val) |
||
| 2445 | |||
| 2446 | /** |
||
| 2447 | * Set the value of the field, using a casting object. |
||
| 2448 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2449 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2450 | * can be saved into the Image table. |
||
| 2451 | * |
||
| 2452 | * @param string $fieldName Name of the field |
||
| 2453 | * @param mixed $value New field value |
||
| 2454 | * @return $this |
||
| 2455 | */ |
||
| 2456 | public function setCastedField($fieldName, $value) |
||
| 2470 | |||
| 2471 | /** |
||
| 2472 | * {@inheritdoc} |
||
| 2473 | */ |
||
| 2474 | public function castingHelper($field) |
||
| 2494 | |||
| 2495 | /** |
||
| 2496 | * Returns true if the given field exists in a database column on any of |
||
| 2497 | * the objects tables and optionally look up a dynamic getter with |
||
| 2498 | * get<fieldName>(). |
||
| 2499 | * |
||
| 2500 | * @param string $field Name of the field |
||
| 2501 | * @return boolean True if the given field exists |
||
| 2502 | */ |
||
| 2503 | public function hasField($field) |
||
| 2513 | |||
| 2514 | /** |
||
| 2515 | * Returns true if the given field exists as a database column |
||
| 2516 | * |
||
| 2517 | * @param string $field Name of the field |
||
| 2518 | * |
||
| 2519 | * @return boolean |
||
| 2520 | */ |
||
| 2521 | public function hasDatabaseField($field) |
||
| 2526 | |||
| 2527 | /** |
||
| 2528 | * Returns true if the member is allowed to do the given action. |
||
| 2529 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2530 | * |
||
| 2531 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2532 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2533 | * in user. |
||
| 2534 | * @param array $context Additional $context to pass to extendedCan() |
||
| 2535 | * |
||
| 2536 | * @return boolean True if the the member is allowed to do the given action |
||
| 2537 | */ |
||
| 2538 | public function can($perm, $member = null, $context = array()) |
||
| 2600 | |||
| 2601 | /** |
||
| 2602 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2603 | * expected to return one of three values: |
||
| 2604 | * |
||
| 2605 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2606 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2607 | * - NULL: Don't affect the outcome |
||
| 2608 | * |
||
| 2609 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2610 | * |
||
| 2611 | * <code> |
||
| 2612 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2613 | * if($extended !== null) return $extended; |
||
| 2614 | * else return $normalValue; |
||
| 2615 | * </code> |
||
| 2616 | * |
||
| 2617 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2618 | * @param Member|int $member |
||
| 2619 | * @param array $context Optional context |
||
| 2620 | * @return boolean|null |
||
| 2621 | */ |
||
| 2622 | public function extendedCan($methodName, $member, $context = array()) |
||
| 2638 | |||
| 2639 | /** |
||
| 2640 | * @param Member $member |
||
| 2641 | * @return boolean |
||
| 2642 | */ |
||
| 2643 | public function canView($member = null) |
||
| 2651 | |||
| 2652 | /** |
||
| 2653 | * @param Member $member |
||
| 2654 | * @return boolean |
||
| 2655 | */ |
||
| 2656 | public function canEdit($member = null) |
||
| 2664 | |||
| 2665 | /** |
||
| 2666 | * @param Member $member |
||
| 2667 | * @return boolean |
||
| 2668 | */ |
||
| 2669 | public function canDelete($member = null) |
||
| 2677 | |||
| 2678 | /** |
||
| 2679 | * @param Member $member |
||
| 2680 | * @param array $context Additional context-specific data which might |
||
| 2681 | * affect whether (or where) this object could be created. |
||
| 2682 | * @return boolean |
||
| 2683 | */ |
||
| 2684 | public function canCreate($member = null, $context = array()) |
||
| 2692 | |||
| 2693 | /** |
||
| 2694 | * Debugging used by Debug::show() |
||
| 2695 | * |
||
| 2696 | * @return string HTML data representing this object |
||
| 2697 | */ |
||
| 2698 | public function debug() |
||
| 2709 | |||
| 2710 | /** |
||
| 2711 | * Return the DBField object that represents the given field. |
||
| 2712 | * This works similarly to obj() with 2 key differences: |
||
| 2713 | * - it still returns an object even when the field has no value. |
||
| 2714 | * - it only matches fields and not methods |
||
| 2715 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 2716 | * |
||
| 2717 | * @param string $fieldName Name of the field |
||
| 2718 | * @return DBField The field as a DBField object |
||
| 2719 | */ |
||
| 2720 | public function dbObject($fieldName) |
||
| 2746 | |||
| 2747 | /** |
||
| 2748 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 2749 | * |
||
| 2750 | * The path to the related field is specified with dot separated syntax |
||
| 2751 | * (eg: Parent.Child.Child.FieldName). |
||
| 2752 | * |
||
| 2753 | * @param string $fieldPath |
||
| 2754 | * |
||
| 2755 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 2756 | */ |
||
| 2757 | public function relObject($fieldPath) |
||
| 2787 | |||
| 2788 | /** |
||
| 2789 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 2790 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 2791 | * |
||
| 2792 | * @param $fieldName string |
||
| 2793 | * @return string | null - will return null on a missing value |
||
| 2794 | */ |
||
| 2795 | public function relField($fieldName) |
||
| 2831 | |||
| 2832 | /** |
||
| 2833 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 2834 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 2835 | * |
||
| 2836 | * @param string $className |
||
| 2837 | * @return string |
||
| 2838 | */ |
||
| 2839 | public function getReverseAssociation($className) |
||
| 2862 | |||
| 2863 | /** |
||
| 2864 | * Return all objects matching the filter |
||
| 2865 | * sub-classes are automatically selected and included |
||
| 2866 | * |
||
| 2867 | * @param string $callerClass The class of objects to be returned |
||
| 2868 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 2869 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 2870 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 2871 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 2872 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 2873 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 2874 | * @param string $containerClass The container class to return the results in. |
||
| 2875 | * |
||
| 2876 | * @todo $containerClass is Ignored, why? |
||
| 2877 | * |
||
| 2878 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 2879 | */ |
||
| 2880 | public static function get( |
||
| 2923 | |||
| 2924 | |||
| 2925 | /** |
||
| 2926 | * Return the first item matching the given query. |
||
| 2927 | * All calls to get_one() are cached. |
||
| 2928 | * |
||
| 2929 | * @param string $callerClass The class of objects to be returned |
||
| 2930 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 2931 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 2932 | * @param boolean $cache Use caching |
||
| 2933 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 2934 | * |
||
| 2935 | * @return DataObject The first item matching the query |
||
| 2936 | */ |
||
| 2937 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") |
||
| 2964 | |||
| 2965 | /** |
||
| 2966 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 2967 | * Also clears any cached aggregate data. |
||
| 2968 | * |
||
| 2969 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 2970 | * When false will just clear session-local cached data |
||
| 2971 | * @return DataObject $this |
||
| 2972 | */ |
||
| 2973 | public function flushCache($persistent = true) |
||
| 2992 | |||
| 2993 | /** |
||
| 2994 | * Flush the get_one global cache and destroy associated objects. |
||
| 2995 | */ |
||
| 2996 | public static function flush_and_destroy_cache() |
||
| 3011 | |||
| 3012 | /** |
||
| 3013 | * Reset all global caches associated with DataObject. |
||
| 3014 | */ |
||
| 3015 | public static function reset() |
||
| 3024 | |||
| 3025 | /** |
||
| 3026 | * Return the given element, searching by ID |
||
| 3027 | * |
||
| 3028 | * @param string $callerClass The class of the object to be returned |
||
| 3029 | * @param int $id The id of the element |
||
| 3030 | * @param boolean $cache See {@link get_one()} |
||
| 3031 | * |
||
| 3032 | * @return DataObject The element |
||
| 3033 | */ |
||
| 3034 | public static function get_by_id($callerClass, $id, $cache = true) |
||
| 3044 | |||
| 3045 | /** |
||
| 3046 | * Get the name of the base table for this object |
||
| 3047 | * |
||
| 3048 | * @return string |
||
| 3049 | */ |
||
| 3050 | public function baseTable() |
||
| 3054 | |||
| 3055 | /** |
||
| 3056 | * Get the base class for this object |
||
| 3057 | * |
||
| 3058 | * @return string |
||
| 3059 | */ |
||
| 3060 | public function baseClass() |
||
| 3064 | |||
| 3065 | /** |
||
| 3066 | * @var array Parameters used in the query that built this object. |
||
| 3067 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3068 | * run additional queries using the same context. |
||
| 3069 | */ |
||
| 3070 | protected $sourceQueryParams; |
||
| 3071 | |||
| 3072 | /** |
||
| 3073 | * @see $sourceQueryParams |
||
| 3074 | * @return array |
||
| 3075 | */ |
||
| 3076 | public function getSourceQueryParams() |
||
| 3080 | |||
| 3081 | /** |
||
| 3082 | * Get list of parameters that should be inherited to relations on this object |
||
| 3083 | * |
||
| 3084 | * @return array |
||
| 3085 | */ |
||
| 3086 | public function getInheritableQueryParams() |
||
| 3092 | |||
| 3093 | /** |
||
| 3094 | * @see $sourceQueryParams |
||
| 3095 | * @param array |
||
| 3096 | */ |
||
| 3097 | public function setSourceQueryParams($array) |
||
| 3101 | |||
| 3102 | /** |
||
| 3103 | * @see $sourceQueryParams |
||
| 3104 | * @param string $key |
||
| 3105 | * @param string $value |
||
| 3106 | */ |
||
| 3107 | public function setSourceQueryParam($key, $value) |
||
| 3111 | |||
| 3112 | /** |
||
| 3113 | * @see $sourceQueryParams |
||
| 3114 | * @param string $key |
||
| 3115 | * @return string |
||
| 3116 | */ |
||
| 3117 | public function getSourceQueryParam($key) |
||
| 3124 | |||
| 3125 | //-------------------------------------------------------------------------------------------// |
||
| 3126 | |||
| 3127 | /** |
||
| 3128 | * Return the database indexes on this table. |
||
| 3129 | * This array is indexed by the name of the field with the index, and |
||
| 3130 | * the value is the type of index. |
||
| 3131 | */ |
||
| 3132 | public function databaseIndexes() |
||
| 3158 | |||
| 3159 | /** |
||
| 3160 | * Check the database schema and update it as necessary. |
||
| 3161 | * |
||
| 3162 | * @uses DataExtension->augmentDatabase() |
||
| 3163 | */ |
||
| 3164 | public function requireTable() |
||
| 3231 | |||
| 3232 | /** |
||
| 3233 | * Add default records to database. This function is called whenever the |
||
| 3234 | * database is built, after the database tables have all been created. Overload |
||
| 3235 | * this to add default records when the database is built, but make sure you |
||
| 3236 | * call parent::requireDefaultRecords(). |
||
| 3237 | * |
||
| 3238 | * @uses DataExtension->requireDefaultRecords() |
||
| 3239 | */ |
||
| 3240 | public function requireDefaultRecords() |
||
| 3259 | |||
| 3260 | /** |
||
| 3261 | * Get the default searchable fields for this object, as defined in the |
||
| 3262 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3263 | * data object, uses a default selection of summary fields. |
||
| 3264 | * |
||
| 3265 | * @return array |
||
| 3266 | */ |
||
| 3267 | public function searchableFields() |
||
| 3344 | |||
| 3345 | /** |
||
| 3346 | * Get any user defined searchable fields labels that |
||
| 3347 | * exist. Allows overriding of default field names in the form |
||
| 3348 | * interface actually presented to the user. |
||
| 3349 | * |
||
| 3350 | * The reason for keeping this separate from searchable_fields, |
||
| 3351 | * which would be a logical place for this functionality, is to |
||
| 3352 | * avoid bloating and complicating the configuration array. Currently |
||
| 3353 | * much of this system is based on sensible defaults, and this property |
||
| 3354 | * would generally only be set in the case of more complex relationships |
||
| 3355 | * between data object being required in the search interface. |
||
| 3356 | * |
||
| 3357 | * Generates labels based on name of the field itself, if no static property |
||
| 3358 | * {@link self::field_labels} exists. |
||
| 3359 | * |
||
| 3360 | * @uses $field_labels |
||
| 3361 | * @uses FormField::name_to_label() |
||
| 3362 | * |
||
| 3363 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3364 | * |
||
| 3365 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3366 | */ |
||
| 3367 | public function fieldLabels($includerelations = true) |
||
| 3407 | |||
| 3408 | /** |
||
| 3409 | * Get a human-readable label for a single field, |
||
| 3410 | * see {@link fieldLabels()} for more details. |
||
| 3411 | * |
||
| 3412 | * @uses fieldLabels() |
||
| 3413 | * @uses FormField::name_to_label() |
||
| 3414 | * |
||
| 3415 | * @param string $name Name of the field |
||
| 3416 | * @return string Label of the field |
||
| 3417 | */ |
||
| 3418 | public function fieldLabel($name) |
||
| 3423 | |||
| 3424 | /** |
||
| 3425 | * Get the default summary fields for this object. |
||
| 3426 | * |
||
| 3427 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3428 | * |
||
| 3429 | * @return array |
||
| 3430 | */ |
||
| 3431 | public function summaryFields() |
||
| 3475 | |||
| 3476 | /** |
||
| 3477 | * Defines a default list of filters for the search context. |
||
| 3478 | * |
||
| 3479 | * If a filter class mapping is defined on the data object, |
||
| 3480 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3481 | * {@link DBField} is used. |
||
| 3482 | * |
||
| 3483 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3484 | * |
||
| 3485 | * @return array |
||
| 3486 | */ |
||
| 3487 | public function defaultSearchFilters() |
||
| 3504 | |||
| 3505 | /** |
||
| 3506 | * @return boolean True if the object is in the database |
||
| 3507 | */ |
||
| 3508 | public function isInDB() |
||
| 3512 | |||
| 3513 | /* |
||
| 3514 | * @ignore |
||
| 3515 | */ |
||
| 3516 | private static $subclass_access = true; |
||
| 3517 | |||
| 3518 | /** |
||
| 3519 | * Temporarily disable subclass access in data object qeur |
||
| 3520 | */ |
||
| 3521 | public static function disable_subclass_access() |
||
| 3529 | |||
| 3530 | //-------------------------------------------------------------------------------------------// |
||
| 3531 | |||
| 3532 | /** |
||
| 3533 | * Database field definitions. |
||
| 3534 | * This is a map from field names to field type. The field |
||
| 3535 | * type should be a class that extends . |
||
| 3536 | * @var array |
||
| 3537 | * @config |
||
| 3538 | */ |
||
| 3539 | private static $db = null; |
||
| 3540 | |||
| 3541 | /** |
||
| 3542 | * Use a casting object for a field. This is a map from |
||
| 3543 | * field name to class name of the casting object. |
||
| 3544 | * |
||
| 3545 | * @var array |
||
| 3546 | */ |
||
| 3547 | private static $casting = array( |
||
| 3548 | "Title" => 'Text', |
||
| 3549 | ); |
||
| 3550 | |||
| 3551 | /** |
||
| 3552 | * Specify custom options for a CREATE TABLE call. |
||
| 3553 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3554 | * All options have to be keyed for a specific database implementation, |
||
| 3555 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3556 | * |
||
| 3557 | * <code> |
||
| 3558 | * array( |
||
| 3559 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3560 | * ) |
||
| 3561 | * </code> |
||
| 3562 | * |
||
| 3563 | * Caution: This API is experimental, and might not be |
||
| 3564 | * included in the next major release. Please use with care. |
||
| 3565 | * |
||
| 3566 | * @var array |
||
| 3567 | * @config |
||
| 3568 | */ |
||
| 3569 | private static $create_table_options = array( |
||
| 3570 | 'SilverStripe\ORM\Connect\MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3571 | ); |
||
| 3572 | |||
| 3573 | /** |
||
| 3574 | * If a field is in this array, then create a database index |
||
| 3575 | * on that field. This is a map from fieldname to index type. |
||
| 3576 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3577 | * |
||
| 3578 | * @var array |
||
| 3579 | * @config |
||
| 3580 | */ |
||
| 3581 | private static $indexes = null; |
||
| 3582 | |||
| 3583 | /** |
||
| 3584 | * Inserts standard column-values when a DataObject |
||
| 3585 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3586 | * This is a map from fieldname to default value. |
||
| 3587 | * |
||
| 3588 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3589 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3590 | * or false in your subclass. Setting it to null won't work. |
||
| 3591 | * |
||
| 3592 | * @var array |
||
| 3593 | * @config |
||
| 3594 | */ |
||
| 3595 | private static $defaults = null; |
||
| 3596 | |||
| 3597 | /** |
||
| 3598 | * Multidimensional array which inserts default data into the database |
||
| 3599 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3600 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3601 | * behaviour such as publishing and ParentNodes. |
||
| 3602 | * |
||
| 3603 | * Example: |
||
| 3604 | * array( |
||
| 3605 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3606 | * array('Title' => "DefaultPage2") |
||
| 3607 | * ). |
||
| 3608 | * |
||
| 3609 | * @var array |
||
| 3610 | * @config |
||
| 3611 | */ |
||
| 3612 | private static $default_records = null; |
||
| 3613 | |||
| 3614 | /** |
||
| 3615 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3616 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3617 | * |
||
| 3618 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3619 | * |
||
| 3620 | * @var array |
||
| 3621 | * @config |
||
| 3622 | */ |
||
| 3623 | private static $has_one = null; |
||
| 3624 | |||
| 3625 | /** |
||
| 3626 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3627 | * |
||
| 3628 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3629 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3630 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3631 | * |
||
| 3632 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3633 | * |
||
| 3634 | * @var array |
||
| 3635 | * @config |
||
| 3636 | */ |
||
| 3637 | private static $belongs_to; |
||
| 3638 | |||
| 3639 | /** |
||
| 3640 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3641 | * |
||
| 3642 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3643 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3644 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3645 | * which foreign key to use. |
||
| 3646 | * |
||
| 3647 | * @var array |
||
| 3648 | * @config |
||
| 3649 | */ |
||
| 3650 | private static $has_many = null; |
||
| 3651 | |||
| 3652 | /** |
||
| 3653 | * many-many relationship definitions. |
||
| 3654 | * This is a map from component name to data type. |
||
| 3655 | * @var array |
||
| 3656 | * @config |
||
| 3657 | */ |
||
| 3658 | private static $many_many = null; |
||
| 3659 | |||
| 3660 | /** |
||
| 3661 | * Extra fields to include on the connecting many-many table. |
||
| 3662 | * This is a map from field name to field type. |
||
| 3663 | * |
||
| 3664 | * Example code: |
||
| 3665 | * <code> |
||
| 3666 | * public static $many_many_extraFields = array( |
||
| 3667 | * 'Members' => array( |
||
| 3668 | * 'Role' => 'Varchar(100)' |
||
| 3669 | * ) |
||
| 3670 | * ); |
||
| 3671 | * </code> |
||
| 3672 | * |
||
| 3673 | * @var array |
||
| 3674 | * @config |
||
| 3675 | */ |
||
| 3676 | private static $many_many_extraFields = null; |
||
| 3677 | |||
| 3678 | /** |
||
| 3679 | * The inverse side of a many-many relationship. |
||
| 3680 | * This is a map from component name to data type. |
||
| 3681 | * @var array |
||
| 3682 | * @config |
||
| 3683 | */ |
||
| 3684 | private static $belongs_many_many = null; |
||
| 3685 | |||
| 3686 | /** |
||
| 3687 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3688 | * clause of a SQL query if no other sort expression is provided. |
||
| 3689 | * @var string |
||
| 3690 | * @config |
||
| 3691 | */ |
||
| 3692 | private static $default_sort = null; |
||
| 3693 | |||
| 3694 | /** |
||
| 3695 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3696 | * search interface. |
||
| 3697 | * |
||
| 3698 | * Overriding the default filter, with a custom defined filter: |
||
| 3699 | * <code> |
||
| 3700 | * static $searchable_fields = array( |
||
| 3701 | * "Name" => "PartialMatchFilter" |
||
| 3702 | * ); |
||
| 3703 | * </code> |
||
| 3704 | * |
||
| 3705 | * Overriding the default form fields, with a custom defined field. |
||
| 3706 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3707 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3708 | * <code> |
||
| 3709 | * static $searchable_fields = array( |
||
| 3710 | * "Name" => array( |
||
| 3711 | * "field" => "TextField" |
||
| 3712 | * ) |
||
| 3713 | * ); |
||
| 3714 | * </code> |
||
| 3715 | * |
||
| 3716 | * Overriding the default form field, filter and title: |
||
| 3717 | * <code> |
||
| 3718 | * static $searchable_fields = array( |
||
| 3719 | * "Organisation.ZipCode" => array( |
||
| 3720 | * "field" => "TextField", |
||
| 3721 | * "filter" => "PartialMatchFilter", |
||
| 3722 | * "title" => 'Organisation ZIP' |
||
| 3723 | * ) |
||
| 3724 | * ); |
||
| 3725 | * </code> |
||
| 3726 | * @config |
||
| 3727 | */ |
||
| 3728 | private static $searchable_fields = null; |
||
| 3729 | |||
| 3730 | /** |
||
| 3731 | * User defined labels for searchable_fields, used to override |
||
| 3732 | * default display in the search form. |
||
| 3733 | * @config |
||
| 3734 | */ |
||
| 3735 | private static $field_labels = null; |
||
| 3736 | |||
| 3737 | /** |
||
| 3738 | * Provides a default list of fields to be used by a 'summary' |
||
| 3739 | * view of this object. |
||
| 3740 | * @config |
||
| 3741 | */ |
||
| 3742 | private static $summary_fields = null; |
||
| 3743 | |||
| 3744 | /** |
||
| 3745 | * Collect all static properties on the object |
||
| 3746 | * which contain natural language, and need to be translated. |
||
| 3747 | * The full entity name is composed from the class name and a custom identifier. |
||
| 3748 | * |
||
| 3749 | * @return array A numerical array which contains one or more entities in array-form. |
||
| 3750 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
| 3751 | * $entity, $string, $priority, $context. |
||
| 3752 | */ |
||
| 3753 | public function provideI18nEntities() |
||
| 3772 | |||
| 3773 | /** |
||
| 3774 | * Returns true if the given method/parameter has a value |
||
| 3775 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 3776 | * |
||
| 3777 | * @param string $field The field name |
||
| 3778 | * @param array $arguments |
||
| 3779 | * @param bool $cache |
||
| 3780 | * @return boolean |
||
| 3781 | */ |
||
| 3782 | public function hasValue($field, $arguments = null, $cache = true) |
||
| 3792 | |||
| 3793 | /** |
||
| 3794 | * If selected through a many_many through relation, this is the instance of the joined record |
||
| 3795 | * |
||
| 3796 | * @return DataObject |
||
| 3797 | */ |
||
| 3798 | public function getJoin() |
||
| 3802 | |||
| 3803 | /** |
||
| 3804 | * Set joining object |
||
| 3805 | * |
||
| 3806 | * @param DataObject $object |
||
| 3807 | * @param string $alias Alias |
||
| 3808 | * @return $this |
||
| 3809 | */ |
||
| 3810 | public function setJoin(DataObject $object, $alias = null) |
||
| 3823 | } |
||
| 3824 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.