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 |
||
104 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider, Resettable |
||
105 | { |
||
106 | |||
107 | /** |
||
108 | * Human-readable singular name. |
||
109 | * @var string |
||
110 | * @config |
||
111 | */ |
||
112 | private static $singular_name = null; |
||
113 | |||
114 | /** |
||
115 | * Human-readable plural name |
||
116 | * @var string |
||
117 | * @config |
||
118 | */ |
||
119 | private static $plural_name = null; |
||
120 | |||
121 | /** |
||
122 | * Allow API access to this object? |
||
123 | * @todo Define the options that can be set here |
||
124 | * @config |
||
125 | */ |
||
126 | private static $api_access = false; |
||
127 | |||
128 | /** |
||
129 | * Allows specification of a default value for the ClassName field. |
||
130 | * Configure this value only in subclasses of DataObject. |
||
131 | * |
||
132 | * @config |
||
133 | * @var string |
||
134 | */ |
||
135 | private static $default_classname = null; |
||
136 | |||
137 | /** |
||
138 | * True if this DataObject has been destroyed. |
||
139 | * @var boolean |
||
140 | */ |
||
141 | public $destroyed = false; |
||
142 | |||
143 | /** |
||
144 | * The DataModel from this this object comes |
||
145 | */ |
||
146 | protected $model; |
||
147 | |||
148 | /** |
||
149 | * Data stored in this objects database record. An array indexed by fieldname. |
||
150 | * |
||
151 | * Use {@link toMap()} if you want an array representation |
||
152 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
153 | * |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $record; |
||
157 | |||
158 | /** |
||
159 | * If selected through a many_many through relation, this is the instance of the through record |
||
160 | * |
||
161 | * @var DataObject |
||
162 | */ |
||
163 | protected $joinRecord; |
||
164 | |||
165 | /** |
||
166 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
167 | */ |
||
168 | const CHANGE_NONE = 0; |
||
169 | |||
170 | /** |
||
171 | * Represents a field that has changed type, although not the loosely defined value. |
||
172 | * (before !== after && before == after) |
||
173 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
174 | * Value changes are by nature also considered strict changes. |
||
175 | */ |
||
176 | const CHANGE_STRICT = 1; |
||
177 | |||
178 | /** |
||
179 | * Represents a field that has changed the loosely defined value |
||
180 | * (before != after, thus, before !== after)) |
||
181 | * E.g. change false to true, but not false to 0 |
||
182 | */ |
||
183 | const CHANGE_VALUE = 2; |
||
184 | |||
185 | /** |
||
186 | * An array indexed by fieldname, true if the field has been changed. |
||
187 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
188 | * the changed state. |
||
189 | * |
||
190 | * @var array |
||
191 | */ |
||
192 | private $changed; |
||
193 | |||
194 | /** |
||
195 | * The database record (in the same format as $record), before |
||
196 | * any changes. |
||
197 | * @var array |
||
198 | */ |
||
199 | protected $original; |
||
200 | |||
201 | /** |
||
202 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
203 | * @var boolean |
||
204 | */ |
||
205 | protected $brokenOnDelete = false; |
||
206 | |||
207 | /** |
||
208 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
209 | * @var boolean |
||
210 | */ |
||
211 | protected $brokenOnWrite = false; |
||
212 | |||
213 | /** |
||
214 | * @config |
||
215 | * @var boolean Should dataobjects be validated before they are written? |
||
216 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
217 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
218 | * to only disable validation for very specific use cases. |
||
219 | */ |
||
220 | private static $validation_enabled = true; |
||
221 | |||
222 | /** |
||
223 | * Static caches used by relevant functions. |
||
224 | * |
||
225 | * @var array |
||
226 | */ |
||
227 | protected static $_cache_get_one; |
||
228 | |||
229 | /** |
||
230 | * Cache of field labels |
||
231 | * |
||
232 | * @var array |
||
233 | */ |
||
234 | protected static $_cache_field_labels = array(); |
||
235 | |||
236 | /** |
||
237 | * Base fields which are not defined in static $db |
||
238 | * |
||
239 | * @config |
||
240 | * @var array |
||
241 | */ |
||
242 | private static $fixed_fields = array( |
||
243 | 'ID' => 'PrimaryKey', |
||
244 | 'ClassName' => 'DBClassName', |
||
245 | 'LastEdited' => 'DBDatetime', |
||
246 | 'Created' => 'DBDatetime', |
||
247 | ); |
||
248 | |||
249 | /** |
||
250 | * Override table name for this class. If ignored will default to FQN of class. |
||
251 | * This option is not inheritable, and must be set on each class. |
||
252 | * If left blank naming will default to the legacy (3.x) behaviour. |
||
253 | * |
||
254 | * @var string |
||
255 | */ |
||
256 | private static $table_name = null; |
||
257 | |||
258 | /** |
||
259 | * Non-static relationship cache, indexed by component name. |
||
260 | * |
||
261 | * @var DataObject[] |
||
262 | */ |
||
263 | protected $components; |
||
264 | |||
265 | /** |
||
266 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
267 | * |
||
268 | * @var UnsavedRelationList[] |
||
269 | */ |
||
270 | protected $unsavedRelations; |
||
271 | |||
272 | /** |
||
273 | * Get schema object |
||
274 | * |
||
275 | * @return DataObjectSchema |
||
276 | */ |
||
277 | public static function getSchema() |
||
278 | { |
||
279 | return Injector::inst()->get(DataObjectSchema::class); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Construct a new DataObject. |
||
284 | * |
||
285 | |||
286 | * @param array|null $record Used internally for rehydrating an object from database content. |
||
287 | * Bypasses setters on this class, and hence should not be used |
||
288 | * for populating data on new records. |
||
289 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
290 | * Singletons don't have their defaults set. |
||
291 | * @param DataModel $model |
||
292 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
293 | */ |
||
294 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) |
||
295 | { |
||
296 | parent::__construct(); |
||
297 | |||
298 | // Set query params on the DataObject to tell the lazy loading mechanism the context the object creation context |
||
299 | $this->setSourceQueryParams($queryParams); |
||
300 | |||
301 | // Set the fields data. |
||
302 | if (!$record) { |
||
303 | $record = array( |
||
304 | 'ID' => 0, |
||
305 | 'ClassName' => static::class, |
||
306 | 'RecordClassName' => static::class |
||
307 | ); |
||
308 | } |
||
309 | |||
310 | if ($record instanceof stdClass) { |
||
311 | $record = (array)$record; |
||
312 | } |
||
313 | |||
314 | if (!is_array($record)) { |
||
315 | if (is_object($record)) { |
||
316 | $passed = "an object of type '".get_class($record)."'"; |
||
317 | } else { |
||
318 | $passed = "The value '$record'"; |
||
319 | } |
||
320 | |||
321 | user_error( |
||
322 | "DataObject::__construct passed $passed. It's supposed to be passed an array," |
||
323 | . " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?", |
||
324 | E_USER_WARNING |
||
325 | ); |
||
326 | $record = null; |
||
327 | } |
||
328 | |||
329 | // Set $this->record to $record, but ignore NULLs |
||
330 | $this->record = array(); |
||
331 | foreach ($record as $k => $v) { |
||
|
|||
332 | // Ensure that ID is stored as a number and not a string |
||
333 | // To do: this kind of clean-up should be done on all numeric fields, in some relatively |
||
334 | // performant manner |
||
335 | if ($v !== null) { |
||
336 | if ($k == 'ID' && is_numeric($v)) { |
||
337 | $this->record[$k] = (int)$v; |
||
338 | } else { |
||
339 | $this->record[$k] = $v; |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | |||
344 | // Identify fields that should be lazy loaded, but only on existing records |
||
345 | if (!empty($record['ID'])) { |
||
346 | // Get all field specs scoped to class for later lazy loading |
||
347 | $fields = static::getSchema()->fieldSpecs( |
||
348 | static::class, |
||
349 | DataObjectSchema::INCLUDE_CLASS | DataObjectSchema::DB_ONLY |
||
350 | ); |
||
351 | foreach ($fields as $field => $fieldSpec) { |
||
352 | $fieldClass = strtok($fieldSpec, "."); |
||
353 | if (!array_key_exists($field, $record)) { |
||
354 | $this->record[$field.'_Lazy'] = $fieldClass; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | $this->original = $this->record; |
||
360 | |||
361 | // Keep track of the modification date of all the data sourced to make this page |
||
362 | // From this we create a Last-Modified HTTP header |
||
363 | if (isset($record['LastEdited'])) { |
||
364 | HTTP::register_modification_date($record['LastEdited']); |
||
365 | } |
||
366 | |||
367 | // this must be called before populateDefaults(), as field getters on a DataObject |
||
368 | // may call getComponent() and others, which rely on $this->model being set. |
||
369 | $this->model = $model ? $model : DataModel::inst(); |
||
370 | |||
371 | // Must be called after parent constructor |
||
372 | if (!$isSingleton && (!isset($this->record['ID']) || !$this->record['ID'])) { |
||
373 | $this->populateDefaults(); |
||
374 | } |
||
375 | |||
376 | // prevent populateDefaults() and setField() from marking overwritten defaults as changed |
||
377 | $this->changed = array(); |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * Set the DataModel |
||
382 | * @param DataModel $model |
||
383 | * @return DataObject $this |
||
384 | */ |
||
385 | public function setDataModel(DataModel $model) |
||
386 | { |
||
387 | $this->model = $model; |
||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Destroy all of this objects dependant objects and local caches. |
||
393 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
394 | */ |
||
395 | public function destroy() |
||
396 | { |
||
397 | //$this->destroyed = true; |
||
398 | gc_collect_cycles(); |
||
399 | $this->flushCache(false); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Create a duplicate of this node. Can duplicate many_many relations |
||
404 | * |
||
405 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
406 | * If this is true, it will create the duplicate in the database. |
||
407 | * @param bool|string $manyMany Which many-many to duplicate. Set to true to duplicate all, false to duplicate none. |
||
408 | * Alternatively set to the string of the relation config to duplicate |
||
409 | * (supports 'many_many', or 'belongs_many_many') |
||
410 | * @return static A duplicate of this node. The exact type will be the type of this node. |
||
411 | */ |
||
412 | public function duplicate($doWrite = true, $manyMany = 'many_many') |
||
413 | { |
||
414 | $map = $this->toMap(); |
||
415 | unset($map['Created']); |
||
416 | /** @var static $clone */ |
||
417 | $clone = Injector::inst()->create(static::class, $map, false, $this->model); |
||
418 | $clone->ID = 0; |
||
419 | |||
420 | $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite, $manyMany); |
||
421 | if ($manyMany) { |
||
422 | $this->duplicateManyManyRelations($this, $clone, $manyMany); |
||
423 | } |
||
424 | if ($doWrite) { |
||
425 | $clone->write(); |
||
426 | } |
||
427 | $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite, $manyMany); |
||
428 | |||
429 | return $clone; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object. |
||
434 | * |
||
435 | * @param DataObject $sourceObject the source object to duplicate from |
||
436 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
437 | * @param bool|string $filter |
||
438 | */ |
||
439 | protected function duplicateManyManyRelations($sourceObject, $destinationObject, $filter) |
||
440 | { |
||
441 | // Get list of relations to duplicate |
||
442 | if ($filter === 'many_many' || $filter === 'belongs_many_many') { |
||
443 | $relations = $sourceObject->config()->get($filter); |
||
444 | } elseif ($filter === true) { |
||
445 | $relations = $sourceObject->manyMany(); |
||
446 | } else { |
||
447 | throw new InvalidArgumentException("Invalid many_many duplication filter"); |
||
448 | } |
||
449 | foreach ($relations as $manyManyName => $type) { |
||
450 | $this->duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName); |
||
451 | } |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * Duplicates a single many_many relation from one object to another |
||
456 | * |
||
457 | * @param DataObject $sourceObject |
||
458 | * @param DataObject $destinationObject |
||
459 | * @param string $manyManyName |
||
460 | */ |
||
461 | protected function duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName) |
||
462 | { |
||
463 | // Ensure this component exists on the destination side as well |
||
464 | if (!static::getSchema()->manyManyComponent(get_class($destinationObject), $manyManyName)) { |
||
465 | return; |
||
466 | } |
||
467 | |||
468 | // Copy all components from source to destination |
||
469 | $source = $sourceObject->getManyManyComponents($manyManyName); |
||
470 | $dest = $destinationObject->getManyManyComponents($manyManyName); |
||
471 | foreach ($source as $item) { |
||
472 | $dest->add($item); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Return obsolete class name, if this is no longer a valid class |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | public function getObsoleteClassName() |
||
482 | { |
||
483 | $className = $this->getField("ClassName"); |
||
484 | if (!ClassInfo::exists($className)) { |
||
485 | return $className; |
||
486 | } |
||
487 | return null; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Gets name of this class |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | public function getClassName() |
||
496 | { |
||
497 | $className = $this->getField("ClassName"); |
||
498 | if (!ClassInfo::exists($className)) { |
||
499 | return static::class; |
||
500 | } |
||
501 | return $className; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Set the ClassName attribute. {@link $class} is also updated. |
||
506 | * Warning: This will produce an inconsistent record, as the object |
||
507 | * instance will not automatically switch to the new subclass. |
||
508 | * Please use {@link newClassInstance()} for this purpose, |
||
509 | * or destroy and reinstanciate the record. |
||
510 | * |
||
511 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
512 | * @return $this |
||
513 | */ |
||
514 | public function setClassName($className) |
||
515 | { |
||
516 | $className = trim($className); |
||
517 | if (!$className || !is_subclass_of($className, self::class)) { |
||
518 | return $this; |
||
519 | } |
||
520 | |||
521 | $this->setField("ClassName", $className); |
||
522 | $this->setField('RecordClassName', $className); |
||
523 | return $this; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Create a new instance of a different class from this object's record. |
||
528 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
529 | * it ensures that the instance of the class is a match for the className of the |
||
530 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
531 | * property manually before calling this method, as it will confuse change detection. |
||
532 | * |
||
533 | * If the new class is different to the original class, defaults are populated again |
||
534 | * because this will only occur automatically on instantiation of a DataObject if |
||
535 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
536 | * we still need to repopulate the defaults. |
||
537 | * |
||
538 | * @param string $newClassName The name of the new class |
||
539 | * |
||
540 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
541 | */ |
||
542 | public function newClassInstance($newClassName) |
||
562 | |||
563 | /** |
||
564 | * Adds methods from the extensions. |
||
565 | * Called by Object::__construct() once per class. |
||
566 | */ |
||
567 | public function defineMethods() |
||
568 | { |
||
569 | parent::defineMethods(); |
||
570 | |||
571 | if (static::class === self::class) { |
||
572 | return; |
||
597 | |||
598 | /** |
||
599 | * Returns true if this object "exists", i.e., has a sensible value. |
||
600 | * The default behaviour for a DataObject is to return true if |
||
601 | * the object exists in the database, you can override this in subclasses. |
||
602 | * |
||
603 | * @return boolean true if this object exists |
||
604 | */ |
||
605 | public function exists() |
||
609 | |||
610 | /** |
||
611 | * Returns TRUE if all values (other than "ID") are |
||
612 | * considered empty (by weak boolean comparison). |
||
613 | * |
||
614 | * @return boolean |
||
615 | */ |
||
616 | public function isEmpty() |
||
635 | |||
636 | /** |
||
637 | * Pluralise this item given a specific count. |
||
638 | * |
||
639 | * E.g. "0 Pages", "1 File", "3 Images" |
||
640 | * |
||
641 | * @param string $count |
||
642 | * @return string |
||
643 | */ |
||
644 | public function i18n_pluralise($count) |
||
653 | |||
654 | /** |
||
655 | * Get the user friendly singular name of this DataObject. |
||
656 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
657 | * this returns the class name. |
||
658 | * |
||
659 | * @return string User friendly singular name of this DataObject |
||
660 | */ |
||
661 | public function singular_name() |
||
673 | |||
674 | /** |
||
675 | * Get the translated user friendly singular name of this DataObject |
||
676 | * same as singular_name() but runs it through the translating function |
||
677 | * |
||
678 | * Translating string is in the form: |
||
679 | * $this->class.SINGULARNAME |
||
680 | * Example: |
||
681 | * Page.SINGULARNAME |
||
682 | * |
||
683 | * @return string User friendly translated singular name of this DataObject |
||
684 | */ |
||
685 | public function i18n_singular_name() |
||
689 | |||
690 | /** |
||
691 | * Get the user friendly plural name of this DataObject |
||
692 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
693 | * this returns a pluralised version of the class name. |
||
694 | * |
||
695 | * @return string User friendly plural name of this DataObject |
||
696 | */ |
||
697 | public function plural_name() |
||
709 | |||
710 | /** |
||
711 | * Get the translated user friendly plural name of this DataObject |
||
712 | * Same as plural_name but runs it through the translation function |
||
713 | * Translation string is in the form: |
||
714 | * $this->class.PLURALNAME |
||
715 | * Example: |
||
716 | * Page.PLURALNAME |
||
717 | * |
||
718 | * @return string User friendly translated plural name of this DataObject |
||
719 | */ |
||
720 | public function i18n_plural_name() |
||
724 | |||
725 | /** |
||
726 | * Standard implementation of a title/label for a specific |
||
727 | * record. Tries to find properties 'Title' or 'Name', |
||
728 | * and falls back to the 'ID'. Useful to provide |
||
729 | * user-friendly identification of a record, e.g. in errormessages |
||
730 | * or UI-selections. |
||
731 | * |
||
732 | * Overload this method to have a more specialized implementation, |
||
733 | * e.g. for an Address record this could be: |
||
734 | * <code> |
||
735 | * function getTitle() { |
||
736 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
737 | * } |
||
738 | * </code> |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | public function getTitle() |
||
754 | |||
755 | /** |
||
756 | * Returns the associated database record - in this case, the object itself. |
||
757 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
758 | * |
||
759 | * @return DataObject Associated database record |
||
760 | */ |
||
761 | public function data() |
||
765 | |||
766 | /** |
||
767 | * Convert this object to a map. |
||
768 | * |
||
769 | * @return array The data as a map. |
||
770 | */ |
||
771 | public function toMap() |
||
776 | |||
777 | /** |
||
778 | * Return all currently fetched database fields. |
||
779 | * |
||
780 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
781 | * Obviously, this makes it a lot faster. |
||
782 | * |
||
783 | * @return array The data as a map. |
||
784 | */ |
||
785 | public function getQueriedDatabaseFields() |
||
789 | |||
790 | /** |
||
791 | * Update a number of fields on this object, given a map of the desired changes. |
||
792 | * |
||
793 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
794 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
795 | * |
||
796 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
797 | * the related objects that it alters. |
||
798 | * |
||
799 | * @param array $data A map of field name to data values to update. |
||
800 | * @return DataObject $this |
||
801 | */ |
||
802 | public function update($data) |
||
853 | |||
854 | /** |
||
855 | * Pass changes as a map, and try to |
||
856 | * get automatic casting for these fields. |
||
857 | * Doesn't write to the database. To write the data, |
||
858 | * use the write() method. |
||
859 | * |
||
860 | * @param array $data A map of field name to data values to update. |
||
861 | * @return DataObject $this |
||
862 | */ |
||
863 | public function castedUpdate($data) |
||
870 | |||
871 | /** |
||
872 | * Merges data and relations from another object of same class, |
||
873 | * without conflict resolution. Allows to specify which |
||
874 | * dataset takes priority in case its not empty. |
||
875 | * has_one-relations are just transferred with priority 'right'. |
||
876 | * has_many and many_many-relations are added regardless of priority. |
||
877 | * |
||
878 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
879 | * meaning they are not connected to the merged object any longer. |
||
880 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
881 | * doesn't write the updated object itself (just writes the object-properties). |
||
882 | * Caution: Does not delete the merged object. |
||
883 | * Caution: Does now overwrite Created date on the original object. |
||
884 | * |
||
885 | * @param DataObject $rightObj |
||
886 | * @param string $priority left|right Determines who wins in case of a conflict (optional) |
||
887 | * @param bool $includeRelations Merge any existing relations (optional) |
||
888 | * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values. |
||
889 | * Only applicable with $priority='right'. (optional) |
||
890 | * @return Boolean |
||
891 | */ |
||
892 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) |
||
964 | |||
965 | /** |
||
966 | * Forces the record to think that all its data has changed. |
||
967 | * Doesn't write to the database. Only sets fields as changed |
||
968 | * if they are not already marked as changed. |
||
969 | * |
||
970 | * @return $this |
||
971 | */ |
||
972 | public function forceChange() |
||
1000 | |||
1001 | /** |
||
1002 | * Validate the current object. |
||
1003 | * |
||
1004 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
1005 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
1006 | * |
||
1007 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
1008 | * and onAfterWrite() won't get called either. |
||
1009 | * |
||
1010 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
1011 | * attempting a write, and respond appropriately if it isn't. |
||
1012 | * |
||
1013 | * @see {@link ValidationResult} |
||
1014 | * @return ValidationResult |
||
1015 | */ |
||
1016 | public function validate() |
||
1022 | |||
1023 | /** |
||
1024 | * Public accessor for {@see DataObject::validate()} |
||
1025 | * |
||
1026 | * @return ValidationResult |
||
1027 | */ |
||
1028 | public function doValidate() |
||
1033 | |||
1034 | /** |
||
1035 | * Event handler called before writing to the database. |
||
1036 | * You can overload this to clean up or otherwise process data before writing it to the |
||
1037 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
1038 | * |
||
1039 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
1040 | * |
||
1041 | * @uses DataExtension->onBeforeWrite() |
||
1042 | */ |
||
1043 | protected function onBeforeWrite() |
||
1050 | |||
1051 | /** |
||
1052 | * Event handler called after writing to the database. |
||
1053 | * You can overload this to act upon changes made to the data after it is written. |
||
1054 | * $this->changed will have a record |
||
1055 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
1056 | * |
||
1057 | * @uses DataExtension->onAfterWrite() |
||
1058 | */ |
||
1059 | protected function onAfterWrite() |
||
1064 | |||
1065 | /** |
||
1066 | * Event handler called before deleting from the database. |
||
1067 | * You can overload this to clean up or otherwise process data before delete this |
||
1068 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
1069 | * |
||
1070 | * @uses DataExtension->onBeforeDelete() |
||
1071 | */ |
||
1072 | protected function onBeforeDelete() |
||
1079 | |||
1080 | protected function onAfterDelete() |
||
1084 | |||
1085 | /** |
||
1086 | * Load the default values in from the self::$defaults array. |
||
1087 | * Will traverse the defaults of the current class and all its parent classes. |
||
1088 | * Called by the constructor when creating new records. |
||
1089 | * |
||
1090 | * @uses DataExtension->populateDefaults() |
||
1091 | * @return DataObject $this |
||
1092 | */ |
||
1093 | public function populateDefaults() |
||
1130 | |||
1131 | /** |
||
1132 | * Determine validation of this object prior to write |
||
1133 | * |
||
1134 | * @return ValidationException Exception generated by this write, or null if valid |
||
1135 | */ |
||
1136 | protected function validateWrite() |
||
1154 | |||
1155 | /** |
||
1156 | * Prepare an object prior to write |
||
1157 | * |
||
1158 | * @throws ValidationException |
||
1159 | */ |
||
1160 | protected function preWrite() |
||
1177 | |||
1178 | /** |
||
1179 | * Detects and updates all changes made to this object |
||
1180 | * |
||
1181 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
1182 | * @return bool True if any changes are detected |
||
1183 | */ |
||
1184 | protected function updateChanges($forceChanges = false) |
||
1195 | |||
1196 | /** |
||
1197 | * Writes a subset of changes for a specific table to the given manipulation |
||
1198 | * |
||
1199 | * @param string $baseTable Base table |
||
1200 | * @param string $now Timestamp to use for the current time |
||
1201 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
1202 | * @param array $manipulation Manipulation to write to |
||
1203 | * @param string $class Class of table to manipulate |
||
1204 | */ |
||
1205 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) |
||
1253 | |||
1254 | /** |
||
1255 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
1256 | * |
||
1257 | * Does nothing if an ID is already assigned for this record |
||
1258 | * |
||
1259 | * @param string $baseTable Base table |
||
1260 | * @param string $now Timestamp to use for the current time |
||
1261 | */ |
||
1262 | protected function writeBaseRecord($baseTable, $now) |
||
1277 | |||
1278 | /** |
||
1279 | * Generate and write the database manipulation for all changed fields |
||
1280 | * |
||
1281 | * @param string $baseTable Base table |
||
1282 | * @param string $now Timestamp to use for the current time |
||
1283 | * @param bool $isNewRecord If this is a new record |
||
1284 | */ |
||
1285 | protected function writeManipulation($baseTable, $now, $isNewRecord) |
||
1305 | |||
1306 | /** |
||
1307 | * Writes all changes to this object to the database. |
||
1308 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
1309 | * - All relevant tables will be updated. |
||
1310 | * - $this->onBeforeWrite() gets called beforehand. |
||
1311 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
1312 | * |
||
1313 | * @uses DataExtension->augmentWrite() |
||
1314 | * |
||
1315 | * @param boolean $showDebug Show debugging information |
||
1316 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
1317 | * @param boolean $forceWrite Write to database even if there are no changes |
||
1318 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
1319 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
1320 | * {@link getManyManyComponents()} (Default: false) |
||
1321 | * @return int The ID of the record |
||
1322 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
1323 | */ |
||
1324 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) |
||
1374 | |||
1375 | /** |
||
1376 | * Writes cached relation lists to the database, if possible |
||
1377 | */ |
||
1378 | public function writeRelations() |
||
1392 | |||
1393 | /** |
||
1394 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
1395 | * same record. |
||
1396 | * |
||
1397 | * @param bool $recursive Recursively write components |
||
1398 | * @return DataObject $this |
||
1399 | */ |
||
1400 | public function writeComponents($recursive = false) |
||
1414 | |||
1415 | /** |
||
1416 | * Delete this data object. |
||
1417 | * $this->onBeforeDelete() gets called. |
||
1418 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
1419 | * @uses DataExtension->augmentSQL() |
||
1420 | */ |
||
1421 | public function delete() |
||
1455 | |||
1456 | /** |
||
1457 | * Delete the record with the given ID. |
||
1458 | * |
||
1459 | * @param string $className The class name of the record to be deleted |
||
1460 | * @param int $id ID of record to be deleted |
||
1461 | */ |
||
1462 | public static function delete_by_id($className, $id) |
||
1471 | |||
1472 | /** |
||
1473 | * Get the class ancestry, including the current class name. |
||
1474 | * The ancestry will be returned as an array of class names, where the 0th element |
||
1475 | * will be the class that inherits directly from DataObject, and the last element |
||
1476 | * will be the current class. |
||
1477 | * |
||
1478 | * @return array Class ancestry |
||
1479 | */ |
||
1480 | public function getClassAncestry() |
||
1484 | |||
1485 | /** |
||
1486 | * Return a component object from a one to one relationship, as a DataObject. |
||
1487 | * If no component is available, an 'empty component' will be returned for |
||
1488 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
1489 | * |
||
1490 | * @param string $componentName Name of the component |
||
1491 | * @return DataObject The component object. It's exact type will be that of the component. |
||
1492 | * @throws Exception |
||
1493 | */ |
||
1494 | public function getComponent($componentName) |
||
1566 | |||
1567 | /** |
||
1568 | * Returns a one-to-many relation as a HasManyList |
||
1569 | * |
||
1570 | * @param string $componentName Name of the component |
||
1571 | * @return HasManyList|UnsavedRelationList The components of the one-to-many relationship. |
||
1572 | */ |
||
1573 | public function getComponents($componentName) |
||
1613 | |||
1614 | /** |
||
1615 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
1616 | * |
||
1617 | * @param string $relationName Relation name. |
||
1618 | * @return string Class name, or null if not found. |
||
1619 | */ |
||
1620 | public function getRelationClass($relationName) |
||
1650 | |||
1651 | /** |
||
1652 | * Given a relation name, determine the relation type |
||
1653 | * |
||
1654 | * @param string $component Name of component |
||
1655 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
1656 | */ |
||
1657 | public function getRelationType($component) |
||
1669 | |||
1670 | /** |
||
1671 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
1672 | * side of the relation. |
||
1673 | * |
||
1674 | * Notes on behaviour: |
||
1675 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
1676 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
1677 | * - Cannot be used on polymorphic relationships |
||
1678 | * - Cannot be used on unsaved objects. |
||
1679 | * |
||
1680 | * @param string $remoteClass |
||
1681 | * @param string $remoteRelation |
||
1682 | * @return DataList|DataObject The component, either as a list or single object |
||
1683 | * @throws BadMethodCallException |
||
1684 | * @throws InvalidArgumentException |
||
1685 | */ |
||
1686 | public function inferReciprocalComponent($remoteClass, $remoteRelation) |
||
1792 | |||
1793 | /** |
||
1794 | * Returns a many-to-many component, as a ManyManyList. |
||
1795 | * @param string $componentName Name of the many-many component |
||
1796 | * @return RelationList|UnsavedRelationList The set of components |
||
1797 | */ |
||
1798 | public function getManyManyComponents($componentName) |
||
1849 | |||
1850 | /** |
||
1851 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
1852 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
1853 | * |
||
1854 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
1855 | * their classes. |
||
1856 | */ |
||
1857 | public function hasOne() |
||
1861 | |||
1862 | /** |
||
1863 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
1864 | * their class name will be returned. |
||
1865 | * |
||
1866 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1867 | * the field data stripped off. It defaults to TRUE. |
||
1868 | * @return string|array |
||
1869 | */ |
||
1870 | View Code Duplication | public function belongsTo($classOnly = true) |
|
1879 | |||
1880 | /** |
||
1881 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
1882 | * relationships and their classes will be returned. |
||
1883 | * |
||
1884 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1885 | * the field data stripped off. It defaults to TRUE. |
||
1886 | * @return string|array|false |
||
1887 | */ |
||
1888 | View Code Duplication | public function hasMany($classOnly = true) |
|
1897 | |||
1898 | /** |
||
1899 | * Return the many-to-many extra fields specification. |
||
1900 | * |
||
1901 | * If you don't specify a component name, it returns all |
||
1902 | * extra fields for all components available. |
||
1903 | * |
||
1904 | * @return array|null |
||
1905 | */ |
||
1906 | public function manyManyExtraFields() |
||
1910 | |||
1911 | /** |
||
1912 | * Return information about a many-to-many component. |
||
1913 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
1914 | * components are returned. |
||
1915 | * |
||
1916 | * @see DataObjectSchema::manyManyComponent() |
||
1917 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
1918 | */ |
||
1919 | public function manyMany() |
||
1927 | |||
1928 | /** |
||
1929 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
1930 | * |
||
1931 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
1932 | * |
||
1933 | * @param string $class |
||
1934 | * @return array|false |
||
1935 | */ |
||
1936 | public function database_extensions($class) |
||
1945 | |||
1946 | /** |
||
1947 | * Generates a SearchContext to be used for building and processing |
||
1948 | * a generic search form for properties on this object. |
||
1949 | * |
||
1950 | * @return SearchContext |
||
1951 | */ |
||
1952 | public function getDefaultSearchContext() |
||
1960 | |||
1961 | /** |
||
1962 | * Determine which properties on the DataObject are |
||
1963 | * searchable, and map them to their default {@link FormField} |
||
1964 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
1965 | * |
||
1966 | * Some additional logic is included for switching field labels, based on |
||
1967 | * how generic or specific the field type is. |
||
1968 | * |
||
1969 | * Used by {@link SearchContext}. |
||
1970 | * |
||
1971 | * @param array $_params |
||
1972 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
1973 | * 'restrictFields': Numeric array of a field name whitelist |
||
1974 | * @return FieldList |
||
1975 | */ |
||
1976 | public function scaffoldSearchFields($_params = null) |
||
2032 | |||
2033 | /** |
||
2034 | * Scaffold a simple edit form for all properties on this dataobject, |
||
2035 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
2036 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
2037 | * |
||
2038 | * @uses FormScaffolder |
||
2039 | * |
||
2040 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
2041 | * @return FieldList |
||
2042 | */ |
||
2043 | public function scaffoldFormFields($_params = null) |
||
2065 | |||
2066 | /** |
||
2067 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
2068 | * being called on extensions |
||
2069 | * |
||
2070 | * @param callable $callback The callback to execute |
||
2071 | */ |
||
2072 | protected function beforeUpdateCMSFields($callback) |
||
2076 | |||
2077 | /** |
||
2078 | * Centerpiece of every data administration interface in Silverstripe, |
||
2079 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
2080 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
2081 | * generate this set. To customize, overload this method in a subclass |
||
2082 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
2083 | * |
||
2084 | * <code> |
||
2085 | * class MyCustomClass extends DataObject { |
||
2086 | * static $db = array('CustomProperty'=>'Boolean'); |
||
2087 | * |
||
2088 | * function getCMSFields() { |
||
2089 | * $fields = parent::getCMSFields(); |
||
2090 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
2091 | * return $fields; |
||
2092 | * } |
||
2093 | * } |
||
2094 | * </code> |
||
2095 | * |
||
2096 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
2097 | * |
||
2098 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
2099 | */ |
||
2100 | public function getCMSFields() |
||
2113 | |||
2114 | /** |
||
2115 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
2116 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
2117 | * |
||
2118 | * @return FieldList an Empty FieldList(); need to be overload by solid subclass |
||
2119 | */ |
||
2120 | public function getCMSActions() |
||
2126 | |||
2127 | |||
2128 | /** |
||
2129 | * Used for simple frontend forms without relation editing |
||
2130 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
2131 | * by default. To customize, either overload this method in your |
||
2132 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
2133 | * |
||
2134 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
2135 | * |
||
2136 | * @param array $params See {@link scaffoldFormFields()} |
||
2137 | * @return FieldList Always returns a simple field collection without TabSet. |
||
2138 | */ |
||
2139 | public function getFrontEndFields($params = null) |
||
2146 | |||
2147 | /** |
||
2148 | * Gets the value of a field. |
||
2149 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
2150 | * |
||
2151 | * @param string $field The name of the field |
||
2152 | * @return mixed The field value |
||
2153 | */ |
||
2154 | public function getField($field) |
||
2174 | |||
2175 | /** |
||
2176 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
2177 | * |
||
2178 | * @param string $class Class to load the values from. Others are joined as required. |
||
2179 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
2180 | * @return bool Flag if lazy loading succeeded |
||
2181 | */ |
||
2182 | protected function loadLazyFields($class = null) |
||
2258 | |||
2259 | /** |
||
2260 | * Return the fields that have changed. |
||
2261 | * |
||
2262 | * The change level affects what the functions defines as "changed": |
||
2263 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
2264 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
2265 | * for example a change from 0 to null would not be included. |
||
2266 | * |
||
2267 | * Example return: |
||
2268 | * <code> |
||
2269 | * array( |
||
2270 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
2271 | * ) |
||
2272 | * </code> |
||
2273 | * |
||
2274 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
2275 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
2276 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
2277 | * @return array |
||
2278 | */ |
||
2279 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) |
||
2326 | |||
2327 | /** |
||
2328 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
2329 | * since loading them from the database. |
||
2330 | * |
||
2331 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
2332 | * @param int $changeLevel See {@link getChangedFields()} |
||
2333 | * @return boolean |
||
2334 | */ |
||
2335 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) |
||
2345 | |||
2346 | /** |
||
2347 | * Set the value of the field |
||
2348 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
2349 | * |
||
2350 | * @param string $fieldName Name of the field |
||
2351 | * @param mixed $val New field value |
||
2352 | * @return $this |
||
2353 | */ |
||
2354 | public function setField($fieldName, $val) |
||
2406 | |||
2407 | /** |
||
2408 | * Set the value of the field, using a casting object. |
||
2409 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
2410 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
2411 | * can be saved into the Image table. |
||
2412 | * |
||
2413 | * @param string $fieldName Name of the field |
||
2414 | * @param mixed $value New field value |
||
2415 | * @return $this |
||
2416 | */ |
||
2417 | public function setCastedField($fieldName, $value) |
||
2431 | |||
2432 | /** |
||
2433 | * {@inheritdoc} |
||
2434 | */ |
||
2435 | public function castingHelper($field) |
||
2455 | |||
2456 | /** |
||
2457 | * Returns true if the given field exists in a database column on any of |
||
2458 | * the objects tables and optionally look up a dynamic getter with |
||
2459 | * get<fieldName>(). |
||
2460 | * |
||
2461 | * @param string $field Name of the field |
||
2462 | * @return boolean True if the given field exists |
||
2463 | */ |
||
2464 | public function hasField($field) |
||
2474 | |||
2475 | /** |
||
2476 | * Returns true if the given field exists as a database column |
||
2477 | * |
||
2478 | * @param string $field Name of the field |
||
2479 | * |
||
2480 | * @return boolean |
||
2481 | */ |
||
2482 | public function hasDatabaseField($field) |
||
2487 | |||
2488 | /** |
||
2489 | * Returns true if the member is allowed to do the given action. |
||
2490 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
2491 | * |
||
2492 | * @param string $perm The permission to be checked, such as 'View'. |
||
2493 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
2494 | * in user. |
||
2495 | * @param array $context Additional $context to pass to extendedCan() |
||
2496 | * |
||
2497 | * @return boolean True if the the member is allowed to do the given action |
||
2498 | */ |
||
2499 | public function can($perm, $member = null, $context = array()) |
||
2521 | |||
2522 | /** |
||
2523 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
2524 | * expected to return one of three values: |
||
2525 | * |
||
2526 | * - false: Disallow this permission, regardless of what other extensions say |
||
2527 | * - true: Allow this permission, as long as no other extensions return false |
||
2528 | * - NULL: Don't affect the outcome |
||
2529 | * |
||
2530 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
2531 | * |
||
2532 | * <code> |
||
2533 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
2534 | * if($extended !== null) return $extended; |
||
2535 | * else return $normalValue; |
||
2536 | * </code> |
||
2537 | * |
||
2538 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
2539 | * @param Member|int $member |
||
2540 | * @param array $context Optional context |
||
2541 | * @return boolean|null |
||
2542 | */ |
||
2543 | public function extendedCan($methodName, $member, $context = array()) |
||
2559 | |||
2560 | /** |
||
2561 | * @param Member $member |
||
2562 | * @return boolean |
||
2563 | */ |
||
2564 | View Code Duplication | public function canView($member = null) |
|
2572 | |||
2573 | /** |
||
2574 | * @param Member $member |
||
2575 | * @return boolean |
||
2576 | */ |
||
2577 | View Code Duplication | public function canEdit($member = null) |
|
2585 | |||
2586 | /** |
||
2587 | * @param Member $member |
||
2588 | * @return boolean |
||
2589 | */ |
||
2590 | View Code Duplication | public function canDelete($member = null) |
|
2598 | |||
2599 | /** |
||
2600 | * @param Member $member |
||
2601 | * @param array $context Additional context-specific data which might |
||
2602 | * affect whether (or where) this object could be created. |
||
2603 | * @return boolean |
||
2604 | */ |
||
2605 | View Code Duplication | public function canCreate($member = null, $context = array()) |
|
2613 | |||
2614 | /** |
||
2615 | * Debugging used by Debug::show() |
||
2616 | * |
||
2617 | * @return string HTML data representing this object |
||
2618 | */ |
||
2619 | public function debug() |
||
2631 | |||
2632 | /** |
||
2633 | * Return the DBField object that represents the given field. |
||
2634 | * This works similarly to obj() with 2 key differences: |
||
2635 | * - it still returns an object even when the field has no value. |
||
2636 | * - it only matches fields and not methods |
||
2637 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
2638 | * |
||
2639 | * @param string $fieldName Name of the field |
||
2640 | * @return DBField The field as a DBField object |
||
2641 | */ |
||
2642 | public function dbObject($fieldName) |
||
2673 | |||
2674 | /** |
||
2675 | * Traverses to a DBField referenced by relationships between data objects. |
||
2676 | * |
||
2677 | * The path to the related field is specified with dot separated syntax |
||
2678 | * (eg: Parent.Child.Child.FieldName). |
||
2679 | * |
||
2680 | * @param string $fieldPath |
||
2681 | * |
||
2682 | * @return mixed DBField of the field on the object or a DataList instance. |
||
2683 | */ |
||
2684 | public function relObject($fieldPath) |
||
2715 | |||
2716 | /** |
||
2717 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
2718 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
2719 | * |
||
2720 | * @param $fieldName string |
||
2721 | * @return string | null - will return null on a missing value |
||
2722 | */ |
||
2723 | public function relField($fieldName) |
||
2760 | |||
2761 | /** |
||
2762 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
2763 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
2764 | * |
||
2765 | * @param string $className |
||
2766 | * @return string |
||
2767 | */ |
||
2768 | public function getReverseAssociation($className) |
||
2791 | |||
2792 | /** |
||
2793 | * Return all objects matching the filter |
||
2794 | * sub-classes are automatically selected and included |
||
2795 | * |
||
2796 | * @param string $callerClass The class of objects to be returned |
||
2797 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
2798 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
2799 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
2800 | * BY clause. If omitted, self::$default_sort will be used. |
||
2801 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
2802 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
2803 | * @param string $containerClass The container class to return the results in. |
||
2804 | * |
||
2805 | * @todo $containerClass is Ignored, why? |
||
2806 | * |
||
2807 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
2808 | */ |
||
2809 | public static function get( |
||
2852 | |||
2853 | |||
2854 | /** |
||
2855 | * Return the first item matching the given query. |
||
2856 | * All calls to get_one() are cached. |
||
2857 | * |
||
2858 | * @param string $callerClass The class of objects to be returned |
||
2859 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
2860 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
2861 | * @param boolean $cache Use caching |
||
2862 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
2863 | * |
||
2864 | * @return DataObject The first item matching the query |
||
2865 | */ |
||
2866 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") |
||
2893 | |||
2894 | /** |
||
2895 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
2896 | * Also clears any cached aggregate data. |
||
2897 | * |
||
2898 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
2899 | * When false will just clear session-local cached data |
||
2900 | * @return DataObject $this |
||
2901 | */ |
||
2902 | public function flushCache($persistent = true) |
||
2921 | |||
2922 | /** |
||
2923 | * Flush the get_one global cache and destroy associated objects. |
||
2924 | */ |
||
2925 | public static function flush_and_destroy_cache() |
||
2940 | |||
2941 | /** |
||
2942 | * Reset all global caches associated with DataObject. |
||
2943 | */ |
||
2944 | public static function reset() |
||
2953 | |||
2954 | /** |
||
2955 | * Return the given element, searching by ID |
||
2956 | * |
||
2957 | * @param string $callerClass The class of the object to be returned |
||
2958 | * @param int $id The id of the element |
||
2959 | * @param boolean $cache See {@link get_one()} |
||
2960 | * |
||
2961 | * @return DataObject The element |
||
2962 | */ |
||
2963 | public static function get_by_id($callerClass, $id, $cache = true) |
||
2973 | |||
2974 | /** |
||
2975 | * Get the name of the base table for this object |
||
2976 | * |
||
2977 | * @return string |
||
2978 | */ |
||
2979 | public function baseTable() |
||
2983 | |||
2984 | /** |
||
2985 | * Get the base class for this object |
||
2986 | * |
||
2987 | * @return string |
||
2988 | */ |
||
2989 | public function baseClass() |
||
2993 | |||
2994 | /** |
||
2995 | * @var array Parameters used in the query that built this object. |
||
2996 | * This can be used by decorators (e.g. lazy loading) to |
||
2997 | * run additional queries using the same context. |
||
2998 | */ |
||
2999 | protected $sourceQueryParams; |
||
3000 | |||
3001 | /** |
||
3002 | * @see $sourceQueryParams |
||
3003 | * @return array |
||
3004 | */ |
||
3005 | public function getSourceQueryParams() |
||
3009 | |||
3010 | /** |
||
3011 | * Get list of parameters that should be inherited to relations on this object |
||
3012 | * |
||
3013 | * @return array |
||
3014 | */ |
||
3015 | public function getInheritableQueryParams() |
||
3021 | |||
3022 | /** |
||
3023 | * @see $sourceQueryParams |
||
3024 | * @param array |
||
3025 | */ |
||
3026 | public function setSourceQueryParams($array) |
||
3030 | |||
3031 | /** |
||
3032 | * @see $sourceQueryParams |
||
3033 | * @param string $key |
||
3034 | * @param string $value |
||
3035 | */ |
||
3036 | public function setSourceQueryParam($key, $value) |
||
3040 | |||
3041 | /** |
||
3042 | * @see $sourceQueryParams |
||
3043 | * @param string $key |
||
3044 | * @return string |
||
3045 | */ |
||
3046 | public function getSourceQueryParam($key) |
||
3053 | |||
3054 | //-------------------------------------------------------------------------------------------// |
||
3055 | |||
3056 | /** |
||
3057 | * Return the database indexes on this table. |
||
3058 | * This array is indexed by the name of the field with the index, and |
||
3059 | * the value is the type of index. |
||
3060 | */ |
||
3061 | public function databaseIndexes() |
||
3087 | |||
3088 | /** |
||
3089 | * Check the database schema and update it as necessary. |
||
3090 | * |
||
3091 | * @uses DataExtension->augmentDatabase() |
||
3092 | */ |
||
3093 | public function requireTable() |
||
3159 | |||
3160 | /** |
||
3161 | * Add default records to database. This function is called whenever the |
||
3162 | * database is built, after the database tables have all been created. Overload |
||
3163 | * this to add default records when the database is built, but make sure you |
||
3164 | * call parent::requireDefaultRecords(). |
||
3165 | * |
||
3166 | * @uses DataExtension->requireDefaultRecords() |
||
3167 | */ |
||
3168 | public function requireDefaultRecords() |
||
3187 | |||
3188 | /** |
||
3189 | * Get the default searchable fields for this object, as defined in the |
||
3190 | * $searchable_fields list. If searchable fields are not defined on the |
||
3191 | * data object, uses a default selection of summary fields. |
||
3192 | * |
||
3193 | * @return array |
||
3194 | */ |
||
3195 | public function searchableFields() |
||
3272 | |||
3273 | /** |
||
3274 | * Get any user defined searchable fields labels that |
||
3275 | * exist. Allows overriding of default field names in the form |
||
3276 | * interface actually presented to the user. |
||
3277 | * |
||
3278 | * The reason for keeping this separate from searchable_fields, |
||
3279 | * which would be a logical place for this functionality, is to |
||
3280 | * avoid bloating and complicating the configuration array. Currently |
||
3281 | * much of this system is based on sensible defaults, and this property |
||
3282 | * would generally only be set in the case of more complex relationships |
||
3283 | * between data object being required in the search interface. |
||
3284 | * |
||
3285 | * Generates labels based on name of the field itself, if no static property |
||
3286 | * {@link self::field_labels} exists. |
||
3287 | * |
||
3288 | * @uses $field_labels |
||
3289 | * @uses FormField::name_to_label() |
||
3290 | * |
||
3291 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
3292 | * |
||
3293 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
3294 | */ |
||
3295 | public function fieldLabels($includerelations = true) |
||
3335 | |||
3336 | /** |
||
3337 | * Get a human-readable label for a single field, |
||
3338 | * see {@link fieldLabels()} for more details. |
||
3339 | * |
||
3340 | * @uses fieldLabels() |
||
3341 | * @uses FormField::name_to_label() |
||
3342 | * |
||
3343 | * @param string $name Name of the field |
||
3344 | * @return string Label of the field |
||
3345 | */ |
||
3346 | public function fieldLabel($name) |
||
3351 | |||
3352 | /** |
||
3353 | * Get the default summary fields for this object. |
||
3354 | * |
||
3355 | * @todo use the translation apparatus to return a default field selection for the language |
||
3356 | * |
||
3357 | * @return array |
||
3358 | */ |
||
3359 | public function summaryFields() |
||
3403 | |||
3404 | /** |
||
3405 | * Defines a default list of filters for the search context. |
||
3406 | * |
||
3407 | * If a filter class mapping is defined on the data object, |
||
3408 | * it is constructed here. Otherwise, the default filter specified in |
||
3409 | * {@link DBField} is used. |
||
3410 | * |
||
3411 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
3412 | * |
||
3413 | * @return array |
||
3414 | */ |
||
3415 | public function defaultSearchFilters() |
||
3432 | |||
3433 | /** |
||
3434 | * @return boolean True if the object is in the database |
||
3435 | */ |
||
3436 | public function isInDB() |
||
3440 | |||
3441 | /* |
||
3442 | * @ignore |
||
3443 | */ |
||
3444 | private static $subclass_access = true; |
||
3445 | |||
3446 | /** |
||
3447 | * Temporarily disable subclass access in data object qeur |
||
3448 | */ |
||
3449 | public static function disable_subclass_access() |
||
3457 | |||
3458 | //-------------------------------------------------------------------------------------------// |
||
3459 | |||
3460 | /** |
||
3461 | * Database field definitions. |
||
3462 | * This is a map from field names to field type. The field |
||
3463 | * type should be a class that extends . |
||
3464 | * @var array |
||
3465 | * @config |
||
3466 | */ |
||
3467 | private static $db = []; |
||
3468 | |||
3469 | /** |
||
3470 | * Use a casting object for a field. This is a map from |
||
3471 | * field name to class name of the casting object. |
||
3472 | * |
||
3473 | * @var array |
||
3474 | */ |
||
3475 | private static $casting = array( |
||
3476 | "Title" => 'Text', |
||
3477 | ); |
||
3478 | |||
3479 | /** |
||
3480 | * Specify custom options for a CREATE TABLE call. |
||
3481 | * Can be used to specify a custom storage engine for specific database table. |
||
3482 | * All options have to be keyed for a specific database implementation, |
||
3483 | * identified by their class name (extending from {@link SS_Database}). |
||
3484 | * |
||
3485 | * <code> |
||
3486 | * array( |
||
3487 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
3488 | * ) |
||
3489 | * </code> |
||
3490 | * |
||
3491 | * Caution: This API is experimental, and might not be |
||
3492 | * included in the next major release. Please use with care. |
||
3493 | * |
||
3494 | * @var array |
||
3495 | * @config |
||
3496 | */ |
||
3497 | private static $create_table_options = array( |
||
3498 | 'SilverStripe\ORM\Connect\MySQLDatabase' => 'ENGINE=InnoDB' |
||
3499 | ); |
||
3500 | |||
3501 | /** |
||
3502 | * If a field is in this array, then create a database index |
||
3503 | * on that field. This is a map from fieldname to index type. |
||
3504 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
3505 | * |
||
3506 | * @var array |
||
3507 | * @config |
||
3508 | */ |
||
3509 | private static $indexes = null; |
||
3510 | |||
3511 | /** |
||
3512 | * Inserts standard column-values when a DataObject |
||
3513 | * is instanciated. Does not insert default records {@see $default_records}. |
||
3514 | * This is a map from fieldname to default value. |
||
3515 | * |
||
3516 | * - If you would like to change a default value in a sub-class, just specify it. |
||
3517 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
3518 | * or false in your subclass. Setting it to null won't work. |
||
3519 | * |
||
3520 | * @var array |
||
3521 | * @config |
||
3522 | */ |
||
3523 | private static $defaults = []; |
||
3524 | |||
3525 | /** |
||
3526 | * Multidimensional array which inserts default data into the database |
||
3527 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
3528 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
3529 | * behaviour such as publishing and ParentNodes. |
||
3530 | * |
||
3531 | * Example: |
||
3532 | * array( |
||
3533 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
3534 | * array('Title' => "DefaultPage2") |
||
3535 | * ). |
||
3536 | * |
||
3537 | * @var array |
||
3538 | * @config |
||
3539 | */ |
||
3540 | private static $default_records = null; |
||
3541 | |||
3542 | /** |
||
3543 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
3544 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
3545 | * |
||
3546 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3547 | * |
||
3548 | * @var array |
||
3549 | * @config |
||
3550 | */ |
||
3551 | private static $has_one = []; |
||
3552 | |||
3553 | /** |
||
3554 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
3555 | * |
||
3556 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
3557 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
3558 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
3559 | * |
||
3560 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3561 | * |
||
3562 | * @var array |
||
3563 | * @config |
||
3564 | */ |
||
3565 | private static $belongs_to = []; |
||
3566 | |||
3567 | /** |
||
3568 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
3569 | * |
||
3570 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
3571 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
3572 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
3573 | * which foreign key to use. |
||
3574 | * |
||
3575 | * @var array |
||
3576 | * @config |
||
3577 | */ |
||
3578 | private static $has_many = []; |
||
3579 | |||
3580 | /** |
||
3581 | * many-many relationship definitions. |
||
3582 | * This is a map from component name to data type. |
||
3583 | * @var array |
||
3584 | * @config |
||
3585 | */ |
||
3586 | private static $many_many = []; |
||
3587 | |||
3588 | /** |
||
3589 | * Extra fields to include on the connecting many-many table. |
||
3590 | * This is a map from field name to field type. |
||
3591 | * |
||
3592 | * Example code: |
||
3593 | * <code> |
||
3594 | * public static $many_many_extraFields = array( |
||
3595 | * 'Members' => array( |
||
3596 | * 'Role' => 'Varchar(100)' |
||
3597 | * ) |
||
3598 | * ); |
||
3599 | * </code> |
||
3600 | * |
||
3601 | * @var array |
||
3602 | * @config |
||
3603 | */ |
||
3604 | private static $many_many_extraFields = []; |
||
3605 | |||
3606 | /** |
||
3607 | * The inverse side of a many-many relationship. |
||
3608 | * This is a map from component name to data type. |
||
3609 | * @var array |
||
3610 | * @config |
||
3611 | */ |
||
3612 | private static $belongs_many_many = []; |
||
3613 | |||
3614 | /** |
||
3615 | * The default sort expression. This will be inserted in the ORDER BY |
||
3616 | * clause of a SQL query if no other sort expression is provided. |
||
3617 | * @var string |
||
3618 | * @config |
||
3619 | */ |
||
3620 | private static $default_sort = null; |
||
3621 | |||
3622 | /** |
||
3623 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
3624 | * search interface. |
||
3625 | * |
||
3626 | * Overriding the default filter, with a custom defined filter: |
||
3627 | * <code> |
||
3628 | * static $searchable_fields = array( |
||
3629 | * "Name" => "PartialMatchFilter" |
||
3630 | * ); |
||
3631 | * </code> |
||
3632 | * |
||
3633 | * Overriding the default form fields, with a custom defined field. |
||
3634 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
3635 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
3636 | * <code> |
||
3637 | * static $searchable_fields = array( |
||
3638 | * "Name" => array( |
||
3639 | * "field" => "TextField" |
||
3640 | * ) |
||
3641 | * ); |
||
3642 | * </code> |
||
3643 | * |
||
3644 | * Overriding the default form field, filter and title: |
||
3645 | * <code> |
||
3646 | * static $searchable_fields = array( |
||
3647 | * "Organisation.ZipCode" => array( |
||
3648 | * "field" => "TextField", |
||
3649 | * "filter" => "PartialMatchFilter", |
||
3650 | * "title" => 'Organisation ZIP' |
||
3651 | * ) |
||
3652 | * ); |
||
3653 | * </code> |
||
3654 | * @config |
||
3655 | */ |
||
3656 | private static $searchable_fields = null; |
||
3657 | |||
3658 | /** |
||
3659 | * User defined labels for searchable_fields, used to override |
||
3660 | * default display in the search form. |
||
3661 | * @config |
||
3662 | */ |
||
3663 | private static $field_labels = []; |
||
3664 | |||
3665 | /** |
||
3666 | * Provides a default list of fields to be used by a 'summary' |
||
3667 | * view of this object. |
||
3668 | * @config |
||
3669 | */ |
||
3670 | private static $summary_fields = []; |
||
3671 | |||
3672 | public function provideI18nEntities() |
||
3688 | |||
3689 | /** |
||
3690 | * Returns true if the given method/parameter has a value |
||
3691 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
3692 | * |
||
3693 | * @param string $field The field name |
||
3694 | * @param array $arguments |
||
3695 | * @param bool $cache |
||
3696 | * @return boolean |
||
3697 | */ |
||
3698 | public function hasValue($field, $arguments = null, $cache = true) |
||
3708 | |||
3709 | /** |
||
3710 | * If selected through a many_many through relation, this is the instance of the joined record |
||
3711 | * |
||
3712 | * @return DataObject |
||
3713 | */ |
||
3714 | public function getJoin() |
||
3718 | |||
3719 | /** |
||
3720 | * Set joining object |
||
3721 | * |
||
3722 | * @param DataObject $object |
||
3723 | * @param string $alias Alias |
||
3724 | * @return $this |
||
3725 | */ |
||
3726 | public function setJoin(DataObject $object, $alias = null) |
||
3739 | } |
||
3740 |
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.