Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DataObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
75 | |||
76 | /** |
||
77 | * Human-readable singular name. |
||
78 | * @var string |
||
79 | * @config |
||
80 | */ |
||
81 | private static $singular_name = null; |
||
82 | |||
83 | /** |
||
84 | * Human-readable plural name |
||
85 | * @var string |
||
86 | * @config |
||
87 | */ |
||
88 | private static $plural_name = null; |
||
89 | |||
90 | /** |
||
91 | * Allow API access to this object? |
||
92 | * @todo Define the options that can be set here |
||
93 | * @config |
||
94 | */ |
||
95 | private static $api_access = false; |
||
96 | |||
97 | /** |
||
98 | * True if this DataObject has been destroyed. |
||
99 | * @var boolean |
||
100 | */ |
||
101 | public $destroyed = false; |
||
102 | |||
103 | /** |
||
104 | * The DataModel from this this object comes |
||
105 | */ |
||
106 | protected $model; |
||
107 | |||
108 | /** |
||
109 | * Data stored in this objects database record. An array indexed by fieldname. |
||
110 | * |
||
111 | * Use {@link toMap()} if you want an array representation |
||
112 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $record; |
||
117 | |||
118 | /** |
||
119 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
120 | */ |
||
121 | const CHANGE_NONE = 0; |
||
122 | |||
123 | /** |
||
124 | * Represents a field that has changed type, although not the loosely defined value. |
||
125 | * (before !== after && before == after) |
||
126 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
127 | * Value changes are by nature also considered strict changes. |
||
128 | */ |
||
129 | const CHANGE_STRICT = 1; |
||
130 | |||
131 | /** |
||
132 | * Represents a field that has changed the loosely defined value |
||
133 | * (before != after, thus, before !== after)) |
||
134 | * E.g. change false to true, but not false to 0 |
||
135 | */ |
||
136 | const CHANGE_VALUE = 2; |
||
137 | |||
138 | /** |
||
139 | * An array indexed by fieldname, true if the field has been changed. |
||
140 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
141 | * the changed state. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | private $changed; |
||
146 | |||
147 | /** |
||
148 | * The database record (in the same format as $record), before |
||
149 | * any changes. |
||
150 | * @var array |
||
151 | */ |
||
152 | protected $original; |
||
153 | |||
154 | /** |
||
155 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
156 | * @var boolean |
||
157 | */ |
||
158 | protected $brokenOnDelete = false; |
||
159 | |||
160 | /** |
||
161 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
162 | * @var boolean |
||
163 | */ |
||
164 | protected $brokenOnWrite = false; |
||
165 | |||
166 | /** |
||
167 | * @config |
||
168 | * @var boolean Should dataobjects be validated before they are written? |
||
169 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
170 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
171 | * to only disable validation for very specific use cases. |
||
172 | */ |
||
173 | private static $validation_enabled = true; |
||
174 | |||
175 | /** |
||
176 | * Static caches used by relevant functions. |
||
177 | */ |
||
178 | public static $cache_has_own_table = array(); |
||
179 | protected static $_cache_db = array(); |
||
180 | protected static $_cache_get_one; |
||
181 | protected static $_cache_get_class_ancestry; |
||
182 | protected static $_cache_composite_fields = array(); |
||
183 | protected static $_cache_is_composite_field = array(); |
||
184 | protected static $_cache_custom_database_fields = array(); |
||
185 | protected static $_cache_field_labels = array(); |
||
186 | |||
187 | // base fields which are not defined in static $db |
||
188 | private static $fixed_fields = array( |
||
189 | 'ID' => 'Int', |
||
190 | 'ClassName' => 'Enum', |
||
191 | 'LastEdited' => 'SS_Datetime', |
||
192 | 'Created' => 'SS_Datetime', |
||
193 | ); |
||
194 | |||
195 | /** |
||
196 | * Non-static relationship cache, indexed by component name. |
||
197 | */ |
||
198 | protected $components; |
||
199 | |||
200 | /** |
||
201 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
202 | */ |
||
203 | protected $unsavedRelations; |
||
204 | |||
205 | /** |
||
206 | * Returns when validation on DataObjects is enabled. |
||
207 | * |
||
208 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
209 | * @return bool |
||
210 | */ |
||
211 | public static function get_validation_enabled() { |
||
215 | |||
216 | /** |
||
217 | * Set whether DataObjects should be validated before they are written. |
||
218 | * |
||
219 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
220 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
221 | * to only disable validation for very specific use cases. |
||
222 | * |
||
223 | * @param $enable bool |
||
224 | * @see DataObject::validate() |
||
225 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
226 | */ |
||
227 | public static function set_validation_enabled($enable) { |
||
231 | |||
232 | /** |
||
233 | * @var [string] - class => ClassName field definition cache for self::database_fields |
||
234 | */ |
||
235 | private static $classname_spec_cache = array(); |
||
236 | |||
237 | /** |
||
238 | * Clear all cached classname specs. It's necessary to clear all cached subclassed names |
||
239 | * for any classes if a new class manifest is generated. |
||
240 | */ |
||
241 | public static function clear_classname_spec_cache() { |
||
245 | |||
246 | /** |
||
247 | * Determines the specification for the ClassName field for the given class |
||
248 | * |
||
249 | * @param string $class |
||
250 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
251 | * @return string Resulting ClassName spec. If $queryDB is true this will include all |
||
252 | * legacy types that no longer have concrete classes in PHP |
||
253 | */ |
||
254 | public static function get_classname_spec($class, $queryDB = true) { |
||
272 | |||
273 | /** |
||
274 | * Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName". |
||
275 | * See {@link custom_database_fields()} for a getter that excludes these "base fields". |
||
276 | * |
||
277 | * @param string $class |
||
278 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
279 | * @return array |
||
280 | */ |
||
281 | public static function database_fields($class, $queryDB = true) { |
||
295 | |||
296 | /** |
||
297 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
298 | * and {@link DataObject::$has_one}. Resolves instances of {@link CompositeDBField} |
||
299 | * into the actual database fields, rather than the name of the field which |
||
300 | * might not equate a database column. |
||
301 | * |
||
302 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
303 | * see {@link database_fields()}. |
||
304 | * |
||
305 | * @uses CompositeDBField->compositeDatabaseFields() |
||
306 | * |
||
307 | * @param string $class |
||
308 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
309 | */ |
||
310 | public static function custom_database_fields($class) { |
||
353 | |||
354 | /** |
||
355 | * Returns the field class if the given db field on the class is a composite field. |
||
356 | * Will check all applicable ancestor classes and aggregate results. |
||
357 | * |
||
358 | * @param string $class Class to check |
||
359 | * @param string $name Field to check |
||
360 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
361 | * @return string Class name of composite field if it exists |
||
362 | */ |
||
363 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
384 | |||
385 | /** |
||
386 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
387 | * Will check all applicable ancestor classes and aggregate results. |
||
388 | */ |
||
389 | public static function composite_fields($class, $aggregated = true) { |
||
401 | |||
402 | /** |
||
403 | * Internal cacher for the composite field information |
||
404 | */ |
||
405 | private static function cache_composite_fields($class) { |
||
424 | |||
425 | /** |
||
426 | * Construct a new DataObject. |
||
427 | * |
||
428 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
429 | * field values. Normally this contructor is only used by the internal systems that get objects from the database. |
||
430 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
431 | * Singletons don't have their defaults set. |
||
432 | */ |
||
433 | public function __construct($record = null, $isSingleton = false, $model = null) { |
||
503 | |||
504 | /** |
||
505 | * Set the DataModel |
||
506 | * @param DataModel $model |
||
507 | * @return DataObject $this |
||
508 | */ |
||
509 | public function setDataModel(DataModel $model) { |
||
513 | |||
514 | /** |
||
515 | * Destroy all of this objects dependant objects and local caches. |
||
516 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
517 | */ |
||
518 | public function destroy() { |
||
523 | |||
524 | /** |
||
525 | * Create a duplicate of this node. |
||
526 | * Note: now also duplicates relations. |
||
527 | * |
||
528 | * @param $doWrite Perform a write() operation before returning the object. If this is true, it will create the |
||
529 | * duplicate in the database. |
||
530 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
531 | */ |
||
532 | public function duplicate($doWrite = true) { |
||
546 | |||
547 | /** |
||
548 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
549 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
550 | * automatically when adding the new relations. |
||
551 | * |
||
552 | * @param $sourceObject the source object to duplicate from |
||
553 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
554 | * @return DataObject with the new many_many relations copied in |
||
555 | */ |
||
556 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
576 | |||
577 | /** |
||
578 | * Helper function to duplicate relations from one object to another |
||
579 | * @param $sourceObject the source object to duplicate from |
||
580 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
581 | * @param $name the name of the relation to duplicate (e.g. members) |
||
582 | */ |
||
583 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
597 | |||
598 | public function getObsoleteClassName() { |
||
602 | |||
603 | public function getClassName() { |
||
608 | |||
609 | /** |
||
610 | * Set the ClassName attribute. {@link $class} is also updated. |
||
611 | * Warning: This will produce an inconsistent record, as the object |
||
612 | * instance will not automatically switch to the new subclass. |
||
613 | * Please use {@link newClassInstance()} for this purpose, |
||
614 | * or destroy and reinstanciate the record. |
||
615 | * |
||
616 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
617 | * @return DataObject $this |
||
618 | */ |
||
619 | public function setClassName($className) { |
||
627 | |||
628 | /** |
||
629 | * Create a new instance of a different class from this object's record. |
||
630 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
631 | * it ensures that the instance of the class is a match for the className of the |
||
632 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
633 | * property manually before calling this method, as it will confuse change detection. |
||
634 | * |
||
635 | * If the new class is different to the original class, defaults are populated again |
||
636 | * because this will only occur automatically on instantiation of a DataObject if |
||
637 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
638 | * we still need to repopulate the defaults. |
||
639 | * |
||
640 | * @param string $newClassName The name of the new class |
||
641 | * |
||
642 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
643 | */ |
||
644 | public function newClassInstance($newClassName) { |
||
662 | |||
663 | /** |
||
664 | * Adds methods from the extensions. |
||
665 | * Called by Object::__construct() once per class. |
||
666 | */ |
||
667 | public function defineMethods() { |
||
706 | |||
707 | /** |
||
708 | * Returns true if this object "exists", i.e., has a sensible value. |
||
709 | * The default behaviour for a DataObject is to return true if |
||
710 | * the object exists in the database, you can override this in subclasses. |
||
711 | * |
||
712 | * @return boolean true if this object exists |
||
713 | */ |
||
714 | public function exists() { |
||
717 | |||
718 | /** |
||
719 | * Returns TRUE if all values (other than "ID") are |
||
720 | * considered empty (by weak boolean comparison). |
||
721 | * Only checks for fields listed in {@link custom_database_fields()} |
||
722 | * |
||
723 | * @todo Use DBField->hasValue() |
||
724 | * |
||
725 | * @return boolean |
||
726 | */ |
||
727 | public function isEmpty(){ |
||
741 | |||
742 | /** |
||
743 | * Get the user friendly singular name of this DataObject. |
||
744 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
745 | * this returns the class name. |
||
746 | * |
||
747 | * @return string User friendly singular name of this DataObject |
||
748 | */ |
||
749 | public function singular_name() { |
||
756 | |||
757 | /** |
||
758 | * Get the translated user friendly singular name of this DataObject |
||
759 | * same as singular_name() but runs it through the translating function |
||
760 | * |
||
761 | * Translating string is in the form: |
||
762 | * $this->class.SINGULARNAME |
||
763 | * Example: |
||
764 | * Page.SINGULARNAME |
||
765 | * |
||
766 | * @return string User friendly translated singular name of this DataObject |
||
767 | */ |
||
768 | public function i18n_singular_name() { |
||
771 | |||
772 | /** |
||
773 | * Get the user friendly plural name of this DataObject |
||
774 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
775 | * this returns a pluralised version of the class name. |
||
776 | * |
||
777 | * @return string User friendly plural name of this DataObject |
||
778 | */ |
||
779 | public function plural_name() { |
||
791 | |||
792 | /** |
||
793 | * Get the translated user friendly plural name of this DataObject |
||
794 | * Same as plural_name but runs it through the translation function |
||
795 | * Translation string is in the form: |
||
796 | * $this->class.PLURALNAME |
||
797 | * Example: |
||
798 | * Page.PLURALNAME |
||
799 | * |
||
800 | * @return string User friendly translated plural name of this DataObject |
||
801 | */ |
||
802 | public function i18n_plural_name() |
||
807 | |||
808 | /** |
||
809 | * Standard implementation of a title/label for a specific |
||
810 | * record. Tries to find properties 'Title' or 'Name', |
||
811 | * and falls back to the 'ID'. Useful to provide |
||
812 | * user-friendly identification of a record, e.g. in errormessages |
||
813 | * or UI-selections. |
||
814 | * |
||
815 | * Overload this method to have a more specialized implementation, |
||
816 | * e.g. for an Address record this could be: |
||
817 | * <code> |
||
818 | * function getTitle() { |
||
819 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
820 | * } |
||
821 | * </code> |
||
822 | * |
||
823 | * @return string |
||
824 | */ |
||
825 | public function getTitle() { |
||
831 | |||
832 | /** |
||
833 | * Returns the associated database record - in this case, the object itself. |
||
834 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
835 | * |
||
836 | * @return DataObject Associated database record |
||
837 | */ |
||
838 | public function data() { |
||
841 | |||
842 | /** |
||
843 | * Convert this object to a map. |
||
844 | * |
||
845 | * @return array The data as a map. |
||
846 | */ |
||
847 | public function toMap() { |
||
851 | |||
852 | /** |
||
853 | * Return all currently fetched database fields. |
||
854 | * |
||
855 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
856 | * Obviously, this makes it a lot faster. |
||
857 | * |
||
858 | * @return array The data as a map. |
||
859 | */ |
||
860 | public function getQueriedDatabaseFields() { |
||
863 | |||
864 | /** |
||
865 | * Update a number of fields on this object, given a map of the desired changes. |
||
866 | * |
||
867 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
868 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
869 | * |
||
870 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
871 | * the related objects that it alters. |
||
872 | * |
||
873 | * @param array $data A map of field name to data values to update. |
||
874 | * @return DataObject $this |
||
875 | */ |
||
876 | public function update($data) { |
||
923 | |||
924 | /** |
||
925 | * Pass changes as a map, and try to |
||
926 | * get automatic casting for these fields. |
||
927 | * Doesn't write to the database. To write the data, |
||
928 | * use the write() method. |
||
929 | * |
||
930 | * @param array $data A map of field name to data values to update. |
||
931 | * @return DataObject $this |
||
932 | */ |
||
933 | public function castedUpdate($data) { |
||
939 | |||
940 | /** |
||
941 | * Merges data and relations from another object of same class, |
||
942 | * without conflict resolution. Allows to specify which |
||
943 | * dataset takes priority in case its not empty. |
||
944 | * has_one-relations are just transferred with priority 'right'. |
||
945 | * has_many and many_many-relations are added regardless of priority. |
||
946 | * |
||
947 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
948 | * meaning they are not connected to the merged object any longer. |
||
949 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
950 | * doesn't write the updated object itself (just writes the object-properties). |
||
951 | * Caution: Does not delete the merged object. |
||
952 | * Caution: Does now overwrite Created date on the original object. |
||
953 | * |
||
954 | * @param $obj DataObject |
||
955 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
956 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
957 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
958 | * Only applicable with $priority='right'. (optional) |
||
959 | * @return Boolean |
||
960 | */ |
||
961 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
1030 | |||
1031 | /** |
||
1032 | * Forces the record to think that all its data has changed. |
||
1033 | * Doesn't write to the database. Only sets fields as changed |
||
1034 | * if they are not already marked as changed. |
||
1035 | * |
||
1036 | * @return $this |
||
1037 | */ |
||
1038 | public function forceChange() { |
||
1058 | |||
1059 | /** |
||
1060 | * Validate the current object. |
||
1061 | * |
||
1062 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
1063 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
1064 | * |
||
1065 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
1066 | * and onAfterWrite() won't get called either. |
||
1067 | * |
||
1068 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
1069 | * attempting a write, and respond appropriately if it isn't. |
||
1070 | * |
||
1071 | * @see {@link ValidationResult} |
||
1072 | * @return ValidationResult |
||
1073 | */ |
||
1074 | protected function validate() { |
||
1079 | |||
1080 | /** |
||
1081 | * Public accessor for {@see DataObject::validate()} |
||
1082 | * |
||
1083 | * @return ValidationResult |
||
1084 | */ |
||
1085 | public function doValidate() { |
||
1089 | |||
1090 | /** |
||
1091 | * Event handler called before writing to the database. |
||
1092 | * You can overload this to clean up or otherwise process data before writing it to the |
||
1093 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
1094 | * |
||
1095 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
1096 | * |
||
1097 | * @uses DataExtension->onBeforeWrite() |
||
1098 | */ |
||
1099 | protected function onBeforeWrite() { |
||
1105 | |||
1106 | /** |
||
1107 | * Event handler called after writing to the database. |
||
1108 | * You can overload this to act upon changes made to the data after it is written. |
||
1109 | * $this->changed will have a record |
||
1110 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
1111 | * |
||
1112 | * @uses DataExtension->onAfterWrite() |
||
1113 | */ |
||
1114 | protected function onAfterWrite() { |
||
1118 | |||
1119 | /** |
||
1120 | * Event handler called before deleting from the database. |
||
1121 | * You can overload this to clean up or otherwise process data before delete this |
||
1122 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
1123 | * |
||
1124 | * @uses DataExtension->onBeforeDelete() |
||
1125 | */ |
||
1126 | protected function onBeforeDelete() { |
||
1132 | |||
1133 | protected function onAfterDelete() { |
||
1136 | |||
1137 | /** |
||
1138 | * Load the default values in from the self::$defaults array. |
||
1139 | * Will traverse the defaults of the current class and all its parent classes. |
||
1140 | * Called by the constructor when creating new records. |
||
1141 | * |
||
1142 | * @uses DataExtension->populateDefaults() |
||
1143 | * @return DataObject $this |
||
1144 | */ |
||
1145 | public function populateDefaults() { |
||
1176 | |||
1177 | /** |
||
1178 | * Determine validation of this object prior to write |
||
1179 | * |
||
1180 | * @return ValidationException Exception generated by this write, or null if valid |
||
1181 | */ |
||
1182 | protected function validateWrite() { |
||
1202 | |||
1203 | /** |
||
1204 | * Prepare an object prior to write |
||
1205 | * |
||
1206 | * @throws ValidationException |
||
1207 | */ |
||
1208 | protected function preWrite() { |
||
1224 | |||
1225 | /** |
||
1226 | * Detects and updates all changes made to this object |
||
1227 | * |
||
1228 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
1229 | * @return bool True if any changes are detected |
||
1230 | */ |
||
1231 | protected function updateChanges($forceChanges = false) |
||
1242 | |||
1243 | /** |
||
1244 | * Writes a subset of changes for a specific table to the given manipulation |
||
1245 | * |
||
1246 | * @param string $baseTable Base table |
||
1247 | * @param string $now Timestamp to use for the current time |
||
1248 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
1249 | * @param array $manipulation Manipulation to write to |
||
1250 | * @param string $class Table and Class to select and write to |
||
1251 | */ |
||
1252 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
1297 | |||
1298 | /** |
||
1299 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
1300 | * |
||
1301 | * Does nothing if an ID is already assigned for this record |
||
1302 | * |
||
1303 | * @param string $baseTable Base table |
||
1304 | * @param string $now Timestamp to use for the current time |
||
1305 | */ |
||
1306 | protected function writeBaseRecord($baseTable, $now) { |
||
1318 | |||
1319 | /** |
||
1320 | * Generate and write the database manipulation for all changed fields |
||
1321 | * |
||
1322 | * @param string $baseTable Base table |
||
1323 | * @param string $now Timestamp to use for the current time |
||
1324 | * @param bool $isNewRecord If this is a new record |
||
1325 | */ |
||
1326 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
1347 | |||
1348 | /** |
||
1349 | * Writes all changes to this object to the database. |
||
1350 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
1351 | * - All relevant tables will be updated. |
||
1352 | * - $this->onBeforeWrite() gets called beforehand. |
||
1353 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
1354 | * |
||
1355 | * @uses DataExtension->augmentWrite() |
||
1356 | * |
||
1357 | * @param boolean $showDebug Show debugging information |
||
1358 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
1359 | * @param boolean $forceWrite Write to database even if there are no changes |
||
1360 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
1361 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
1362 | * {@link getManyManyComponents()} (Default: false) |
||
1363 | * @return int The ID of the record |
||
1364 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
1365 | */ |
||
1366 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
1411 | |||
1412 | /** |
||
1413 | * Writes cached relation lists to the database, if possible |
||
1414 | */ |
||
1415 | public function writeRelations() { |
||
1426 | |||
1427 | /** |
||
1428 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
1429 | * same record. |
||
1430 | * |
||
1431 | * @param $recursive Recursively write components |
||
1432 | * @return DataObject $this |
||
1433 | */ |
||
1434 | public function writeComponents($recursive = false) { |
||
1442 | |||
1443 | /** |
||
1444 | * Delete this data object. |
||
1445 | * $this->onBeforeDelete() gets called. |
||
1446 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
1447 | * @uses DataExtension->augmentSQL() |
||
1448 | */ |
||
1449 | public function delete() { |
||
1477 | |||
1478 | /** |
||
1479 | * Delete the record with the given ID. |
||
1480 | * |
||
1481 | * @param string $className The class name of the record to be deleted |
||
1482 | * @param int $id ID of record to be deleted |
||
1483 | */ |
||
1484 | public static function delete_by_id($className, $id) { |
||
1492 | |||
1493 | /** |
||
1494 | * Get the class ancestry, including the current class name. |
||
1495 | * The ancestry will be returned as an array of class names, where the 0th element |
||
1496 | * will be the class that inherits directly from DataObject, and the last element |
||
1497 | * will be the current class. |
||
1498 | * |
||
1499 | * @return array Class ancestry |
||
1500 | */ |
||
1501 | public function getClassAncestry() { |
||
1510 | |||
1511 | /** |
||
1512 | * Return a component object from a one to one relationship, as a DataObject. |
||
1513 | * If no component is available, an 'empty component' will be returned for |
||
1514 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
1515 | * |
||
1516 | * @param string $componentName Name of the component |
||
1517 | * |
||
1518 | * @return DataObject The component object. It's exact type will be that of the component. |
||
1519 | */ |
||
1520 | public function getComponent($componentName) { |
||
1576 | |||
1577 | /** |
||
1578 | * Returns a one-to-many relation as a HasManyList |
||
1579 | * |
||
1580 | * @param string $componentName Name of the component |
||
1581 | * @param string|null $filter Deprecated. A filter to be inserted into the WHERE clause |
||
1582 | * @param string|null|array $sort Deprecated. A sort expression to be inserted into the ORDER BY clause. If omitted, |
||
1583 | * the static field $default_sort on the component class will be used. |
||
1584 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
1585 | * @param string|null|array $limit Deprecated. A limit expression to be inserted into the LIMIT clause |
||
1586 | * |
||
1587 | * @return HasManyList The components of the one-to-many relationship. |
||
1588 | */ |
||
1589 | public function getComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
1633 | |||
1634 | /** |
||
1635 | * @deprecated |
||
1636 | */ |
||
1637 | public function getComponentsQuery($componentName, $filter = "", $sort = "", $join = "", $limit = "") { |
||
1641 | |||
1642 | /** |
||
1643 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
1644 | * |
||
1645 | * @param $relationName Relation name. |
||
1646 | * @return string Class name, or null if not found. |
||
1647 | */ |
||
1648 | public function getRelationClass($relationName) { |
||
1672 | |||
1673 | /** |
||
1674 | * Tries to find the database key on another object that is used to store a |
||
1675 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
1676 | * |
||
1677 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
1678 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
1679 | * |
||
1680 | * @param string $component Name of the relation on the current object pointing to the |
||
1681 | * remote object. |
||
1682 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
1683 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
1684 | * @return string |
||
1685 | */ |
||
1686 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
1754 | |||
1755 | /** |
||
1756 | * Returns a many-to-many component, as a ManyManyList. |
||
1757 | * @param string $componentName Name of the many-many component |
||
1758 | * @return ManyManyList The set of components |
||
1759 | * |
||
1760 | * @todo Implement query-params |
||
1761 | */ |
||
1762 | public function getManyManyComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
1802 | |||
1803 | /** |
||
1804 | * @deprecated 4.0 Method has been replaced by hasOne() and hasOneComponent() |
||
1805 | * @param string $component |
||
1806 | * @return array|null |
||
1807 | */ |
||
1808 | public function has_one($component = null) { |
||
1817 | |||
1818 | /** |
||
1819 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
1820 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
1821 | * |
||
1822 | * @param string $component Deprecated - Name of component |
||
1823 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
1824 | * their classes. |
||
1825 | */ |
||
1826 | View Code Duplication | public function hasOne($component = null) { |
|
1838 | |||
1839 | /** |
||
1840 | * Return data for a specific has_one component. |
||
1841 | * @param string $component |
||
1842 | * @return string|null |
||
1843 | */ |
||
1844 | public function hasOneComponent($component) { |
||
1851 | |||
1852 | /** |
||
1853 | * @deprecated 4.0 Method has been replaced by belongsTo() and belongsToComponent() |
||
1854 | * @param string $component |
||
1855 | * @param bool $classOnly |
||
1856 | * @return array|null |
||
1857 | */ |
||
1858 | View Code Duplication | public function belongs_to($component = null, $classOnly = true) { |
|
1867 | |||
1868 | /** |
||
1869 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
1870 | * their class name will be returned. |
||
1871 | * |
||
1872 | * @param string $component - Name of component |
||
1873 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1874 | * the field data stripped off. It defaults to TRUE. |
||
1875 | * @return string|array |
||
1876 | */ |
||
1877 | View Code Duplication | public function belongsTo($component = null, $classOnly = true) { |
|
1894 | |||
1895 | /** |
||
1896 | * Return data for a specific belongs_to component. |
||
1897 | * @param string $component |
||
1898 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1899 | * the field data stripped off. It defaults to TRUE. |
||
1900 | * @return string|false |
||
1901 | */ |
||
1902 | View Code Duplication | public function belongsToComponent($component, $classOnly = true) { |
|
1913 | |||
1914 | /** |
||
1915 | * Return all of the database fields defined in self::$db and all the parent classes. |
||
1916 | * Doesn't include any fields specified by self::$has_one. Use $this->hasOne() to get these fields |
||
1917 | * |
||
1918 | * @param string $fieldName Limit the output to a specific field name |
||
1919 | * @return array The database fields |
||
1920 | */ |
||
1921 | public function db($fieldName = null) { |
||
1949 | |||
1950 | /** |
||
1951 | * @deprecated 4.0 Method has been replaced by hasMany() and hasManyComponent() |
||
1952 | * @param string $component |
||
1953 | * @param bool $classOnly |
||
1954 | * @return array|null |
||
1955 | */ |
||
1956 | View Code Duplication | public function has_many($component = null, $classOnly = true) { |
|
1965 | |||
1966 | /** |
||
1967 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
1968 | * relationships and their classes will be returned. |
||
1969 | * |
||
1970 | * @param string $component Deprecated - Name of component |
||
1971 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1972 | * the field data stripped off. It defaults to TRUE. |
||
1973 | * @return string|array|false |
||
1974 | */ |
||
1975 | View Code Duplication | public function hasMany($component = null, $classOnly = true) { |
|
1992 | |||
1993 | /** |
||
1994 | * Return data for a specific has_many component. |
||
1995 | * @param string $component |
||
1996 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1997 | * the field data stripped off. It defaults to TRUE. |
||
1998 | * @return string|false |
||
1999 | */ |
||
2000 | View Code Duplication | public function hasManyComponent($component, $classOnly = true) { |
|
2011 | |||
2012 | /** |
||
2013 | * @deprecated 4.0 Method has been replaced by manyManyExtraFields() and |
||
2014 | * manyManyExtraFieldsForComponent() |
||
2015 | * @param string $component |
||
2016 | * @return array |
||
2017 | */ |
||
2018 | public function many_many_extraFields($component = null) { |
||
2027 | |||
2028 | /** |
||
2029 | * Return the many-to-many extra fields specification. |
||
2030 | * |
||
2031 | * If you don't specify a component name, it returns all |
||
2032 | * extra fields for all components available. |
||
2033 | * |
||
2034 | * @param string $component Deprecated - Name of component |
||
2035 | * @return array|null |
||
2036 | */ |
||
2037 | View Code Duplication | public function manyManyExtraFields($component = null) { |
|
2050 | |||
2051 | /** |
||
2052 | * Return the many-to-many extra fields specification for a specific component. |
||
2053 | * @param string $component |
||
2054 | * @return array|null |
||
2055 | */ |
||
2056 | public function manyManyExtraFieldsForComponent($component) { |
||
2096 | |||
2097 | /** |
||
2098 | * @deprecated 4.0 Method has been renamed to manyMany() |
||
2099 | * @param string $component |
||
2100 | * @return array|null |
||
2101 | */ |
||
2102 | public function many_many($component = null) { |
||
2111 | |||
2112 | /** |
||
2113 | * Return information about a many-to-many component. |
||
2114 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
2115 | * components are returned. |
||
2116 | * |
||
2117 | * @see DataObject::manyManyComponent() |
||
2118 | * @param string $component Deprecated - Name of component |
||
2119 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
2120 | */ |
||
2121 | public function manyMany($component = null) { |
||
2137 | |||
2138 | /** |
||
2139 | * Return information about a specific many_many component. Returns a numeric array of: |
||
2140 | * array( |
||
2141 | * <classname>, The class that relation is defined in e.g. "Product" |
||
2142 | * <candidateName>, The target class of the relation e.g. "Category" |
||
2143 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
2144 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
2145 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
2146 | * ) |
||
2147 | * @param string $component The component name |
||
2148 | * @return array|null |
||
2149 | */ |
||
2150 | public function manyManyComponent($component) { |
||
2205 | |||
2206 | /** |
||
2207 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
2208 | * |
||
2209 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
2210 | * |
||
2211 | * @return array or false |
||
2212 | */ |
||
2213 | public function database_extensions($class){ |
||
2221 | |||
2222 | /** |
||
2223 | * Generates a SearchContext to be used for building and processing |
||
2224 | * a generic search form for properties on this object. |
||
2225 | * |
||
2226 | * @return SearchContext |
||
2227 | */ |
||
2228 | public function getDefaultSearchContext() { |
||
2235 | |||
2236 | /** |
||
2237 | * Determine which properties on the DataObject are |
||
2238 | * searchable, and map them to their default {@link FormField} |
||
2239 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
2240 | * |
||
2241 | * Some additional logic is included for switching field labels, based on |
||
2242 | * how generic or specific the field type is. |
||
2243 | * |
||
2244 | * Used by {@link SearchContext}. |
||
2245 | * |
||
2246 | * @param array $_params |
||
2247 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
2248 | * 'restrictFields': Numeric array of a field name whitelist |
||
2249 | * @return FieldList |
||
2250 | */ |
||
2251 | public function scaffoldSearchFields($_params = null) { |
||
2298 | |||
2299 | /** |
||
2300 | * Scaffold a simple edit form for all properties on this dataobject, |
||
2301 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
2302 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
2303 | * |
||
2304 | * @uses FormScaffolder |
||
2305 | * |
||
2306 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
2307 | * @return FieldList |
||
2308 | */ |
||
2309 | public function scaffoldFormFields($_params = null) { |
||
2330 | |||
2331 | /** |
||
2332 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
2333 | * being called on extensions |
||
2334 | * |
||
2335 | * @param callable $callback The callback to execute |
||
2336 | */ |
||
2337 | protected function beforeUpdateCMSFields($callback) { |
||
2340 | |||
2341 | /** |
||
2342 | * Centerpiece of every data administration interface in Silverstripe, |
||
2343 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
2344 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
2345 | * generate this set. To customize, overload this method in a subclass |
||
2346 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
2347 | * |
||
2348 | * <code> |
||
2349 | * class MyCustomClass extends DataObject { |
||
2350 | * static $db = array('CustomProperty'=>'Boolean'); |
||
2351 | * |
||
2352 | * function getCMSFields() { |
||
2353 | * $fields = parent::getCMSFields(); |
||
2354 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
2355 | * return $fields; |
||
2356 | * } |
||
2357 | * } |
||
2358 | * </code> |
||
2359 | * |
||
2360 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
2361 | * |
||
2362 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
2363 | */ |
||
2364 | public function getCMSFields() { |
||
2376 | |||
2377 | /** |
||
2378 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
2379 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
2380 | * |
||
2381 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
2382 | */ |
||
2383 | public function getCMSActions() { |
||
2388 | |||
2389 | |||
2390 | /** |
||
2391 | * Used for simple frontend forms without relation editing |
||
2392 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
2393 | * by default. To customize, either overload this method in your |
||
2394 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
2395 | * |
||
2396 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
2397 | * |
||
2398 | * @param array $params See {@link scaffoldFormFields()} |
||
2399 | * @return FieldList Always returns a simple field collection without TabSet. |
||
2400 | */ |
||
2401 | public function getFrontEndFields($params = null) { |
||
2407 | |||
2408 | /** |
||
2409 | * Gets the value of a field. |
||
2410 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
2411 | * |
||
2412 | * @param string $field The name of the field |
||
2413 | * |
||
2414 | * @return mixed The field value |
||
2415 | */ |
||
2416 | public function getField($field) { |
||
2451 | |||
2452 | /** |
||
2453 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
2454 | * |
||
2455 | * @param string $tableClass Base table to load the values from. Others are joined as required. |
||
2456 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
2457 | * @return bool Flag if lazy loading succeeded |
||
2458 | */ |
||
2459 | protected function loadLazyFields($tableClass = null) { |
||
2530 | |||
2531 | /** |
||
2532 | * Return the fields that have changed. |
||
2533 | * |
||
2534 | * The change level affects what the functions defines as "changed": |
||
2535 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
2536 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
2537 | * for example a change from 0 to null would not be included. |
||
2538 | * |
||
2539 | * Example return: |
||
2540 | * <code> |
||
2541 | * array( |
||
2542 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
2543 | * ) |
||
2544 | * </code> |
||
2545 | * |
||
2546 | * @param boolean $databaseFieldsOnly Get only database fields that have changed |
||
2547 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
2548 | * @return array |
||
2549 | */ |
||
2550 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
2594 | |||
2595 | /** |
||
2596 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
2597 | * since loading them from the database. |
||
2598 | * |
||
2599 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
2600 | * @param int $changeLevel See {@link getChangedFields()} |
||
2601 | * @return boolean |
||
2602 | */ |
||
2603 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
2614 | |||
2615 | /** |
||
2616 | * Set the value of the field |
||
2617 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
2618 | * |
||
2619 | * @param string $fieldName Name of the field |
||
2620 | * @param mixed $val New field value |
||
2621 | * @return DataObject $this |
||
2622 | */ |
||
2623 | public function setField($fieldName, $val) { |
||
2670 | |||
2671 | /** |
||
2672 | * Set the value of the field, using a casting object. |
||
2673 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
2674 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
2675 | * can be saved into the Image table. |
||
2676 | * |
||
2677 | * @param string $fieldName Name of the field |
||
2678 | * @param mixed $value New field value |
||
2679 | * @return DataObject $this |
||
2680 | */ |
||
2681 | public function setCastedField($fieldName, $val) { |
||
2695 | |||
2696 | /** |
||
2697 | * {@inheritdoc} |
||
2698 | */ |
||
2699 | public function castingHelper($field) { |
||
2717 | |||
2718 | /** |
||
2719 | * Returns true if the given field exists in a database column on any of |
||
2720 | * the objects tables and optionally look up a dynamic getter with |
||
2721 | * get<fieldName>(). |
||
2722 | * |
||
2723 | * @param string $field Name of the field |
||
2724 | * @return boolean True if the given field exists |
||
2725 | */ |
||
2726 | public function hasField($field) { |
||
2734 | |||
2735 | /** |
||
2736 | * Returns true if the given field exists as a database column |
||
2737 | * |
||
2738 | * @param string $field Name of the field |
||
2739 | * |
||
2740 | * @return boolean |
||
2741 | */ |
||
2742 | public function hasDatabaseField($field) { |
||
2747 | |||
2748 | /** |
||
2749 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2750 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2751 | * |
||
2752 | * @param string $field Name of the field |
||
2753 | * @return string The field type of the given field |
||
2754 | */ |
||
2755 | public function hasOwnTableDatabaseField($field) { |
||
2758 | |||
2759 | /** |
||
2760 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2761 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2762 | * |
||
2763 | * @param string $class Class name to check |
||
2764 | * @param string $field Name of the field |
||
2765 | * @return string The field type of the given field |
||
2766 | */ |
||
2767 | public static function has_own_table_database_field($class, $field) { |
||
2780 | |||
2781 | /** |
||
2782 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
2783 | * actually looking in the database. |
||
2784 | * |
||
2785 | * @param string $dataClass |
||
2786 | * @return bool |
||
2787 | */ |
||
2788 | public static function has_own_table($dataClass) { |
||
2803 | |||
2804 | /** |
||
2805 | * Returns true if the member is allowed to do the given action. |
||
2806 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
2807 | * |
||
2808 | * @param string $perm The permission to be checked, such as 'View'. |
||
2809 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
2810 | * in user. |
||
2811 | * |
||
2812 | * @return boolean True if the the member is allowed to do the given action |
||
2813 | */ |
||
2814 | public function can($perm, $member = null) { |
||
2873 | |||
2874 | /** |
||
2875 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
2876 | * expected to return one of three values: |
||
2877 | * |
||
2878 | * - false: Disallow this permission, regardless of what other extensions say |
||
2879 | * - true: Allow this permission, as long as no other extensions return false |
||
2880 | * - NULL: Don't affect the outcome |
||
2881 | * |
||
2882 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
2883 | * |
||
2884 | * <code> |
||
2885 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
2886 | * if($extended !== null) return $extended; |
||
2887 | * else return $normalValue; |
||
2888 | * </code> |
||
2889 | * |
||
2890 | * @param String $methodName Method on the same object, e.g. {@link canEdit()} |
||
2891 | * @param Member|int $member |
||
2892 | * @return boolean|null |
||
2893 | */ |
||
2894 | public function extendedCan($methodName, $member) { |
||
2905 | |||
2906 | /** |
||
2907 | * @param Member $member |
||
2908 | * @return boolean |
||
2909 | */ |
||
2910 | View Code Duplication | public function canView($member = null) { |
|
2917 | |||
2918 | /** |
||
2919 | * @param Member $member |
||
2920 | * @return boolean |
||
2921 | */ |
||
2922 | View Code Duplication | public function canEdit($member = null) { |
|
2929 | |||
2930 | /** |
||
2931 | * @param Member $member |
||
2932 | * @return boolean |
||
2933 | */ |
||
2934 | View Code Duplication | public function canDelete($member = null) { |
|
2941 | |||
2942 | /** |
||
2943 | * @todo Should canCreate be a static method? |
||
2944 | * |
||
2945 | * @param Member $member |
||
2946 | * @return boolean |
||
2947 | */ |
||
2948 | View Code Duplication | public function canCreate($member = null) { |
|
2955 | |||
2956 | /** |
||
2957 | * Debugging used by Debug::show() |
||
2958 | * |
||
2959 | * @return string HTML data representing this object |
||
2960 | */ |
||
2961 | public function debug() { |
||
2969 | |||
2970 | /** |
||
2971 | * Return the DBField object that represents the given field. |
||
2972 | * This works similarly to obj() with 2 key differences: |
||
2973 | * - it still returns an object even when the field has no value. |
||
2974 | * - it only matches fields and not methods |
||
2975 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
2976 | * |
||
2977 | * @param string $fieldName Name of the field |
||
2978 | * @return DBField The field as a DBField object |
||
2979 | */ |
||
2980 | public function dbObject($fieldName) { |
||
3015 | |||
3016 | /** |
||
3017 | * Traverses to a DBField referenced by relationships between data objects. |
||
3018 | * |
||
3019 | * The path to the related field is specified with dot separated syntax |
||
3020 | * (eg: Parent.Child.Child.FieldName). |
||
3021 | * |
||
3022 | * @param string $fieldPath |
||
3023 | * |
||
3024 | * @return mixed DBField of the field on the object or a DataList instance. |
||
3025 | */ |
||
3026 | public function relObject($fieldPath) { |
||
3056 | |||
3057 | /** |
||
3058 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
3059 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
3060 | * |
||
3061 | * @param $fieldPath string |
||
3062 | * @return string | null - will return null on a missing value |
||
3063 | */ |
||
3064 | public function relField($fieldName) { |
||
3099 | |||
3100 | /** |
||
3101 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
3102 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
3103 | * |
||
3104 | * @return String |
||
3105 | */ |
||
3106 | public function getReverseAssociation($className) { |
||
3122 | |||
3123 | /** |
||
3124 | * Return all objects matching the filter |
||
3125 | * sub-classes are automatically selected and included |
||
3126 | * |
||
3127 | * @param string $callerClass The class of objects to be returned |
||
3128 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3129 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
3130 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
3131 | * BY clause. If omitted, self::$default_sort will be used. |
||
3132 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
3133 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
3134 | * @param string $containerClass The container class to return the results in. |
||
3135 | * |
||
3136 | * @todo $containerClass is Ignored, why? |
||
3137 | * |
||
3138 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
3139 | */ |
||
3140 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
3177 | |||
3178 | |||
3179 | /** |
||
3180 | * @deprecated |
||
3181 | */ |
||
3182 | public function Aggregate($class = null) { |
||
3199 | |||
3200 | /** |
||
3201 | * @deprecated |
||
3202 | */ |
||
3203 | public function RelationshipAggregate($relationship) { |
||
3208 | |||
3209 | /** |
||
3210 | * Return the first item matching the given query. |
||
3211 | * All calls to get_one() are cached. |
||
3212 | * |
||
3213 | * @param string $callerClass The class of objects to be returned |
||
3214 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3215 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
3216 | * @param boolean $cache Use caching |
||
3217 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
3218 | * |
||
3219 | * @return DataObject The first item matching the query |
||
3220 | */ |
||
3221 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
3247 | |||
3248 | /** |
||
3249 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
3250 | * Also clears any cached aggregate data. |
||
3251 | * |
||
3252 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
3253 | * When false will just clear session-local cached data |
||
3254 | * @return DataObject $this |
||
3255 | */ |
||
3256 | public function flushCache($persistent = true) { |
||
3274 | |||
3275 | /** |
||
3276 | * Flush the get_one global cache and destroy associated objects. |
||
3277 | */ |
||
3278 | public static function flush_and_destroy_cache() { |
||
3286 | |||
3287 | /** |
||
3288 | * Reset all global caches associated with DataObject. |
||
3289 | */ |
||
3290 | public static function reset() { |
||
3301 | |||
3302 | /** |
||
3303 | * Return the given element, searching by ID |
||
3304 | * |
||
3305 | * @param string $callerClass The class of the object to be returned |
||
3306 | * @param int $id The id of the element |
||
3307 | * @param boolean $cache See {@link get_one()} |
||
3308 | * |
||
3309 | * @return DataObject The element |
||
3310 | */ |
||
3311 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
3328 | |||
3329 | /** |
||
3330 | * Get the name of the base table for this object |
||
3331 | */ |
||
3332 | public function baseTable() { |
||
3336 | |||
3337 | /** |
||
3338 | * @var Array Parameters used in the query that built this object. |
||
3339 | * This can be used by decorators (e.g. lazy loading) to |
||
3340 | * run additional queries using the same context. |
||
3341 | */ |
||
3342 | protected $sourceQueryParams; |
||
3343 | |||
3344 | /** |
||
3345 | * @see $sourceQueryParams |
||
3346 | * @return array |
||
3347 | */ |
||
3348 | public function getSourceQueryParams() { |
||
3351 | |||
3352 | /** |
||
3353 | * @see $sourceQueryParams |
||
3354 | * @param array |
||
3355 | */ |
||
3356 | public function setSourceQueryParams($array) { |
||
3359 | |||
3360 | /** |
||
3361 | * @see $sourceQueryParams |
||
3362 | * @param array |
||
3363 | */ |
||
3364 | public function setSourceQueryParam($key, $value) { |
||
3367 | |||
3368 | /** |
||
3369 | * @see $sourceQueryParams |
||
3370 | * @return Mixed |
||
3371 | */ |
||
3372 | public function getSourceQueryParam($key) { |
||
3376 | |||
3377 | //-------------------------------------------------------------------------------------------// |
||
3378 | |||
3379 | /** |
||
3380 | * Return the database indexes on this table. |
||
3381 | * This array is indexed by the name of the field with the index, and |
||
3382 | * the value is the type of index. |
||
3383 | */ |
||
3384 | public function databaseIndexes() { |
||
3409 | |||
3410 | /** |
||
3411 | * Check the database schema and update it as necessary. |
||
3412 | * |
||
3413 | * @uses DataExtension->augmentDatabase() |
||
3414 | */ |
||
3415 | public function requireTable() { |
||
3460 | |||
3461 | /** |
||
3462 | * Validate that the configured relations for this class use the correct syntaxes |
||
3463 | * @throws LogicException |
||
3464 | */ |
||
3465 | protected function validateModelDefinitions() { |
||
3496 | |||
3497 | /** |
||
3498 | * Add default records to database. This function is called whenever the |
||
3499 | * database is built, after the database tables have all been created. Overload |
||
3500 | * this to add default records when the database is built, but make sure you |
||
3501 | * call parent::requireDefaultRecords(). |
||
3502 | * |
||
3503 | * @uses DataExtension->requireDefaultRecords() |
||
3504 | */ |
||
3505 | public function requireDefaultRecords() { |
||
3523 | |||
3524 | /** |
||
3525 | * Returns fields bu traversing the class heirachy in a bottom-up direction. |
||
3526 | * |
||
3527 | * Needed to avoid getCMSFields being empty when customDatabaseFields overlooks |
||
3528 | * the inheritance chain of the $db array, where a child data object has no $db array, |
||
3529 | * but still needs to know the properties of its parent. This should be merged into databaseFields or |
||
3530 | * customDatabaseFields. |
||
3531 | * |
||
3532 | * @todo review whether this is still needed after recent API changes |
||
3533 | */ |
||
3534 | public function inheritedDatabaseFields() { |
||
3545 | |||
3546 | /** |
||
3547 | * Get the default searchable fields for this object, as defined in the |
||
3548 | * $searchable_fields list. If searchable fields are not defined on the |
||
3549 | * data object, uses a default selection of summary fields. |
||
3550 | * |
||
3551 | * @return array |
||
3552 | */ |
||
3553 | public function searchableFields() { |
||
3627 | |||
3628 | /** |
||
3629 | * Get any user defined searchable fields labels that |
||
3630 | * exist. Allows overriding of default field names in the form |
||
3631 | * interface actually presented to the user. |
||
3632 | * |
||
3633 | * The reason for keeping this separate from searchable_fields, |
||
3634 | * which would be a logical place for this functionality, is to |
||
3635 | * avoid bloating and complicating the configuration array. Currently |
||
3636 | * much of this system is based on sensible defaults, and this property |
||
3637 | * would generally only be set in the case of more complex relationships |
||
3638 | * between data object being required in the search interface. |
||
3639 | * |
||
3640 | * Generates labels based on name of the field itself, if no static property |
||
3641 | * {@link self::field_labels} exists. |
||
3642 | * |
||
3643 | * @uses $field_labels |
||
3644 | * @uses FormField::name_to_label() |
||
3645 | * |
||
3646 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
3647 | * |
||
3648 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
3649 | */ |
||
3650 | public function fieldLabels($includerelations = true) { |
||
3685 | |||
3686 | /** |
||
3687 | * Get a human-readable label for a single field, |
||
3688 | * see {@link fieldLabels()} for more details. |
||
3689 | * |
||
3690 | * @uses fieldLabels() |
||
3691 | * @uses FormField::name_to_label() |
||
3692 | * |
||
3693 | * @param string $name Name of the field |
||
3694 | * @return string Label of the field |
||
3695 | */ |
||
3696 | public function fieldLabel($name) { |
||
3700 | |||
3701 | /** |
||
3702 | * Get the default summary fields for this object. |
||
3703 | * |
||
3704 | * @todo use the translation apparatus to return a default field selection for the language |
||
3705 | * |
||
3706 | * @return array |
||
3707 | */ |
||
3708 | public function summaryFields() { |
||
3741 | |||
3742 | /** |
||
3743 | * Defines a default list of filters for the search context. |
||
3744 | * |
||
3745 | * If a filter class mapping is defined on the data object, |
||
3746 | * it is constructed here. Otherwise, the default filter specified in |
||
3747 | * {@link DBField} is used. |
||
3748 | * |
||
3749 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
3750 | * |
||
3751 | * @return array |
||
3752 | */ |
||
3753 | public function defaultSearchFilters() { |
||
3774 | |||
3775 | /** |
||
3776 | * @return boolean True if the object is in the database |
||
3777 | */ |
||
3778 | public function isInDB() { |
||
3781 | |||
3782 | /* |
||
3783 | * @ignore |
||
3784 | */ |
||
3785 | private static $subclass_access = true; |
||
3786 | |||
3787 | /** |
||
3788 | * Temporarily disable subclass access in data object qeur |
||
3789 | */ |
||
3790 | public static function disable_subclass_access() { |
||
3796 | |||
3797 | //-------------------------------------------------------------------------------------------// |
||
3798 | |||
3799 | /** |
||
3800 | * Database field definitions. |
||
3801 | * This is a map from field names to field type. The field |
||
3802 | * type should be a class that extends . |
||
3803 | * @var array |
||
3804 | * @config |
||
3805 | */ |
||
3806 | private static $db = null; |
||
3807 | |||
3808 | /** |
||
3809 | * Use a casting object for a field. This is a map from |
||
3810 | * field name to class name of the casting object. |
||
3811 | * @var array |
||
3812 | */ |
||
3813 | private static $casting = array( |
||
3814 | "ID" => 'Int', |
||
3815 | "ClassName" => 'Varchar', |
||
3816 | "LastEdited" => "SS_Datetime", |
||
3817 | "Created" => "SS_Datetime", |
||
3818 | "Title" => 'Text', |
||
3819 | ); |
||
3820 | |||
3821 | /** |
||
3822 | * Specify custom options for a CREATE TABLE call. |
||
3823 | * Can be used to specify a custom storage engine for specific database table. |
||
3824 | * All options have to be keyed for a specific database implementation, |
||
3825 | * identified by their class name (extending from {@link SS_Database}). |
||
3826 | * |
||
3827 | * <code> |
||
3828 | * array( |
||
3829 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
3830 | * ) |
||
3831 | * </code> |
||
3832 | * |
||
3833 | * Caution: This API is experimental, and might not be |
||
3834 | * included in the next major release. Please use with care. |
||
3835 | * |
||
3836 | * @var array |
||
3837 | * @config |
||
3838 | */ |
||
3839 | private static $create_table_options = array( |
||
3840 | 'MySQLDatabase' => 'ENGINE=InnoDB' |
||
3841 | ); |
||
3842 | |||
3843 | /** |
||
3844 | * If a field is in this array, then create a database index |
||
3845 | * on that field. This is a map from fieldname to index type. |
||
3846 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
3847 | * |
||
3848 | * @var array |
||
3849 | * @config |
||
3850 | */ |
||
3851 | private static $indexes = null; |
||
3852 | |||
3853 | /** |
||
3854 | * Inserts standard column-values when a DataObject |
||
3855 | * is instanciated. Does not insert default records {@see $default_records}. |
||
3856 | * This is a map from fieldname to default value. |
||
3857 | * |
||
3858 | * - If you would like to change a default value in a sub-class, just specify it. |
||
3859 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
3860 | * or false in your subclass. Setting it to null won't work. |
||
3861 | * |
||
3862 | * @var array |
||
3863 | * @config |
||
3864 | */ |
||
3865 | private static $defaults = null; |
||
3866 | |||
3867 | /** |
||
3868 | * Multidimensional array which inserts default data into the database |
||
3869 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
3870 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
3871 | * behaviour such as publishing and ParentNodes. |
||
3872 | * |
||
3873 | * Example: |
||
3874 | * array( |
||
3875 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
3876 | * array('Title' => "DefaultPage2") |
||
3877 | * ). |
||
3878 | * |
||
3879 | * @var array |
||
3880 | * @config |
||
3881 | */ |
||
3882 | private static $default_records = null; |
||
3883 | |||
3884 | /** |
||
3885 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
3886 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
3887 | * |
||
3888 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3889 | * |
||
3890 | * @var array |
||
3891 | * @config |
||
3892 | */ |
||
3893 | private static $has_one = null; |
||
3894 | |||
3895 | /** |
||
3896 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
3897 | * |
||
3898 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
3899 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
3900 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
3901 | * |
||
3902 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3903 | * |
||
3904 | * @var array |
||
3905 | * @config |
||
3906 | */ |
||
3907 | private static $belongs_to; |
||
3908 | |||
3909 | /** |
||
3910 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
3911 | * |
||
3912 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
3913 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
3914 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
3915 | * which foreign key to use. |
||
3916 | * |
||
3917 | * @var array |
||
3918 | * @config |
||
3919 | */ |
||
3920 | private static $has_many = null; |
||
3921 | |||
3922 | /** |
||
3923 | * many-many relationship definitions. |
||
3924 | * This is a map from component name to data type. |
||
3925 | * @var array |
||
3926 | * @config |
||
3927 | */ |
||
3928 | private static $many_many = null; |
||
3929 | |||
3930 | /** |
||
3931 | * Extra fields to include on the connecting many-many table. |
||
3932 | * This is a map from field name to field type. |
||
3933 | * |
||
3934 | * Example code: |
||
3935 | * <code> |
||
3936 | * public static $many_many_extraFields = array( |
||
3937 | * 'Members' => array( |
||
3938 | * 'Role' => 'Varchar(100)' |
||
3939 | * ) |
||
3940 | * ); |
||
3941 | * </code> |
||
3942 | * |
||
3943 | * @var array |
||
3944 | * @config |
||
3945 | */ |
||
3946 | private static $many_many_extraFields = null; |
||
3947 | |||
3948 | /** |
||
3949 | * The inverse side of a many-many relationship. |
||
3950 | * This is a map from component name to data type. |
||
3951 | * @var array |
||
3952 | * @config |
||
3953 | */ |
||
3954 | private static $belongs_many_many = null; |
||
3955 | |||
3956 | /** |
||
3957 | * The default sort expression. This will be inserted in the ORDER BY |
||
3958 | * clause of a SQL query if no other sort expression is provided. |
||
3959 | * @var string |
||
3960 | * @config |
||
3961 | */ |
||
3962 | private static $default_sort = null; |
||
3963 | |||
3964 | /** |
||
3965 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
3966 | * search interface. |
||
3967 | * |
||
3968 | * Overriding the default filter, with a custom defined filter: |
||
3969 | * <code> |
||
3970 | * static $searchable_fields = array( |
||
3971 | * "Name" => "PartialMatchFilter" |
||
3972 | * ); |
||
3973 | * </code> |
||
3974 | * |
||
3975 | * Overriding the default form fields, with a custom defined field. |
||
3976 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
3977 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
3978 | * <code> |
||
3979 | * static $searchable_fields = array( |
||
3980 | * "Name" => array( |
||
3981 | * "field" => "TextField" |
||
3982 | * ) |
||
3983 | * ); |
||
3984 | * </code> |
||
3985 | * |
||
3986 | * Overriding the default form field, filter and title: |
||
3987 | * <code> |
||
3988 | * static $searchable_fields = array( |
||
3989 | * "Organisation.ZipCode" => array( |
||
3990 | * "field" => "TextField", |
||
3991 | * "filter" => "PartialMatchFilter", |
||
3992 | * "title" => 'Organisation ZIP' |
||
3993 | * ) |
||
3994 | * ); |
||
3995 | * </code> |
||
3996 | * @config |
||
3997 | */ |
||
3998 | private static $searchable_fields = null; |
||
3999 | |||
4000 | /** |
||
4001 | * User defined labels for searchable_fields, used to override |
||
4002 | * default display in the search form. |
||
4003 | * @config |
||
4004 | */ |
||
4005 | private static $field_labels = null; |
||
4006 | |||
4007 | /** |
||
4008 | * Provides a default list of fields to be used by a 'summary' |
||
4009 | * view of this object. |
||
4010 | * @config |
||
4011 | */ |
||
4012 | private static $summary_fields = null; |
||
4013 | |||
4014 | /** |
||
4015 | * Provides a list of allowed methods that can be called via RESTful api. |
||
4016 | */ |
||
4017 | public static $allowed_actions = null; |
||
4018 | |||
4019 | /** |
||
4020 | * Collect all static properties on the object |
||
4021 | * which contain natural language, and need to be translated. |
||
4022 | * The full entity name is composed from the class name and a custom identifier. |
||
4023 | * |
||
4024 | * @return array A numerical array which contains one or more entities in array-form. |
||
4025 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
4026 | * $entity, $string, $priority, $context. |
||
4027 | */ |
||
4028 | public function provideI18nEntities() { |
||
4046 | |||
4047 | /** |
||
4048 | * Returns true if the given method/parameter has a value |
||
4049 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
4050 | * |
||
4051 | * @param string $field The field name |
||
4052 | * @param array $arguments |
||
4053 | * @param bool $cache |
||
4054 | * @return boolean |
||
4055 | */ |
||
4056 | public function hasValue($field, $arguments = null, $cache = true) { |
||
4064 | |||
4065 | } |
||
4066 |
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.