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 |
||
97 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
98 | |||
99 | /** |
||
100 | * Human-readable singular name. |
||
101 | * @var string |
||
102 | * @config |
||
103 | */ |
||
104 | private static $singular_name = null; |
||
105 | |||
106 | /** |
||
107 | * Human-readable plural name |
||
108 | * @var string |
||
109 | * @config |
||
110 | */ |
||
111 | private static $plural_name = null; |
||
112 | |||
113 | /** |
||
114 | * Allow API access to this object? |
||
115 | * @todo Define the options that can be set here |
||
116 | * @config |
||
117 | */ |
||
118 | private static $api_access = false; |
||
119 | |||
120 | /** |
||
121 | * Allows specification of a default value for the ClassName field. |
||
122 | * Configure this value only in subclasses of DataObject. |
||
123 | * |
||
124 | * @config |
||
125 | * @var string |
||
126 | */ |
||
127 | private static $default_classname = null; |
||
128 | |||
129 | /** |
||
130 | * True if this DataObject has been destroyed. |
||
131 | * @var boolean |
||
132 | */ |
||
133 | public $destroyed = false; |
||
134 | |||
135 | /** |
||
136 | * The DataModel from this this object comes |
||
137 | */ |
||
138 | protected $model; |
||
139 | |||
140 | /** |
||
141 | * Data stored in this objects database record. An array indexed by fieldname. |
||
142 | * |
||
143 | * Use {@link toMap()} if you want an array representation |
||
144 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | protected $record; |
||
149 | |||
150 | /** |
||
151 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
152 | */ |
||
153 | const CHANGE_NONE = 0; |
||
154 | |||
155 | /** |
||
156 | * Represents a field that has changed type, although not the loosely defined value. |
||
157 | * (before !== after && before == after) |
||
158 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
159 | * Value changes are by nature also considered strict changes. |
||
160 | */ |
||
161 | const CHANGE_STRICT = 1; |
||
162 | |||
163 | /** |
||
164 | * Represents a field that has changed the loosely defined value |
||
165 | * (before != after, thus, before !== after)) |
||
166 | * E.g. change false to true, but not false to 0 |
||
167 | */ |
||
168 | const CHANGE_VALUE = 2; |
||
169 | |||
170 | /** |
||
171 | * An array indexed by fieldname, true if the field has been changed. |
||
172 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
173 | * the changed state. |
||
174 | * |
||
175 | * @var array |
||
176 | */ |
||
177 | private $changed; |
||
178 | |||
179 | /** |
||
180 | * The database record (in the same format as $record), before |
||
181 | * any changes. |
||
182 | * @var array |
||
183 | */ |
||
184 | protected $original; |
||
185 | |||
186 | /** |
||
187 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
188 | * @var boolean |
||
189 | */ |
||
190 | protected $brokenOnDelete = false; |
||
191 | |||
192 | /** |
||
193 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
194 | * @var boolean |
||
195 | */ |
||
196 | protected $brokenOnWrite = false; |
||
197 | |||
198 | /** |
||
199 | * @config |
||
200 | * @var boolean Should dataobjects be validated before they are written? |
||
201 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
202 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
203 | * to only disable validation for very specific use cases. |
||
204 | */ |
||
205 | private static $validation_enabled = true; |
||
206 | |||
207 | /** |
||
208 | * Static caches used by relevant functions. |
||
209 | */ |
||
210 | protected static $_cache_has_own_table = array(); |
||
211 | protected static $_cache_get_one; |
||
212 | protected static $_cache_get_class_ancestry; |
||
213 | protected static $_cache_composite_fields = array(); |
||
214 | protected static $_cache_database_fields = array(); |
||
215 | protected static $_cache_field_labels = array(); |
||
216 | |||
217 | /** |
||
218 | * Base fields which are not defined in static $db |
||
219 | * |
||
220 | * @config |
||
221 | * @var array |
||
222 | */ |
||
223 | private static $fixed_fields = array( |
||
224 | 'ID' => 'PrimaryKey', |
||
225 | 'ClassName' => 'DBClassName', |
||
226 | 'LastEdited' => 'SS_Datetime', |
||
227 | 'Created' => 'SS_Datetime', |
||
228 | ); |
||
229 | |||
230 | /** |
||
231 | * Core dataobject extensions |
||
232 | * |
||
233 | * @config |
||
234 | * @var array |
||
235 | */ |
||
236 | private static $extensions = array( |
||
|
|||
237 | 'AssetControl' => '\\SilverStripe\\Filesystem\\AssetControlExtension' |
||
238 | ); |
||
239 | |||
240 | /** |
||
241 | * Non-static relationship cache, indexed by component name. |
||
242 | */ |
||
243 | protected $components; |
||
244 | |||
245 | /** |
||
246 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
247 | */ |
||
248 | protected $unsavedRelations; |
||
249 | |||
250 | /** |
||
251 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
252 | * "ID" will be included on every table. |
||
253 | * |
||
254 | * Composite DB field specifications are returned by reference if necessary, but not in the return |
||
255 | * array. |
||
256 | * |
||
257 | * Can be called directly on an object. E.g. Member::database_fields() |
||
258 | * |
||
259 | * @param string $class Class name to query from |
||
260 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
261 | */ |
||
262 | public static function database_fields($class = null) { |
||
273 | |||
274 | /** |
||
275 | * Cache all database and composite fields for the given class. |
||
276 | * Will do nothing if already cached |
||
277 | * |
||
278 | * @param string $class Class name to cache |
||
279 | */ |
||
280 | protected static function cache_database_fields($class) { |
||
335 | |||
336 | /** |
||
337 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
338 | * and {@link DataObject::$has_one}. Resolves instances of {@link DBComposite} |
||
339 | * into the actual database fields, rather than the name of the field which |
||
340 | * might not equate a database column. |
||
341 | * |
||
342 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
343 | * see {@link database_fields()}. |
||
344 | * |
||
345 | * Can be called directly on an object. E.g. Member::custom_database_fields() |
||
346 | * |
||
347 | * @uses DBComposite->compositeDatabaseFields() |
||
348 | * |
||
349 | * @param string $class Class name to query from |
||
350 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
351 | */ |
||
352 | public static function custom_database_fields($class = null) { |
||
364 | |||
365 | /** |
||
366 | * Returns the field class if the given db field on the class is a composite field. |
||
367 | * Will check all applicable ancestor classes and aggregate results. |
||
368 | * |
||
369 | * @param string $class Class to check |
||
370 | * @param string $name Field to check |
||
371 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
372 | * @return string|false Class spec name of composite field if it exists, or false if not |
||
373 | */ |
||
374 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
378 | |||
379 | /** |
||
380 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
381 | * Will check all applicable ancestor classes and aggregate results. |
||
382 | * |
||
383 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
384 | * to aggregate. |
||
385 | * |
||
386 | * Includes composite has_one (Polymorphic) fields |
||
387 | * |
||
388 | * @param string $class Name of class to check |
||
389 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
390 | * @return array List of composite fields and their class spec |
||
391 | */ |
||
392 | public static function composite_fields($class = null, $aggregated = true) { |
||
416 | |||
417 | /** |
||
418 | * Construct a new DataObject. |
||
419 | * |
||
420 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
421 | * field values. Normally this contructor is only used by the internal systems that get objects from the database. |
||
422 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
423 | * Singletons don't have their defaults set. |
||
424 | * @param DataModel $model |
||
425 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
426 | */ |
||
427 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) { |
||
500 | |||
501 | /** |
||
502 | * Set the DataModel |
||
503 | * @param DataModel $model |
||
504 | * @return DataObject $this |
||
505 | */ |
||
506 | public function setDataModel(DataModel $model) { |
||
510 | |||
511 | /** |
||
512 | * Destroy all of this objects dependant objects and local caches. |
||
513 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
514 | */ |
||
515 | public function destroy() { |
||
520 | |||
521 | /** |
||
522 | * Create a duplicate of this node. |
||
523 | * Note: now also duplicates relations. |
||
524 | * |
||
525 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
526 | * If this is true, it will create the duplicate in the database. |
||
527 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
528 | */ |
||
529 | public function duplicate($doWrite = true) { |
||
543 | |||
544 | /** |
||
545 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
546 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
547 | * automatically when adding the new relations. |
||
548 | * |
||
549 | * @param DataObject $sourceObject the source object to duplicate from |
||
550 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
551 | * @return DataObject with the new many_many relations copied in |
||
552 | */ |
||
553 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
573 | |||
574 | /** |
||
575 | * Helper function to duplicate relations from one object to another |
||
576 | * @param $sourceObject the source object to duplicate from |
||
577 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
578 | * @param $name the name of the relation to duplicate (e.g. members) |
||
579 | */ |
||
580 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
594 | |||
595 | public function getObsoleteClassName() { |
||
599 | |||
600 | public function getClassName() { |
||
605 | |||
606 | /** |
||
607 | * Set the ClassName attribute. {@link $class} is also updated. |
||
608 | * Warning: This will produce an inconsistent record, as the object |
||
609 | * instance will not automatically switch to the new subclass. |
||
610 | * Please use {@link newClassInstance()} for this purpose, |
||
611 | * or destroy and reinstanciate the record. |
||
612 | * |
||
613 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
614 | * @return DataObject $this |
||
615 | */ |
||
616 | public function setClassName($className) { |
||
624 | |||
625 | /** |
||
626 | * Create a new instance of a different class from this object's record. |
||
627 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
628 | * it ensures that the instance of the class is a match for the className of the |
||
629 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
630 | * property manually before calling this method, as it will confuse change detection. |
||
631 | * |
||
632 | * If the new class is different to the original class, defaults are populated again |
||
633 | * because this will only occur automatically on instantiation of a DataObject if |
||
634 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
635 | * we still need to repopulate the defaults. |
||
636 | * |
||
637 | * @param string $newClassName The name of the new class |
||
638 | * |
||
639 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
640 | */ |
||
641 | public function newClassInstance($newClassName) { |
||
659 | |||
660 | /** |
||
661 | * Adds methods from the extensions. |
||
662 | * Called by Object::__construct() once per class. |
||
663 | */ |
||
664 | public function defineMethods() { |
||
703 | |||
704 | /** |
||
705 | * Returns true if this object "exists", i.e., has a sensible value. |
||
706 | * The default behaviour for a DataObject is to return true if |
||
707 | * the object exists in the database, you can override this in subclasses. |
||
708 | * |
||
709 | * @return boolean true if this object exists |
||
710 | */ |
||
711 | public function exists() { |
||
714 | |||
715 | /** |
||
716 | * Returns TRUE if all values (other than "ID") are |
||
717 | * considered empty (by weak boolean comparison). |
||
718 | * |
||
719 | * @return boolean |
||
720 | */ |
||
721 | public function isEmpty() { |
||
739 | |||
740 | /** |
||
741 | * Pluralise this item given a specific count. |
||
742 | * |
||
743 | * E.g. "0 Pages", "1 File", "3 Images" |
||
744 | * |
||
745 | * @param string $count |
||
746 | * @param bool $prependNumber Include number in result. Defaults to true. |
||
747 | * @return string |
||
748 | */ |
||
749 | public function i18n_pluralise($count, $prependNumber = true) { |
||
757 | |||
758 | /** |
||
759 | * Get the user friendly singular name of this DataObject. |
||
760 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
761 | * this returns the class name. |
||
762 | * |
||
763 | * @return string User friendly singular name of this DataObject |
||
764 | */ |
||
765 | public function singular_name() { |
||
772 | |||
773 | /** |
||
774 | * Get the translated user friendly singular name of this DataObject |
||
775 | * same as singular_name() but runs it through the translating function |
||
776 | * |
||
777 | * Translating string is in the form: |
||
778 | * $this->class.SINGULARNAME |
||
779 | * Example: |
||
780 | * Page.SINGULARNAME |
||
781 | * |
||
782 | * @return string User friendly translated singular name of this DataObject |
||
783 | */ |
||
784 | public function i18n_singular_name() { |
||
787 | |||
788 | /** |
||
789 | * Get the user friendly plural name of this DataObject |
||
790 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
791 | * this returns a pluralised version of the class name. |
||
792 | * |
||
793 | * @return string User friendly plural name of this DataObject |
||
794 | */ |
||
795 | public function plural_name() { |
||
807 | |||
808 | /** |
||
809 | * Get the translated user friendly plural name of this DataObject |
||
810 | * Same as plural_name but runs it through the translation function |
||
811 | * Translation string is in the form: |
||
812 | * $this->class.PLURALNAME |
||
813 | * Example: |
||
814 | * Page.PLURALNAME |
||
815 | * |
||
816 | * @return string User friendly translated plural name of this DataObject |
||
817 | */ |
||
818 | public function i18n_plural_name() |
||
823 | |||
824 | /** |
||
825 | * Standard implementation of a title/label for a specific |
||
826 | * record. Tries to find properties 'Title' or 'Name', |
||
827 | * and falls back to the 'ID'. Useful to provide |
||
828 | * user-friendly identification of a record, e.g. in errormessages |
||
829 | * or UI-selections. |
||
830 | * |
||
831 | * Overload this method to have a more specialized implementation, |
||
832 | * e.g. for an Address record this could be: |
||
833 | * <code> |
||
834 | * function getTitle() { |
||
835 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
836 | * } |
||
837 | * </code> |
||
838 | * |
||
839 | * @return string |
||
840 | */ |
||
841 | public function getTitle() { |
||
847 | |||
848 | /** |
||
849 | * Returns the associated database record - in this case, the object itself. |
||
850 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
851 | * |
||
852 | * @return DataObject Associated database record |
||
853 | */ |
||
854 | public function data() { |
||
857 | |||
858 | /** |
||
859 | * Convert this object to a map. |
||
860 | * |
||
861 | * @return array The data as a map. |
||
862 | */ |
||
863 | public function toMap() { |
||
867 | |||
868 | /** |
||
869 | * Return all currently fetched database fields. |
||
870 | * |
||
871 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
872 | * Obviously, this makes it a lot faster. |
||
873 | * |
||
874 | * @return array The data as a map. |
||
875 | */ |
||
876 | public function getQueriedDatabaseFields() { |
||
879 | |||
880 | /** |
||
881 | * Update a number of fields on this object, given a map of the desired changes. |
||
882 | * |
||
883 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
884 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
885 | * |
||
886 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
887 | * the related objects that it alters. |
||
888 | * |
||
889 | * @param array $data A map of field name to data values to update. |
||
890 | * @return DataObject $this |
||
891 | */ |
||
892 | public function update($data) { |
||
939 | |||
940 | /** |
||
941 | * Pass changes as a map, and try to |
||
942 | * get automatic casting for these fields. |
||
943 | * Doesn't write to the database. To write the data, |
||
944 | * use the write() method. |
||
945 | * |
||
946 | * @param array $data A map of field name to data values to update. |
||
947 | * @return DataObject $this |
||
948 | */ |
||
949 | public function castedUpdate($data) { |
||
955 | |||
956 | /** |
||
957 | * Merges data and relations from another object of same class, |
||
958 | * without conflict resolution. Allows to specify which |
||
959 | * dataset takes priority in case its not empty. |
||
960 | * has_one-relations are just transferred with priority 'right'. |
||
961 | * has_many and many_many-relations are added regardless of priority. |
||
962 | * |
||
963 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
964 | * meaning they are not connected to the merged object any longer. |
||
965 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
966 | * doesn't write the updated object itself (just writes the object-properties). |
||
967 | * Caution: Does not delete the merged object. |
||
968 | * Caution: Does now overwrite Created date on the original object. |
||
969 | * |
||
970 | * @param $obj DataObject |
||
971 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
972 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
973 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
974 | * Only applicable with $priority='right'. (optional) |
||
975 | * @return Boolean |
||
976 | */ |
||
977 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
1050 | |||
1051 | /** |
||
1052 | * Forces the record to think that all its data has changed. |
||
1053 | * Doesn't write to the database. Only sets fields as changed |
||
1054 | * if they are not already marked as changed. |
||
1055 | * |
||
1056 | * @return DataObject $this |
||
1057 | */ |
||
1058 | public function forceChange() { |
||
1078 | |||
1079 | /** |
||
1080 | * Validate the current object. |
||
1081 | * |
||
1082 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
1083 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
1084 | * |
||
1085 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
1086 | * and onAfterWrite() won't get called either. |
||
1087 | * |
||
1088 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
1089 | * attempting a write, and respond appropriately if it isn't. |
||
1090 | * |
||
1091 | * @see {@link ValidationResult} |
||
1092 | * @return ValidationResult |
||
1093 | */ |
||
1094 | public function validate() { |
||
1099 | |||
1100 | /** |
||
1101 | * Public accessor for {@see DataObject::validate()} |
||
1102 | * |
||
1103 | * @return ValidationResult |
||
1104 | */ |
||
1105 | public function doValidate() { |
||
1109 | |||
1110 | /** |
||
1111 | * Event handler called before writing to the database. |
||
1112 | * You can overload this to clean up or otherwise process data before writing it to the |
||
1113 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
1114 | * |
||
1115 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
1116 | * |
||
1117 | * @uses DataExtension->onBeforeWrite() |
||
1118 | */ |
||
1119 | protected function onBeforeWrite() { |
||
1125 | |||
1126 | /** |
||
1127 | * Event handler called after writing to the database. |
||
1128 | * You can overload this to act upon changes made to the data after it is written. |
||
1129 | * $this->changed will have a record |
||
1130 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
1131 | * |
||
1132 | * @uses DataExtension->onAfterWrite() |
||
1133 | */ |
||
1134 | protected function onAfterWrite() { |
||
1138 | |||
1139 | /** |
||
1140 | * Event handler called before deleting from the database. |
||
1141 | * You can overload this to clean up or otherwise process data before delete this |
||
1142 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
1143 | * |
||
1144 | * @uses DataExtension->onBeforeDelete() |
||
1145 | */ |
||
1146 | protected function onBeforeDelete() { |
||
1152 | |||
1153 | protected function onAfterDelete() { |
||
1156 | |||
1157 | /** |
||
1158 | * Load the default values in from the self::$defaults array. |
||
1159 | * Will traverse the defaults of the current class and all its parent classes. |
||
1160 | * Called by the constructor when creating new records. |
||
1161 | * |
||
1162 | * @uses DataExtension->populateDefaults() |
||
1163 | * @return DataObject $this |
||
1164 | */ |
||
1165 | public function populateDefaults() { |
||
1196 | |||
1197 | /** |
||
1198 | * Determine validation of this object prior to write |
||
1199 | * |
||
1200 | * @return ValidationException Exception generated by this write, or null if valid |
||
1201 | */ |
||
1202 | protected function validateWrite() { |
||
1222 | |||
1223 | /** |
||
1224 | * Prepare an object prior to write |
||
1225 | * |
||
1226 | * @throws ValidationException |
||
1227 | */ |
||
1228 | protected function preWrite() { |
||
1244 | |||
1245 | /** |
||
1246 | * Detects and updates all changes made to this object |
||
1247 | * |
||
1248 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
1249 | * @return bool True if any changes are detected |
||
1250 | */ |
||
1251 | protected function updateChanges($forceChanges = false) { |
||
1268 | |||
1269 | /** |
||
1270 | * Writes a subset of changes for a specific table to the given manipulation |
||
1271 | * |
||
1272 | * @param string $baseTable Base table |
||
1273 | * @param string $now Timestamp to use for the current time |
||
1274 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
1275 | * @param array $manipulation Manipulation to write to |
||
1276 | * @param string $class Table and Class to select and write to |
||
1277 | */ |
||
1278 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
1322 | |||
1323 | /** |
||
1324 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
1325 | * |
||
1326 | * Does nothing if an ID is already assigned for this record |
||
1327 | * |
||
1328 | * @param string $baseTable Base table |
||
1329 | * @param string $now Timestamp to use for the current time |
||
1330 | */ |
||
1331 | protected function writeBaseRecord($baseTable, $now) { |
||
1343 | |||
1344 | /** |
||
1345 | * Generate and write the database manipulation for all changed fields |
||
1346 | * |
||
1347 | * @param string $baseTable Base table |
||
1348 | * @param string $now Timestamp to use for the current time |
||
1349 | * @param bool $isNewRecord If this is a new record |
||
1350 | */ |
||
1351 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
1372 | |||
1373 | /** |
||
1374 | * Writes all changes to this object to the database. |
||
1375 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
1376 | * - All relevant tables will be updated. |
||
1377 | * - $this->onBeforeWrite() gets called beforehand. |
||
1378 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
1379 | * |
||
1380 | * @uses DataExtension->augmentWrite() |
||
1381 | * |
||
1382 | * @param boolean $showDebug Show debugging information |
||
1383 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
1384 | * @param boolean $forceWrite Write to database even if there are no changes |
||
1385 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
1386 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
1387 | * {@link getManyManyComponents()} (Default: false) |
||
1388 | * @return int The ID of the record |
||
1389 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
1390 | */ |
||
1391 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
1436 | |||
1437 | /** |
||
1438 | * Writes cached relation lists to the database, if possible |
||
1439 | */ |
||
1440 | public function writeRelations() { |
||
1451 | |||
1452 | /** |
||
1453 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
1454 | * same record. |
||
1455 | * |
||
1456 | * @param $recursive Recursively write components |
||
1457 | * @return DataObject $this |
||
1458 | */ |
||
1459 | public function writeComponents($recursive = false) { |
||
1467 | |||
1468 | /** |
||
1469 | * Delete this data object. |
||
1470 | * $this->onBeforeDelete() gets called. |
||
1471 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
1472 | * @uses DataExtension->augmentSQL() |
||
1473 | */ |
||
1474 | public function delete() { |
||
1502 | |||
1503 | /** |
||
1504 | * Delete the record with the given ID. |
||
1505 | * |
||
1506 | * @param string $className The class name of the record to be deleted |
||
1507 | * @param int $id ID of record to be deleted |
||
1508 | */ |
||
1509 | public static function delete_by_id($className, $id) { |
||
1517 | |||
1518 | /** |
||
1519 | * Get the class ancestry, including the current class name. |
||
1520 | * The ancestry will be returned as an array of class names, where the 0th element |
||
1521 | * will be the class that inherits directly from DataObject, and the last element |
||
1522 | * will be the current class. |
||
1523 | * |
||
1524 | * @return array Class ancestry |
||
1525 | */ |
||
1526 | public function getClassAncestry() { |
||
1535 | |||
1536 | /** |
||
1537 | * Return a component object from a one to one relationship, as a DataObject. |
||
1538 | * If no component is available, an 'empty component' will be returned for |
||
1539 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
1540 | * |
||
1541 | * @param string $componentName Name of the component |
||
1542 | * @return DataObject The component object. It's exact type will be that of the component. |
||
1543 | * @throws Exception |
||
1544 | */ |
||
1545 | public function getComponent($componentName) { |
||
1613 | |||
1614 | /** |
||
1615 | * Returns a one-to-many relation as a HasManyList |
||
1616 | * |
||
1617 | * @param string $componentName Name of the component |
||
1618 | * @return HasManyList The components of the one-to-many relationship. |
||
1619 | */ |
||
1620 | public function getComponents($componentName) { |
||
1658 | |||
1659 | /** |
||
1660 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
1661 | * |
||
1662 | * @param string $relationName Relation name. |
||
1663 | * @return string Class name, or null if not found. |
||
1664 | */ |
||
1665 | public function getRelationClass($relationName) { |
||
1689 | |||
1690 | /** |
||
1691 | * Given a relation name, determine the relation type |
||
1692 | * |
||
1693 | * @param string $component Name of component |
||
1694 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
1695 | */ |
||
1696 | public function getRelationType($component) { |
||
1706 | |||
1707 | /** |
||
1708 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
1709 | * side of the relation. |
||
1710 | * |
||
1711 | * Notes on behaviour: |
||
1712 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
1713 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
1714 | * - Cannot be used on polymorphic relationships |
||
1715 | * - Cannot be used on unsaved objects. |
||
1716 | * |
||
1717 | * @param string $remoteClass |
||
1718 | * @param string $remoteRelation |
||
1719 | * @return DataList|DataObject The component, either as a list or single object |
||
1720 | * @throws BadMethodCallException |
||
1721 | * @throws InvalidArgumentException |
||
1722 | */ |
||
1723 | public function inferReciprocalComponent($remoteClass, $remoteRelation) { |
||
1820 | |||
1821 | /** |
||
1822 | * Tries to find the database key on another object that is used to store a |
||
1823 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
1824 | * |
||
1825 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
1826 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
1827 | * |
||
1828 | * @param string $component Name of the relation on the current object pointing to the |
||
1829 | * remote object. |
||
1830 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
1831 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
1832 | * @return string |
||
1833 | * @throws Exception |
||
1834 | */ |
||
1835 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
1903 | |||
1904 | /** |
||
1905 | * Returns a many-to-many component, as a ManyManyList. |
||
1906 | * @param string $componentName Name of the many-many component |
||
1907 | * @return ManyManyList The set of components |
||
1908 | */ |
||
1909 | public function getManyManyComponents($componentName) { |
||
1945 | |||
1946 | /** |
||
1947 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
1948 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
1949 | * |
||
1950 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
1951 | * their classes. |
||
1952 | */ |
||
1953 | public function hasOne() { |
||
1956 | |||
1957 | /** |
||
1958 | * Return data for a specific has_one component. |
||
1959 | * @param string $component |
||
1960 | * @return string|null |
||
1961 | */ |
||
1962 | public function hasOneComponent($component) { |
||
1972 | |||
1973 | /** |
||
1974 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
1975 | * their class name will be returned. |
||
1976 | * |
||
1977 | * @param string $component - Name of component |
||
1978 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1979 | * the field data stripped off. It defaults to TRUE. |
||
1980 | * @return string|array |
||
1981 | */ |
||
1982 | View Code Duplication | public function belongsTo($component = null, $classOnly = true) { |
|
1999 | |||
2000 | /** |
||
2001 | * Return data for a specific belongs_to component. |
||
2002 | * @param string $component |
||
2003 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
2004 | * the field data stripped off. It defaults to TRUE. |
||
2005 | * @return string|null |
||
2006 | */ |
||
2007 | View Code Duplication | public function belongsToComponent($component, $classOnly = true) { |
|
2018 | |||
2019 | /** |
||
2020 | * Return all of the database fields in this object |
||
2021 | * |
||
2022 | * @param string $fieldName Limit the output to a specific field name |
||
2023 | * @param string $includeTable If returning a single column, prefix the column with the table name |
||
2024 | * in Table.Column(spec) format |
||
2025 | * @return array|string|null The database fields, or if searching a single field, just this one field if found |
||
2026 | * Field will be a string in ClassName(args) format, or Table.ClassName(args) format if $includeTable is true |
||
2027 | */ |
||
2028 | public function db($fieldName = null, $includeTable = false) { |
||
2067 | |||
2068 | /** |
||
2069 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
2070 | * relationships and their classes will be returned. |
||
2071 | * |
||
2072 | * @param string $component Deprecated - Name of component |
||
2073 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
2074 | * the field data stripped off. It defaults to TRUE. |
||
2075 | * @return string|array|false |
||
2076 | */ |
||
2077 | View Code Duplication | public function hasMany($component = null, $classOnly = true) { |
|
2094 | |||
2095 | /** |
||
2096 | * Return data for a specific has_many component. |
||
2097 | * @param string $component |
||
2098 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
2099 | * the field data stripped off. It defaults to TRUE. |
||
2100 | * @return string|null |
||
2101 | */ |
||
2102 | View Code Duplication | public function hasManyComponent($component, $classOnly = true) { |
|
2113 | |||
2114 | /** |
||
2115 | * Return the many-to-many extra fields specification. |
||
2116 | * |
||
2117 | * If you don't specify a component name, it returns all |
||
2118 | * extra fields for all components available. |
||
2119 | * |
||
2120 | * @return array|null |
||
2121 | */ |
||
2122 | public function manyManyExtraFields() { |
||
2125 | |||
2126 | /** |
||
2127 | * Return the many-to-many extra fields specification for a specific component. |
||
2128 | * @param string $component |
||
2129 | * @return array|null |
||
2130 | */ |
||
2131 | public function manyManyExtraFieldsForComponent($component) { |
||
2171 | |||
2172 | /** |
||
2173 | * Return information about a many-to-many component. |
||
2174 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
2175 | * components are returned. |
||
2176 | * |
||
2177 | * @see DataObject::manyManyComponent() |
||
2178 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
2179 | */ |
||
2180 | public function manyMany() { |
||
2186 | |||
2187 | /** |
||
2188 | * Return information about a specific many_many component. Returns a numeric array of: |
||
2189 | * array( |
||
2190 | * <classname>, The class that relation is defined in e.g. "Product" |
||
2191 | * <candidateName>, The target class of the relation e.g. "Category" |
||
2192 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
2193 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
2194 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
2195 | * ) |
||
2196 | * @param string $component The component name |
||
2197 | * @return array|null |
||
2198 | */ |
||
2199 | public function manyManyComponent($component) { |
||
2254 | |||
2255 | /** |
||
2256 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
2257 | * |
||
2258 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
2259 | * |
||
2260 | * @return array or false |
||
2261 | */ |
||
2262 | public function database_extensions($class){ |
||
2270 | |||
2271 | /** |
||
2272 | * Generates a SearchContext to be used for building and processing |
||
2273 | * a generic search form for properties on this object. |
||
2274 | * |
||
2275 | * @return SearchContext |
||
2276 | */ |
||
2277 | public function getDefaultSearchContext() { |
||
2284 | |||
2285 | /** |
||
2286 | * Determine which properties on the DataObject are |
||
2287 | * searchable, and map them to their default {@link FormField} |
||
2288 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
2289 | * |
||
2290 | * Some additional logic is included for switching field labels, based on |
||
2291 | * how generic or specific the field type is. |
||
2292 | * |
||
2293 | * Used by {@link SearchContext}. |
||
2294 | * |
||
2295 | * @param array $_params |
||
2296 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
2297 | * 'restrictFields': Numeric array of a field name whitelist |
||
2298 | * @return FieldList |
||
2299 | */ |
||
2300 | public function scaffoldSearchFields($_params = null) { |
||
2352 | |||
2353 | /** |
||
2354 | * Scaffold a simple edit form for all properties on this dataobject, |
||
2355 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
2356 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
2357 | * |
||
2358 | * @uses FormScaffolder |
||
2359 | * |
||
2360 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
2361 | * @return FieldList |
||
2362 | */ |
||
2363 | public function scaffoldFormFields($_params = null) { |
||
2384 | |||
2385 | /** |
||
2386 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
2387 | * being called on extensions |
||
2388 | * |
||
2389 | * @param callable $callback The callback to execute |
||
2390 | */ |
||
2391 | protected function beforeUpdateCMSFields($callback) { |
||
2394 | |||
2395 | /** |
||
2396 | * Centerpiece of every data administration interface in Silverstripe, |
||
2397 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
2398 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
2399 | * generate this set. To customize, overload this method in a subclass |
||
2400 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
2401 | * |
||
2402 | * <code> |
||
2403 | * class MyCustomClass extends DataObject { |
||
2404 | * static $db = array('CustomProperty'=>'Boolean'); |
||
2405 | * |
||
2406 | * function getCMSFields() { |
||
2407 | * $fields = parent::getCMSFields(); |
||
2408 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
2409 | * return $fields; |
||
2410 | * } |
||
2411 | * } |
||
2412 | * </code> |
||
2413 | * |
||
2414 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
2415 | * |
||
2416 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
2417 | */ |
||
2418 | public function getCMSFields() { |
||
2430 | |||
2431 | /** |
||
2432 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
2433 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
2434 | * |
||
2435 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
2436 | */ |
||
2437 | public function getCMSActions() { |
||
2442 | |||
2443 | |||
2444 | /** |
||
2445 | * Used for simple frontend forms without relation editing |
||
2446 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
2447 | * by default. To customize, either overload this method in your |
||
2448 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
2449 | * |
||
2450 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
2451 | * |
||
2452 | * @param array $params See {@link scaffoldFormFields()} |
||
2453 | * @return FieldList Always returns a simple field collection without TabSet. |
||
2454 | */ |
||
2455 | public function getFrontEndFields($params = null) { |
||
2461 | |||
2462 | /** |
||
2463 | * Gets the value of a field. |
||
2464 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
2465 | * |
||
2466 | * @param string $field The name of the field |
||
2467 | * |
||
2468 | * @return mixed The field value |
||
2469 | */ |
||
2470 | public function getField($field) { |
||
2489 | |||
2490 | /** |
||
2491 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
2492 | * |
||
2493 | * @param string $tableClass Base table to load the values from. Others are joined as required. |
||
2494 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
2495 | */ |
||
2496 | protected function loadLazyFields($tableClass = null) { |
||
2570 | |||
2571 | /** |
||
2572 | * Return the fields that have changed. |
||
2573 | * |
||
2574 | * The change level affects what the functions defines as "changed": |
||
2575 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
2576 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
2577 | * for example a change from 0 to null would not be included. |
||
2578 | * |
||
2579 | * Example return: |
||
2580 | * <code> |
||
2581 | * array( |
||
2582 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
2583 | * ) |
||
2584 | * </code> |
||
2585 | * |
||
2586 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
2587 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
2588 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
2589 | * @return array |
||
2590 | */ |
||
2591 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
2632 | |||
2633 | /** |
||
2634 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
2635 | * since loading them from the database. |
||
2636 | * |
||
2637 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
2638 | * @param int $changeLevel See {@link getChangedFields()} |
||
2639 | * @return boolean |
||
2640 | */ |
||
2641 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
2651 | |||
2652 | /** |
||
2653 | * Set the value of the field |
||
2654 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
2655 | * |
||
2656 | * @param string $fieldName Name of the field |
||
2657 | * @param mixed $val New field value |
||
2658 | * @return DataObject $this |
||
2659 | */ |
||
2660 | public function setField($fieldName, $val) { |
||
2710 | |||
2711 | /** |
||
2712 | * Set the value of the field, using a casting object. |
||
2713 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
2714 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
2715 | * can be saved into the Image table. |
||
2716 | * |
||
2717 | * @param string $fieldName Name of the field |
||
2718 | * @param mixed $value New field value |
||
2719 | * @return $this |
||
2720 | */ |
||
2721 | public function setCastedField($fieldName, $value) { |
||
2734 | |||
2735 | public function castingHelper($field) { |
||
2742 | |||
2743 | /** |
||
2744 | * Returns true if the given field exists in a database column on any of |
||
2745 | * the objects tables and optionally look up a dynamic getter with |
||
2746 | * get<fieldName>(). |
||
2747 | * |
||
2748 | * @param string $field Name of the field |
||
2749 | * @return boolean True if the given field exists |
||
2750 | */ |
||
2751 | public function hasField($field) { |
||
2759 | |||
2760 | /** |
||
2761 | * Returns true if the given field exists as a database column |
||
2762 | * |
||
2763 | * @param string $field Name of the field |
||
2764 | * |
||
2765 | * @return boolean |
||
2766 | */ |
||
2767 | public function hasDatabaseField($field) { |
||
2771 | |||
2772 | /** |
||
2773 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2774 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2775 | * |
||
2776 | * @param string $field Name of the field |
||
2777 | * @return string The field type of the given field |
||
2778 | */ |
||
2779 | public function hasOwnTableDatabaseField($field) { |
||
2782 | |||
2783 | /** |
||
2784 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2785 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2786 | * |
||
2787 | * @param string $class Class name to check |
||
2788 | * @param string $field Name of the field |
||
2789 | * @return string The field type of the given field |
||
2790 | */ |
||
2791 | public static function has_own_table_database_field($class, $field) { |
||
2801 | |||
2802 | /** |
||
2803 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
2804 | * actually looking in the database. |
||
2805 | * |
||
2806 | * @param string $dataClass |
||
2807 | * @return bool |
||
2808 | */ |
||
2809 | public static function has_own_table($dataClass) { |
||
2824 | |||
2825 | /** |
||
2826 | * Returns true if the member is allowed to do the given action. |
||
2827 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
2828 | * |
||
2829 | * @param string $perm The permission to be checked, such as 'View'. |
||
2830 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
2831 | * in user. |
||
2832 | * @param array $context Additional $context to pass to extendedCan() |
||
2833 | * |
||
2834 | * @return boolean True if the the member is allowed to do the given action |
||
2835 | */ |
||
2836 | public function can($perm, $member = null, $context = array()) { |
||
2895 | |||
2896 | /** |
||
2897 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
2898 | * expected to return one of three values: |
||
2899 | * |
||
2900 | * - false: Disallow this permission, regardless of what other extensions say |
||
2901 | * - true: Allow this permission, as long as no other extensions return false |
||
2902 | * - NULL: Don't affect the outcome |
||
2903 | * |
||
2904 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
2905 | * |
||
2906 | * <code> |
||
2907 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
2908 | * if($extended !== null) return $extended; |
||
2909 | * else return $normalValue; |
||
2910 | * </code> |
||
2911 | * |
||
2912 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
2913 | * @param Member|int $member |
||
2914 | * @param array $context Optional context |
||
2915 | * @return boolean|null |
||
2916 | */ |
||
2917 | public function extendedCan($methodName, $member, $context = array()) { |
||
2928 | |||
2929 | /** |
||
2930 | * @param Member $member |
||
2931 | * @return boolean |
||
2932 | */ |
||
2933 | View Code Duplication | public function canView($member = null) { |
|
2940 | |||
2941 | /** |
||
2942 | * @param Member $member |
||
2943 | * @return boolean |
||
2944 | */ |
||
2945 | View Code Duplication | public function canEdit($member = null) { |
|
2952 | |||
2953 | /** |
||
2954 | * @param Member $member |
||
2955 | * @return boolean |
||
2956 | */ |
||
2957 | View Code Duplication | public function canDelete($member = null) { |
|
2964 | |||
2965 | /** |
||
2966 | * @param Member $member |
||
2967 | * @param array $context Additional context-specific data which might |
||
2968 | * affect whether (or where) this object could be created. |
||
2969 | * @return boolean |
||
2970 | */ |
||
2971 | View Code Duplication | public function canCreate($member = null, $context = array()) { |
|
2978 | |||
2979 | /** |
||
2980 | * Debugging used by Debug::show() |
||
2981 | * |
||
2982 | * @return string HTML data representing this object |
||
2983 | */ |
||
2984 | public function debug() { |
||
2992 | |||
2993 | /** |
||
2994 | * Return the DBField object that represents the given field. |
||
2995 | * This works similarly to obj() with 2 key differences: |
||
2996 | * - it still returns an object even when the field has no value. |
||
2997 | * - it only matches fields and not methods |
||
2998 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
2999 | * |
||
3000 | * @param string $fieldName Name of the field |
||
3001 | * @return DBField The field as a DBField object |
||
3002 | */ |
||
3003 | public function dbObject($fieldName) { |
||
3023 | |||
3024 | /** |
||
3025 | * Traverses to a DBField referenced by relationships between data objects. |
||
3026 | * |
||
3027 | * The path to the related field is specified with dot separated syntax |
||
3028 | * (eg: Parent.Child.Child.FieldName). |
||
3029 | * |
||
3030 | * @param string $fieldPath |
||
3031 | * |
||
3032 | * @return mixed DBField of the field on the object or a DataList instance. |
||
3033 | */ |
||
3034 | public function relObject($fieldPath) { |
||
3064 | |||
3065 | /** |
||
3066 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
3067 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
3068 | * |
||
3069 | * @param $fieldName string |
||
3070 | * @return string | null - will return null on a missing value |
||
3071 | */ |
||
3072 | View Code Duplication | public function relField($fieldName) { |
|
3107 | |||
3108 | /** |
||
3109 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
3110 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
3111 | * |
||
3112 | * @return String |
||
3113 | */ |
||
3114 | public function getReverseAssociation($className) { |
||
3130 | |||
3131 | /** |
||
3132 | * Return all objects matching the filter |
||
3133 | * sub-classes are automatically selected and included |
||
3134 | * |
||
3135 | * @param string $callerClass The class of objects to be returned |
||
3136 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3137 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
3138 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
3139 | * BY clause. If omitted, self::$default_sort will be used. |
||
3140 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
3141 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
3142 | * @param string $containerClass The container class to return the results in. |
||
3143 | * |
||
3144 | * @todo $containerClass is Ignored, why? |
||
3145 | * |
||
3146 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
3147 | */ |
||
3148 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
3185 | |||
3186 | |||
3187 | /** |
||
3188 | * Return the first item matching the given query. |
||
3189 | * All calls to get_one() are cached. |
||
3190 | * |
||
3191 | * @param string $callerClass The class of objects to be returned |
||
3192 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3193 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
3194 | * @param boolean $cache Use caching |
||
3195 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
3196 | * |
||
3197 | * @return DataObject The first item matching the query |
||
3198 | */ |
||
3199 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
3225 | |||
3226 | /** |
||
3227 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
3228 | * Also clears any cached aggregate data. |
||
3229 | * |
||
3230 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
3231 | * When false will just clear session-local cached data |
||
3232 | * @return DataObject $this |
||
3233 | */ |
||
3234 | public function flushCache($persistent = true) { |
||
3250 | |||
3251 | /** |
||
3252 | * Flush the get_one global cache and destroy associated objects. |
||
3253 | */ |
||
3254 | public static function flush_and_destroy_cache() { |
||
3262 | |||
3263 | /** |
||
3264 | * Reset all global caches associated with DataObject. |
||
3265 | */ |
||
3266 | public static function reset() { |
||
3275 | |||
3276 | /** |
||
3277 | * Return the given element, searching by ID |
||
3278 | * |
||
3279 | * @param string $callerClass The class of the object to be returned |
||
3280 | * @param int $id The id of the element |
||
3281 | * @param boolean $cache See {@link get_one()} |
||
3282 | * |
||
3283 | * @return DataObject The element |
||
3284 | */ |
||
3285 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
3302 | |||
3303 | /** |
||
3304 | * Get the name of the base table for this object |
||
3305 | */ |
||
3306 | public function baseTable() { |
||
3310 | |||
3311 | /** |
||
3312 | * @var Array Parameters used in the query that built this object. |
||
3313 | * This can be used by decorators (e.g. lazy loading) to |
||
3314 | * run additional queries using the same context. |
||
3315 | */ |
||
3316 | protected $sourceQueryParams; |
||
3317 | |||
3318 | /** |
||
3319 | * @see $sourceQueryParams |
||
3320 | * @return array |
||
3321 | */ |
||
3322 | public function getSourceQueryParams() { |
||
3325 | |||
3326 | /** |
||
3327 | * Get list of parameters that should be inherited to relations on this object |
||
3328 | * |
||
3329 | * @return array |
||
3330 | */ |
||
3331 | public function getInheritableQueryParams() { |
||
3336 | |||
3337 | /** |
||
3338 | * @see $sourceQueryParams |
||
3339 | * @param array |
||
3340 | */ |
||
3341 | public function setSourceQueryParams($array) { |
||
3344 | |||
3345 | /** |
||
3346 | * @see $sourceQueryParams |
||
3347 | * @param array |
||
3348 | */ |
||
3349 | public function setSourceQueryParam($key, $value) { |
||
3352 | |||
3353 | /** |
||
3354 | * @see $sourceQueryParams |
||
3355 | * @return Mixed |
||
3356 | */ |
||
3357 | public function getSourceQueryParam($key) { |
||
3361 | |||
3362 | //-------------------------------------------------------------------------------------------// |
||
3363 | |||
3364 | /** |
||
3365 | * Return the database indexes on this table. |
||
3366 | * This array is indexed by the name of the field with the index, and |
||
3367 | * the value is the type of index. |
||
3368 | */ |
||
3369 | public function databaseIndexes() { |
||
3394 | |||
3395 | /** |
||
3396 | * Check the database schema and update it as necessary. |
||
3397 | * |
||
3398 | * @uses DataExtension->augmentDatabase() |
||
3399 | */ |
||
3400 | public function requireTable() { |
||
3445 | |||
3446 | /** |
||
3447 | * Validate that the configured relations for this class use the correct syntaxes |
||
3448 | * @throws LogicException |
||
3449 | */ |
||
3450 | protected function validateModelDefinitions() { |
||
3481 | |||
3482 | /** |
||
3483 | * Add default records to database. This function is called whenever the |
||
3484 | * database is built, after the database tables have all been created. Overload |
||
3485 | * this to add default records when the database is built, but make sure you |
||
3486 | * call parent::requireDefaultRecords(). |
||
3487 | * |
||
3488 | * @uses DataExtension->requireDefaultRecords() |
||
3489 | */ |
||
3490 | public function requireDefaultRecords() { |
||
3508 | |||
3509 | /** |
||
3510 | * Get the default searchable fields for this object, as defined in the |
||
3511 | * $searchable_fields list. If searchable fields are not defined on the |
||
3512 | * data object, uses a default selection of summary fields. |
||
3513 | * |
||
3514 | * @return array |
||
3515 | */ |
||
3516 | public function searchableFields() { |
||
3590 | |||
3591 | /** |
||
3592 | * Get any user defined searchable fields labels that |
||
3593 | * exist. Allows overriding of default field names in the form |
||
3594 | * interface actually presented to the user. |
||
3595 | * |
||
3596 | * The reason for keeping this separate from searchable_fields, |
||
3597 | * which would be a logical place for this functionality, is to |
||
3598 | * avoid bloating and complicating the configuration array. Currently |
||
3599 | * much of this system is based on sensible defaults, and this property |
||
3600 | * would generally only be set in the case of more complex relationships |
||
3601 | * between data object being required in the search interface. |
||
3602 | * |
||
3603 | * Generates labels based on name of the field itself, if no static property |
||
3604 | * {@link self::field_labels} exists. |
||
3605 | * |
||
3606 | * @uses $field_labels |
||
3607 | * @uses FormField::name_to_label() |
||
3608 | * |
||
3609 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
3610 | * |
||
3611 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
3612 | */ |
||
3613 | public function fieldLabels($includerelations = true) { |
||
3648 | |||
3649 | /** |
||
3650 | * Get a human-readable label for a single field, |
||
3651 | * see {@link fieldLabels()} for more details. |
||
3652 | * |
||
3653 | * @uses fieldLabels() |
||
3654 | * @uses FormField::name_to_label() |
||
3655 | * |
||
3656 | * @param string $name Name of the field |
||
3657 | * @return string Label of the field |
||
3658 | */ |
||
3659 | public function fieldLabel($name) { |
||
3663 | |||
3664 | /** |
||
3665 | * Get the default summary fields for this object. |
||
3666 | * |
||
3667 | * @todo use the translation apparatus to return a default field selection for the language |
||
3668 | * |
||
3669 | * @return array |
||
3670 | */ |
||
3671 | public function summaryFields() { |
||
3704 | |||
3705 | /** |
||
3706 | * Defines a default list of filters for the search context. |
||
3707 | * |
||
3708 | * If a filter class mapping is defined on the data object, |
||
3709 | * it is constructed here. Otherwise, the default filter specified in |
||
3710 | * {@link DBField} is used. |
||
3711 | * |
||
3712 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
3713 | * |
||
3714 | * @return array |
||
3715 | */ |
||
3716 | public function defaultSearchFilters() { |
||
3737 | |||
3738 | /** |
||
3739 | * @return boolean True if the object is in the database |
||
3740 | */ |
||
3741 | public function isInDB() { |
||
3744 | |||
3745 | /* |
||
3746 | * @ignore |
||
3747 | */ |
||
3748 | private static $subclass_access = true; |
||
3749 | |||
3750 | /** |
||
3751 | * Temporarily disable subclass access in data object qeur |
||
3752 | */ |
||
3753 | public static function disable_subclass_access() { |
||
3759 | |||
3760 | //-------------------------------------------------------------------------------------------// |
||
3761 | |||
3762 | /** |
||
3763 | * Database field definitions. |
||
3764 | * This is a map from field names to field type. The field |
||
3765 | * type should be a class that extends . |
||
3766 | * @var array |
||
3767 | * @config |
||
3768 | */ |
||
3769 | private static $db = null; |
||
3770 | |||
3771 | /** |
||
3772 | * Use a casting object for a field. This is a map from |
||
3773 | * field name to class name of the casting object. |
||
3774 | * |
||
3775 | * @var array |
||
3776 | */ |
||
3777 | private static $casting = array( |
||
3778 | "Title" => 'Text', |
||
3779 | ); |
||
3780 | |||
3781 | /** |
||
3782 | * Specify custom options for a CREATE TABLE call. |
||
3783 | * Can be used to specify a custom storage engine for specific database table. |
||
3784 | * All options have to be keyed for a specific database implementation, |
||
3785 | * identified by their class name (extending from {@link SS_Database}). |
||
3786 | * |
||
3787 | * <code> |
||
3788 | * array( |
||
3789 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
3790 | * ) |
||
3791 | * </code> |
||
3792 | * |
||
3793 | * Caution: This API is experimental, and might not be |
||
3794 | * included in the next major release. Please use with care. |
||
3795 | * |
||
3796 | * @var array |
||
3797 | * @config |
||
3798 | */ |
||
3799 | private static $create_table_options = array( |
||
3800 | 'SilverStripe\Model\Connect\MySQLDatabase' => 'ENGINE=InnoDB' |
||
3801 | ); |
||
3802 | |||
3803 | /** |
||
3804 | * If a field is in this array, then create a database index |
||
3805 | * on that field. This is a map from fieldname to index type. |
||
3806 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
3807 | * |
||
3808 | * @var array |
||
3809 | * @config |
||
3810 | */ |
||
3811 | private static $indexes = null; |
||
3812 | |||
3813 | /** |
||
3814 | * Inserts standard column-values when a DataObject |
||
3815 | * is instanciated. Does not insert default records {@see $default_records}. |
||
3816 | * This is a map from fieldname to default value. |
||
3817 | * |
||
3818 | * - If you would like to change a default value in a sub-class, just specify it. |
||
3819 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
3820 | * or false in your subclass. Setting it to null won't work. |
||
3821 | * |
||
3822 | * @var array |
||
3823 | * @config |
||
3824 | */ |
||
3825 | private static $defaults = null; |
||
3826 | |||
3827 | /** |
||
3828 | * Multidimensional array which inserts default data into the database |
||
3829 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
3830 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
3831 | * behaviour such as publishing and ParentNodes. |
||
3832 | * |
||
3833 | * Example: |
||
3834 | * array( |
||
3835 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
3836 | * array('Title' => "DefaultPage2") |
||
3837 | * ). |
||
3838 | * |
||
3839 | * @var array |
||
3840 | * @config |
||
3841 | */ |
||
3842 | private static $default_records = null; |
||
3843 | |||
3844 | /** |
||
3845 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
3846 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
3847 | * |
||
3848 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3849 | * |
||
3850 | * @var array |
||
3851 | * @config |
||
3852 | */ |
||
3853 | private static $has_one = null; |
||
3854 | |||
3855 | /** |
||
3856 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
3857 | * |
||
3858 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
3859 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
3860 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
3861 | * |
||
3862 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3863 | * |
||
3864 | * @var array |
||
3865 | * @config |
||
3866 | */ |
||
3867 | private static $belongs_to; |
||
3868 | |||
3869 | /** |
||
3870 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
3871 | * |
||
3872 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
3873 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
3874 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
3875 | * which foreign key to use. |
||
3876 | * |
||
3877 | * @var array |
||
3878 | * @config |
||
3879 | */ |
||
3880 | private static $has_many = null; |
||
3881 | |||
3882 | /** |
||
3883 | * many-many relationship definitions. |
||
3884 | * This is a map from component name to data type. |
||
3885 | * @var array |
||
3886 | * @config |
||
3887 | */ |
||
3888 | private static $many_many = null; |
||
3889 | |||
3890 | /** |
||
3891 | * Extra fields to include on the connecting many-many table. |
||
3892 | * This is a map from field name to field type. |
||
3893 | * |
||
3894 | * Example code: |
||
3895 | * <code> |
||
3896 | * public static $many_many_extraFields = array( |
||
3897 | * 'Members' => array( |
||
3898 | * 'Role' => 'Varchar(100)' |
||
3899 | * ) |
||
3900 | * ); |
||
3901 | * </code> |
||
3902 | * |
||
3903 | * @var array |
||
3904 | * @config |
||
3905 | */ |
||
3906 | private static $many_many_extraFields = null; |
||
3907 | |||
3908 | /** |
||
3909 | * The inverse side of a many-many relationship. |
||
3910 | * This is a map from component name to data type. |
||
3911 | * @var array |
||
3912 | * @config |
||
3913 | */ |
||
3914 | private static $belongs_many_many = null; |
||
3915 | |||
3916 | /** |
||
3917 | * The default sort expression. This will be inserted in the ORDER BY |
||
3918 | * clause of a SQL query if no other sort expression is provided. |
||
3919 | * @var string |
||
3920 | * @config |
||
3921 | */ |
||
3922 | private static $default_sort = null; |
||
3923 | |||
3924 | /** |
||
3925 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
3926 | * search interface. |
||
3927 | * |
||
3928 | * Overriding the default filter, with a custom defined filter: |
||
3929 | * <code> |
||
3930 | * static $searchable_fields = array( |
||
3931 | * "Name" => "PartialMatchFilter" |
||
3932 | * ); |
||
3933 | * </code> |
||
3934 | * |
||
3935 | * Overriding the default form fields, with a custom defined field. |
||
3936 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
3937 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
3938 | * <code> |
||
3939 | * static $searchable_fields = array( |
||
3940 | * "Name" => array( |
||
3941 | * "field" => "TextField" |
||
3942 | * ) |
||
3943 | * ); |
||
3944 | * </code> |
||
3945 | * |
||
3946 | * Overriding the default form field, filter and title: |
||
3947 | * <code> |
||
3948 | * static $searchable_fields = array( |
||
3949 | * "Organisation.ZipCode" => array( |
||
3950 | * "field" => "TextField", |
||
3951 | * "filter" => "PartialMatchFilter", |
||
3952 | * "title" => 'Organisation ZIP' |
||
3953 | * ) |
||
3954 | * ); |
||
3955 | * </code> |
||
3956 | * @config |
||
3957 | */ |
||
3958 | private static $searchable_fields = null; |
||
3959 | |||
3960 | /** |
||
3961 | * User defined labels for searchable_fields, used to override |
||
3962 | * default display in the search form. |
||
3963 | * @config |
||
3964 | */ |
||
3965 | private static $field_labels = null; |
||
3966 | |||
3967 | /** |
||
3968 | * Provides a default list of fields to be used by a 'summary' |
||
3969 | * view of this object. |
||
3970 | * @config |
||
3971 | */ |
||
3972 | private static $summary_fields = null; |
||
3973 | |||
3974 | /** |
||
3975 | * Collect all static properties on the object |
||
3976 | * which contain natural language, and need to be translated. |
||
3977 | * The full entity name is composed from the class name and a custom identifier. |
||
3978 | * |
||
3979 | * @return array A numerical array which contains one or more entities in array-form. |
||
3980 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
3981 | * $entity, $string, $priority, $context. |
||
3982 | */ |
||
3983 | public function provideI18nEntities() { |
||
4001 | |||
4002 | /** |
||
4003 | * Returns true if the given method/parameter has a value |
||
4004 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
4005 | * |
||
4006 | * @param string $field The field name |
||
4007 | * @param array $arguments |
||
4008 | * @param bool $cache |
||
4009 | * @return boolean |
||
4010 | */ |
||
4011 | public function hasValue($field, $arguments = null, $cache = true) { |
||
4019 | |||
4020 | } |
||
4021 |