Complex classes like DataObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
75 | |||
76 | /** |
||
77 | * Human-readable singular name. |
||
78 | * @var string |
||
79 | * @config |
||
80 | */ |
||
81 | private static $singular_name = null; |
||
82 | |||
83 | /** |
||
84 | * Human-readable plural name |
||
85 | * @var string |
||
86 | * @config |
||
87 | */ |
||
88 | private static $plural_name = null; |
||
89 | |||
90 | /** |
||
91 | * Allow API access to this object? |
||
92 | * @todo Define the options that can be set here |
||
93 | * @config |
||
94 | */ |
||
95 | private static $api_access = false; |
||
96 | |||
97 | /** |
||
98 | * True if this DataObject has been destroyed. |
||
99 | * @var boolean |
||
100 | */ |
||
101 | public $destroyed = false; |
||
102 | |||
103 | /** |
||
104 | * The DataModel from this this object comes |
||
105 | */ |
||
106 | protected $model; |
||
107 | |||
108 | /** |
||
109 | * Data stored in this objects database record. An array indexed by fieldname. |
||
110 | * |
||
111 | * Use {@link toMap()} if you want an array representation |
||
112 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $record; |
||
117 | |||
118 | /** |
||
119 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
120 | */ |
||
121 | const CHANGE_NONE = 0; |
||
122 | |||
123 | /** |
||
124 | * Represents a field that has changed type, although not the loosely defined value. |
||
125 | * (before !== after && before == after) |
||
126 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
127 | * Value changes are by nature also considered strict changes. |
||
128 | */ |
||
129 | const CHANGE_STRICT = 1; |
||
130 | |||
131 | /** |
||
132 | * Represents a field that has changed the loosely defined value |
||
133 | * (before != after, thus, before !== after)) |
||
134 | * E.g. change false to true, but not false to 0 |
||
135 | */ |
||
136 | const CHANGE_VALUE = 2; |
||
137 | |||
138 | /** |
||
139 | * An array indexed by fieldname, true if the field has been changed. |
||
140 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
141 | * the changed state. |
||
142 | * |
||
143 | * @var array |
||
144 | */ |
||
145 | private $changed; |
||
146 | |||
147 | /** |
||
148 | * The database record (in the same format as $record), before |
||
149 | * any changes. |
||
150 | * @var array |
||
151 | */ |
||
152 | protected $original; |
||
153 | |||
154 | /** |
||
155 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
156 | * @var boolean |
||
157 | */ |
||
158 | protected $brokenOnDelete = false; |
||
159 | |||
160 | /** |
||
161 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
162 | * @var boolean |
||
163 | */ |
||
164 | protected $brokenOnWrite = false; |
||
165 | |||
166 | /** |
||
167 | * @config |
||
168 | * @var boolean Should dataobjects be validated before they are written? |
||
169 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
170 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
171 | * to only disable validation for very specific use cases. |
||
172 | */ |
||
173 | private static $validation_enabled = true; |
||
174 | |||
175 | /** |
||
176 | * Static caches used by relevant functions. |
||
177 | */ |
||
178 | public static $cache_has_own_table = array(); |
||
179 | protected static $_cache_db = array(); |
||
180 | protected static $_cache_get_one; |
||
181 | protected static $_cache_get_class_ancestry; |
||
182 | protected static $_cache_composite_fields = array(); |
||
183 | protected static $_cache_is_composite_field = array(); |
||
184 | protected static $_cache_custom_database_fields = array(); |
||
185 | protected static $_cache_field_labels = array(); |
||
186 | |||
187 | // base fields which are not defined in static $db |
||
188 | private static $fixed_fields = array( |
||
189 | 'ID' => 'Int', |
||
190 | 'ClassName' => 'Enum', |
||
191 | 'LastEdited' => 'SS_Datetime', |
||
192 | 'Created' => 'SS_Datetime', |
||
193 | ); |
||
194 | |||
195 | /** |
||
196 | * Non-static relationship cache, indexed by component name. |
||
197 | */ |
||
198 | protected $components; |
||
199 | |||
200 | /** |
||
201 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
202 | */ |
||
203 | protected $unsavedRelations; |
||
204 | |||
205 | /** |
||
206 | * Returns when validation on DataObjects is enabled. |
||
207 | * |
||
208 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
209 | * @return bool |
||
210 | */ |
||
211 | public static function get_validation_enabled() { |
||
215 | |||
216 | /** |
||
217 | * Set whether DataObjects should be validated before they are written. |
||
218 | * |
||
219 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
220 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
221 | * to only disable validation for very specific use cases. |
||
222 | * |
||
223 | * @param $enable bool |
||
224 | * @see DataObject::validate() |
||
225 | * @deprecated 3.2 Use the "DataObject.validation_enabled" config setting instead |
||
226 | */ |
||
227 | public static function set_validation_enabled($enable) { |
||
231 | |||
232 | /** |
||
233 | * @var [string] - class => ClassName field definition cache for self::database_fields |
||
234 | */ |
||
235 | private static $classname_spec_cache = array(); |
||
236 | |||
237 | /** |
||
238 | * Clear all cached classname specs. It's necessary to clear all cached subclassed names |
||
239 | * for any classes if a new class manifest is generated. |
||
240 | */ |
||
241 | public static function clear_classname_spec_cache() { |
||
245 | |||
246 | /** |
||
247 | * Determines the specification for the ClassName field for the given class |
||
248 | * |
||
249 | * @param string $class |
||
250 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
251 | * @return string Resulting ClassName spec. If $queryDB is true this will include all |
||
252 | * legacy types that no longer have concrete classes in PHP |
||
253 | */ |
||
254 | public static function get_classname_spec($class, $queryDB = true) { |
||
272 | |||
273 | /** |
||
274 | * Return the complete map of fields on this object, including "Created", "LastEdited" and "ClassName". |
||
275 | * See {@link custom_database_fields()} for a getter that excludes these "base fields". |
||
276 | * |
||
277 | * @param string $class |
||
278 | * @param boolean $queryDB Determine if the DB may be queried for additional information |
||
279 | * @return array |
||
280 | */ |
||
281 | public static function database_fields($class, $queryDB = true) { |
||
295 | |||
296 | /** |
||
297 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
298 | * and {@link DataObject::$has_one}. Resolves instances of {@link CompositeDBField} |
||
299 | * into the actual database fields, rather than the name of the field which |
||
300 | * might not equate a database column. |
||
301 | * |
||
302 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
303 | * see {@link database_fields()}. |
||
304 | * |
||
305 | * @uses CompositeDBField->compositeDatabaseFields() |
||
306 | * |
||
307 | * @param string $class |
||
308 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
309 | */ |
||
310 | public static function custom_database_fields($class) { |
||
353 | |||
354 | /** |
||
355 | * Returns the field class if the given db field on the class is a composite field. |
||
356 | * Will check all applicable ancestor classes and aggregate results. |
||
357 | * |
||
358 | * @param string $class Class to check |
||
359 | * @param string $name Field to check |
||
360 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
361 | * @return string Class name of composite field if it exists |
||
362 | */ |
||
363 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
384 | |||
385 | /** |
||
386 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
387 | * Will check all applicable ancestor classes and aggregate results. |
||
388 | */ |
||
389 | public static function composite_fields($class, $aggregated = true) { |
||
401 | |||
402 | /** |
||
403 | * Internal cacher for the composite field information |
||
404 | */ |
||
405 | private static function cache_composite_fields($class) { |
||
424 | |||
425 | /** |
||
426 | * Creates a class instance by the "singleton" design pattern. |
||
427 | * It will always return the same instance for this class, |
||
428 | * which can be used for performance reasons and as a simple |
||
429 | * way to access instance methods which don't rely on instance |
||
430 | * data (e.g. the custom SilverStripe static handling). |
||
431 | * |
||
432 | * @return static The singleton instance |
||
433 | */ |
||
434 | public static function singleton() { |
||
435 | $class = get_called_class(); |
||
436 | return Injector::inst()->get($class, true, array(null, true)); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Construct a new DataObject. |
||
441 | * |
||
442 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
443 | * field values. Normally this contructor is only used by the internal systems that get objects from the database. |
||
444 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
445 | * Singletons don't have their defaults set. |
||
446 | */ |
||
447 | public function __construct($record = null, $isSingleton = false, $model = null) { |
||
448 | parent::__construct(); |
||
449 | |||
450 | // Set the fields data. |
||
451 | if(!$record) { |
||
452 | $record = array( |
||
453 | 'ID' => 0, |
||
454 | 'ClassName' => get_class($this), |
||
455 | 'RecordClassName' => get_class($this) |
||
456 | ); |
||
457 | } |
||
458 | |||
459 | if(!is_array($record) && !is_a($record, "stdClass")) { |
||
460 | if(is_object($record)) $passed = "an object of type '$record->class'"; |
||
461 | else $passed = "The value '$record'"; |
||
462 | |||
463 | user_error("DataObject::__construct passed $passed. It's supposed to be passed an array," |
||
464 | . " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?", |
||
465 | E_USER_WARNING); |
||
466 | $record = null; |
||
467 | } |
||
468 | |||
469 | if(is_a($record, "stdClass")) { |
||
470 | $record = (array)$record; |
||
471 | } |
||
472 | |||
473 | // Set $this->record to $record, but ignore NULLs |
||
474 | $this->record = array(); |
||
475 | foreach($record as $k => $v) { |
||
476 | // Ensure that ID is stored as a number and not a string |
||
477 | // To do: this kind of clean-up should be done on all numeric fields, in some relatively |
||
478 | // performant manner |
||
479 | if($v !== null) { |
||
480 | if($k == 'ID' && is_numeric($v)) $this->record[$k] = (int)$v; |
||
481 | else $this->record[$k] = $v; |
||
482 | } |
||
483 | } |
||
484 | |||
485 | // Identify fields that should be lazy loaded, but only on existing records |
||
486 | if(!empty($record['ID'])) { |
||
487 | $currentObj = get_class($this); |
||
488 | while($currentObj != 'DataObject') { |
||
489 | $fields = self::custom_database_fields($currentObj); |
||
490 | foreach($fields as $field => $type) { |
||
491 | if(!array_key_exists($field, $record)) $this->record[$field.'_Lazy'] = $currentObj; |
||
492 | } |
||
493 | $currentObj = get_parent_class($currentObj); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | $this->original = $this->record; |
||
498 | |||
499 | // Keep track of the modification date of all the data sourced to make this page |
||
500 | // From this we create a Last-Modified HTTP header |
||
501 | if(isset($record['LastEdited'])) { |
||
502 | HTTP::register_modification_date($record['LastEdited']); |
||
503 | } |
||
504 | |||
505 | // this must be called before populateDefaults(), as field getters on a DataObject |
||
506 | // may call getComponent() and others, which rely on $this->model being set. |
||
507 | $this->model = $model ? $model : DataModel::inst(); |
||
508 | |||
509 | // Must be called after parent constructor |
||
510 | if(!$isSingleton && (!isset($this->record['ID']) || !$this->record['ID'])) { |
||
511 | $this->populateDefaults(); |
||
512 | } |
||
513 | |||
514 | // prevent populateDefaults() and setField() from marking overwritten defaults as changed |
||
515 | $this->changed = array(); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Set the DataModel |
||
520 | * @param DataModel $model |
||
521 | * @return DataObject $this |
||
522 | */ |
||
523 | public function setDataModel(DataModel $model) { |
||
524 | $this->model = $model; |
||
525 | return $this; |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Destroy all of this objects dependant objects and local caches. |
||
530 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
531 | */ |
||
532 | public function destroy() { |
||
533 | //$this->destroyed = true; |
||
534 | gc_collect_cycles(); |
||
535 | $this->flushCache(false); |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * Create a duplicate of this node. |
||
540 | * Note: now also duplicates relations. |
||
541 | * |
||
542 | * @param $doWrite Perform a write() operation before returning the object. If this is true, it will create the |
||
543 | * duplicate in the database. |
||
544 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
545 | */ |
||
546 | public function duplicate($doWrite = true) { |
||
547 | $className = $this->class; |
||
548 | $clone = new $className( $this->toMap(), false, $this->model ); |
||
549 | $clone->ID = 0; |
||
550 | |||
551 | $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite); |
||
552 | if($doWrite) { |
||
553 | $clone->write(); |
||
554 | $this->duplicateManyManyRelations($this, $clone); |
||
555 | } |
||
556 | $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite); |
||
557 | |||
558 | return $clone; |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
563 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
564 | * automatically when adding the new relations. |
||
565 | * |
||
566 | * @param $sourceObject the source object to duplicate from |
||
567 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
568 | * @return DataObject with the new many_many relations copied in |
||
569 | */ |
||
570 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
571 | if (!$destinationObject || $destinationObject->ID < 1) { |
||
572 | user_error("Can't duplicate relations for an object that has not been written to the database", |
||
573 | E_USER_ERROR); |
||
574 | } |
||
575 | |||
576 | //duplicate complex relations |
||
577 | // DO NOT copy has_many relations, because copying the relation would result in us changing the has_one |
||
578 | // relation on the other side of this relation to point at the copy and no longer the original (being a |
||
579 | // has_one, it can only point at one thing at a time). So, all relations except has_many can and are copied |
||
580 | if ($sourceObject->hasOne()) foreach($sourceObject->hasOne() as $name => $type) { |
||
581 | $this->duplicateRelations($sourceObject, $destinationObject, $name); |
||
582 | } |
||
583 | if ($sourceObject->manyMany()) foreach($sourceObject->manyMany() as $name => $type) { |
||
584 | //many_many include belongs_many_many |
||
585 | $this->duplicateRelations($sourceObject, $destinationObject, $name); |
||
586 | } |
||
587 | |||
588 | return $destinationObject; |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Helper function to duplicate relations from one object to another |
||
593 | * @param $sourceObject the source object to duplicate from |
||
594 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
595 | * @param $name the name of the relation to duplicate (e.g. members) |
||
596 | */ |
||
597 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
598 | $relations = $sourceObject->$name(); |
||
599 | if ($relations) { |
||
600 | if ($relations instanceOf RelationList) { //many-to-something relation |
||
601 | if ($relations->Count() > 0) { //with more than one thing it is related to |
||
602 | foreach($relations as $relation) { |
||
603 | $destinationObject->$name()->add($relation); |
||
604 | } |
||
605 | } |
||
606 | } else { //one-to-one relation |
||
607 | $destinationObject->{"{$name}ID"} = $relations->ID; |
||
608 | } |
||
609 | } |
||
610 | } |
||
611 | |||
612 | public function getObsoleteClassName() { |
||
613 | $className = $this->getField("ClassName"); |
||
614 | if (!ClassInfo::exists($className)) return $className; |
||
615 | } |
||
616 | |||
617 | public function getClassName() { |
||
618 | $className = $this->getField("ClassName"); |
||
619 | if (!ClassInfo::exists($className)) return get_class($this); |
||
620 | return $className; |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Set the ClassName attribute. {@link $class} is also updated. |
||
625 | * Warning: This will produce an inconsistent record, as the object |
||
626 | * instance will not automatically switch to the new subclass. |
||
627 | * Please use {@link newClassInstance()} for this purpose, |
||
628 | * or destroy and reinstanciate the record. |
||
629 | * |
||
630 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
631 | * @return DataObject $this |
||
632 | */ |
||
633 | public function setClassName($className) { |
||
634 | $className = trim($className); |
||
635 | if(!$className || !is_subclass_of($className, 'DataObject')) return; |
||
636 | |||
637 | $this->class = $className; |
||
638 | $this->setField("ClassName", $className); |
||
639 | return $this; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Create a new instance of a different class from this object's record. |
||
644 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
645 | * it ensures that the instance of the class is a match for the className of the |
||
646 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
647 | * property manually before calling this method, as it will confuse change detection. |
||
648 | * |
||
649 | * If the new class is different to the original class, defaults are populated again |
||
650 | * because this will only occur automatically on instantiation of a DataObject if |
||
651 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
652 | * we still need to repopulate the defaults. |
||
653 | * |
||
654 | * @param string $newClassName The name of the new class |
||
655 | * |
||
656 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
657 | */ |
||
658 | public function newClassInstance($newClassName) { |
||
659 | $originalClass = $this->ClassName; |
||
660 | $newInstance = new $newClassName(array_merge( |
||
661 | $this->record, |
||
662 | array( |
||
663 | 'ClassName' => $originalClass, |
||
664 | 'RecordClassName' => $originalClass, |
||
665 | ) |
||
666 | ), false, $this->model); |
||
667 | |||
668 | if($newClassName != $originalClass) { |
||
669 | $newInstance->setClassName($newClassName); |
||
670 | $newInstance->populateDefaults(); |
||
671 | $newInstance->forceChange(); |
||
672 | } |
||
673 | |||
674 | return $newInstance; |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * Adds methods from the extensions. |
||
679 | * Called by Object::__construct() once per class. |
||
680 | */ |
||
681 | public function defineMethods() { |
||
682 | parent::defineMethods(); |
||
683 | |||
684 | // Define the extra db fields - this is only necessary for extensions added in the |
||
685 | // class definition. Object::add_extension() will call this at definition time for |
||
686 | // those objects, which is a better mechanism. Perhaps extensions defined inside the |
||
687 | // class def can somehow be applied at definiton time also? |
||
688 | if($this->extension_instances) foreach($this->extension_instances as $i => $instance) { |
||
689 | if(!$instance->class) { |
||
690 | $class = get_class($instance); |
||
691 | user_error("DataObject::defineMethods(): Please ensure {$class}::__construct() calls" |
||
692 | . " parent::__construct()", E_USER_ERROR); |
||
693 | } |
||
694 | } |
||
695 | |||
696 | if($this->class == 'DataObject') return; |
||
697 | |||
698 | // Set up accessors for joined items |
||
699 | if($manyMany = $this->manyMany()) { |
||
700 | foreach($manyMany as $relationship => $class) { |
||
701 | $this->addWrapperMethod($relationship, 'getManyManyComponents'); |
||
702 | } |
||
703 | } |
||
704 | if($hasMany = $this->hasMany()) { |
||
705 | |||
706 | foreach($hasMany as $relationship => $class) { |
||
707 | $this->addWrapperMethod($relationship, 'getComponents'); |
||
708 | } |
||
709 | |||
710 | } |
||
711 | if($hasOne = $this->hasOne()) { |
||
712 | foreach($hasOne as $relationship => $class) { |
||
713 | $this->addWrapperMethod($relationship, 'getComponent'); |
||
714 | } |
||
715 | } |
||
716 | if($belongsTo = $this->belongsTo()) foreach(array_keys($belongsTo) as $relationship) { |
||
717 | $this->addWrapperMethod($relationship, 'getComponent'); |
||
718 | } |
||
719 | } |
||
720 | |||
721 | /** |
||
722 | * Returns true if this object "exists", i.e., has a sensible value. |
||
723 | * The default behaviour for a DataObject is to return true if |
||
724 | * the object exists in the database, you can override this in subclasses. |
||
725 | * |
||
726 | * @return boolean true if this object exists |
||
727 | */ |
||
728 | public function exists() { |
||
729 | return (isset($this->record['ID']) && $this->record['ID'] > 0); |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Returns TRUE if all values (other than "ID") are |
||
734 | * considered empty (by weak boolean comparison). |
||
735 | * Only checks for fields listed in {@link custom_database_fields()} |
||
736 | * |
||
737 | * @todo Use DBField->hasValue() |
||
738 | * |
||
739 | * @return boolean |
||
740 | */ |
||
741 | public function isEmpty(){ |
||
742 | $isEmpty = true; |
||
743 | $customFields = self::custom_database_fields(get_class($this)); |
||
744 | if($map = $this->toMap()){ |
||
745 | foreach($map as $k=>$v){ |
||
746 | // only look at custom fields |
||
747 | if(!array_key_exists($k, $customFields)) continue; |
||
748 | |||
749 | $dbObj = ($v instanceof DBField) ? $v : $this->dbObject($k); |
||
750 | $isEmpty = ($isEmpty && !$dbObj->exists()); |
||
751 | } |
||
752 | } |
||
753 | return $isEmpty; |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * Get the user friendly singular name of this DataObject. |
||
758 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
759 | * this returns the class name. |
||
760 | * |
||
761 | * @return string User friendly singular name of this DataObject |
||
762 | */ |
||
763 | public function singular_name() { |
||
764 | if(!$name = $this->stat('singular_name')) { |
||
765 | $name = ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $this->class)))); |
||
766 | } |
||
767 | |||
768 | return $name; |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Get the translated user friendly singular name of this DataObject |
||
773 | * same as singular_name() but runs it through the translating function |
||
774 | * |
||
775 | * Translating string is in the form: |
||
776 | * $this->class.SINGULARNAME |
||
777 | * Example: |
||
778 | * Page.SINGULARNAME |
||
779 | * |
||
780 | * @return string User friendly translated singular name of this DataObject |
||
781 | */ |
||
782 | public function i18n_singular_name() { |
||
783 | return _t($this->class.'.SINGULARNAME', $this->singular_name()); |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * Get the user friendly plural name of this DataObject |
||
788 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
789 | * this returns a pluralised version of the class name. |
||
790 | * |
||
791 | * @return string User friendly plural name of this DataObject |
||
792 | */ |
||
793 | public function plural_name() { |
||
794 | if($name = $this->stat('plural_name')) { |
||
795 | return $name; |
||
796 | } else { |
||
797 | $name = $this->singular_name(); |
||
798 | //if the penultimate character is not a vowel, replace "y" with "ies" |
||
799 | if (preg_match('/[^aeiou]y$/i', $name)) { |
||
800 | $name = substr($name,0,-1) . 'ie'; |
||
801 | } |
||
802 | return ucfirst($name . 's'); |
||
803 | } |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * Get the translated user friendly plural name of this DataObject |
||
808 | * Same as plural_name but runs it through the translation function |
||
809 | * Translation string is in the form: |
||
810 | * $this->class.PLURALNAME |
||
811 | * Example: |
||
812 | * Page.PLURALNAME |
||
813 | * |
||
814 | * @return string User friendly translated plural name of this DataObject |
||
815 | */ |
||
816 | public function i18n_plural_name() |
||
817 | { |
||
818 | $name = $this->plural_name(); |
||
819 | return _t($this->class.'.PLURALNAME', $name); |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Standard implementation of a title/label for a specific |
||
824 | * record. Tries to find properties 'Title' or 'Name', |
||
825 | * and falls back to the 'ID'. Useful to provide |
||
826 | * user-friendly identification of a record, e.g. in errormessages |
||
827 | * or UI-selections. |
||
828 | * |
||
829 | * Overload this method to have a more specialized implementation, |
||
830 | * e.g. for an Address record this could be: |
||
831 | * <code> |
||
832 | * function getTitle() { |
||
833 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
834 | * } |
||
835 | * </code> |
||
836 | * |
||
837 | * @return string |
||
838 | */ |
||
839 | public function getTitle() { |
||
840 | if($this->hasDatabaseField('Title')) return $this->getField('Title'); |
||
841 | if($this->hasDatabaseField('Name')) return $this->getField('Name'); |
||
842 | |||
843 | return "#{$this->ID}"; |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * Returns the associated database record - in this case, the object itself. |
||
848 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
849 | * |
||
850 | * @return DataObject Associated database record |
||
851 | */ |
||
852 | public function data() { |
||
853 | return $this; |
||
854 | } |
||
855 | |||
856 | /** |
||
857 | * Convert this object to a map. |
||
858 | * |
||
859 | * @return array The data as a map. |
||
860 | */ |
||
861 | public function toMap() { |
||
862 | $this->loadLazyFields(); |
||
863 | return $this->record; |
||
864 | } |
||
865 | |||
866 | /** |
||
867 | * Return all currently fetched database fields. |
||
868 | * |
||
869 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
870 | * Obviously, this makes it a lot faster. |
||
871 | * |
||
872 | * @return array The data as a map. |
||
873 | */ |
||
874 | public function getQueriedDatabaseFields() { |
||
875 | return $this->record; |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * Update a number of fields on this object, given a map of the desired changes. |
||
880 | * |
||
881 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
882 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
883 | * |
||
884 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
885 | * the related objects that it alters. |
||
886 | * |
||
887 | * @param array $data A map of field name to data values to update. |
||
888 | * @return DataObject $this |
||
889 | */ |
||
890 | public function update($data) { |
||
891 | foreach($data as $k => $v) { |
||
892 | // Implement dot syntax for updates |
||
893 | if(strpos($k,'.') !== false) { |
||
894 | $relations = explode('.', $k); |
||
895 | $fieldName = array_pop($relations); |
||
896 | $relObj = $this; |
||
897 | foreach($relations as $i=>$relation) { |
||
898 | // no support for has_many or many_many relationships, |
||
899 | // as the updater wouldn't know which object to write to (or create) |
||
900 | if($relObj->$relation() instanceof DataObject) { |
||
901 | $parentObj = $relObj; |
||
902 | $relObj = $relObj->$relation(); |
||
903 | // If the intermediate relationship objects have been created, then write them |
||
904 | if($i<sizeof($relation)-1 && !$relObj->ID || (!$relObj->ID && $parentObj != $this)) { |
||
905 | $relObj->write(); |
||
906 | $relatedFieldName = $relation."ID"; |
||
907 | $parentObj->$relatedFieldName = $relObj->ID; |
||
908 | $parentObj->write(); |
||
909 | } |
||
910 | } else { |
||
911 | user_error( |
||
912 | "DataObject::update(): Can't traverse relationship '$relation'," . |
||
913 | "it has to be a has_one relationship or return a single DataObject", |
||
914 | E_USER_NOTICE |
||
915 | ); |
||
916 | // unset relation object so we don't write properties to the wrong object |
||
917 | unset($relObj); |
||
918 | break; |
||
919 | } |
||
920 | } |
||
921 | |||
922 | if($relObj) { |
||
923 | $relObj->$fieldName = $v; |
||
924 | $relObj->write(); |
||
925 | $relatedFieldName = $relation."ID"; |
||
926 | $this->$relatedFieldName = $relObj->ID; |
||
927 | $relObj->flushCache(); |
||
928 | } else { |
||
929 | user_error("Couldn't follow dot syntax '$k' on '$this->class' object", E_USER_WARNING); |
||
930 | } |
||
931 | } else { |
||
932 | $this->$k = $v; |
||
933 | } |
||
934 | } |
||
935 | return $this; |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Pass changes as a map, and try to |
||
940 | * get automatic casting for these fields. |
||
941 | * Doesn't write to the database. To write the data, |
||
942 | * use the write() method. |
||
943 | * |
||
944 | * @param array $data A map of field name to data values to update. |
||
945 | * @return DataObject $this |
||
946 | */ |
||
947 | public function castedUpdate($data) { |
||
948 | foreach($data as $k => $v) { |
||
949 | $this->setCastedField($k,$v); |
||
950 | } |
||
951 | return $this; |
||
952 | } |
||
953 | |||
954 | /** |
||
955 | * Merges data and relations from another object of same class, |
||
956 | * without conflict resolution. Allows to specify which |
||
957 | * dataset takes priority in case its not empty. |
||
958 | * has_one-relations are just transferred with priority 'right'. |
||
959 | * has_many and many_many-relations are added regardless of priority. |
||
960 | * |
||
961 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
962 | * meaning they are not connected to the merged object any longer. |
||
963 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
964 | * doesn't write the updated object itself (just writes the object-properties). |
||
965 | * Caution: Does not delete the merged object. |
||
966 | * Caution: Does now overwrite Created date on the original object. |
||
967 | * |
||
968 | * @param $obj DataObject |
||
969 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
970 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
971 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
972 | * Only applicable with $priority='right'. (optional) |
||
973 | * @return Boolean |
||
974 | */ |
||
975 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
976 | $leftObj = $this; |
||
977 | |||
978 | if($leftObj->ClassName != $rightObj->ClassName) { |
||
979 | // we can't merge similiar subclasses because they might have additional relations |
||
980 | user_error("DataObject->merge(): Invalid object class '{$rightObj->ClassName}' |
||
981 | (expected '{$leftObj->ClassName}').", E_USER_WARNING); |
||
982 | return false; |
||
983 | } |
||
984 | |||
985 | if(!$rightObj->ID) { |
||
986 | user_error("DataObject->merge(): Please write your merged-in object to the database before merging, |
||
987 | to make sure all relations are transferred properly.').", E_USER_WARNING); |
||
988 | return false; |
||
989 | } |
||
990 | |||
991 | // makes sure we don't merge data like ID or ClassName |
||
992 | $leftData = $leftObj->inheritedDatabaseFields(); |
||
993 | $rightData = $rightObj->inheritedDatabaseFields(); |
||
994 | |||
995 | foreach($rightData as $key=>$rightVal) { |
||
996 | // don't merge conflicting values if priority is 'left' |
||
997 | if($priority == 'left' && $leftObj->{$key} !== $rightObj->{$key}) continue; |
||
998 | |||
999 | // don't overwrite existing left values with empty right values (if $overwriteWithEmpty is set) |
||
1000 | if($priority == 'right' && !$overwriteWithEmpty && empty($rightObj->{$key})) continue; |
||
1001 | |||
1002 | // TODO remove redundant merge of has_one fields |
||
1003 | $leftObj->{$key} = $rightObj->{$key}; |
||
1004 | } |
||
1005 | |||
1006 | // merge relations |
||
1007 | if($includeRelations) { |
||
1008 | if($manyMany = $this->manyMany()) { |
||
1009 | foreach($manyMany as $relationship => $class) { |
||
1010 | $leftComponents = $leftObj->getManyManyComponents($relationship); |
||
1011 | $rightComponents = $rightObj->getManyManyComponents($relationship); |
||
1012 | if($rightComponents && $rightComponents->exists()) { |
||
1013 | $leftComponents->addMany($rightComponents->column('ID')); |
||
1014 | } |
||
1015 | $leftComponents->write(); |
||
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | if($hasMany = $this->hasMany()) { |
||
1020 | foreach($hasMany as $relationship => $class) { |
||
1021 | $leftComponents = $leftObj->getComponents($relationship); |
||
1022 | $rightComponents = $rightObj->getComponents($relationship); |
||
1023 | if($rightComponents && $rightComponents->exists()) { |
||
1024 | $leftComponents->addMany($rightComponents->column('ID')); |
||
1025 | } |
||
1026 | $leftComponents->write(); |
||
1027 | } |
||
1028 | |||
1029 | } |
||
1030 | |||
1031 | if($hasOne = $this->hasOne()) { |
||
1032 | foreach($hasOne as $relationship => $class) { |
||
1033 | $leftComponent = $leftObj->getComponent($relationship); |
||
1034 | $rightComponent = $rightObj->getComponent($relationship); |
||
1035 | if($leftComponent->exists() && $rightComponent->exists() && $priority == 'right') { |
||
1036 | $leftObj->{$relationship . 'ID'} = $rightObj->{$relationship . 'ID'}; |
||
1037 | } |
||
1038 | } |
||
1039 | } |
||
1040 | } |
||
1041 | |||
1042 | return true; |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * Forces the record to think that all its data has changed. |
||
1047 | * Doesn't write to the database. Only sets fields as changed |
||
1048 | * if they are not already marked as changed. |
||
1049 | * |
||
1050 | * @return DataObject $this |
||
1051 | */ |
||
1052 | public function forceChange() { |
||
1053 | // Ensure lazy fields loaded |
||
1054 | $this->loadLazyFields(); |
||
1055 | |||
1056 | // $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well |
||
1057 | $fieldNames = array_unique(array_merge( |
||
1058 | array_keys($this->record), |
||
1059 | array_keys($this->inheritedDatabaseFields()))); |
||
1060 | |||
1061 | foreach($fieldNames as $fieldName) { |
||
1062 | if(!isset($this->changed[$fieldName])) $this->changed[$fieldName] = self::CHANGE_STRICT; |
||
1063 | // Populate the null values in record so that they actually get written |
||
1064 | if(!isset($this->record[$fieldName])) $this->record[$fieldName] = null; |
||
1065 | } |
||
1066 | |||
1067 | // @todo Find better way to allow versioned to write a new version after forceChange |
||
1068 | if($this->isChanged('Version')) unset($this->changed['Version']); |
||
1069 | return $this; |
||
1070 | } |
||
1071 | |||
1072 | /** |
||
1073 | * Validate the current object. |
||
1074 | * |
||
1075 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
1076 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
1077 | * |
||
1078 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
1079 | * and onAfterWrite() won't get called either. |
||
1080 | * |
||
1081 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
1082 | * attempting a write, and respond appropriately if it isn't. |
||
1083 | * |
||
1084 | * @see {@link ValidationResult} |
||
1085 | * @return ValidationResult |
||
1086 | */ |
||
1087 | protected function validate() { |
||
1088 | $result = ValidationResult::create(); |
||
1089 | $this->extend('validate', $result); |
||
1090 | return $result; |
||
1091 | } |
||
1092 | |||
1093 | /** |
||
1094 | * Public accessor for {@see DataObject::validate()} |
||
1095 | * |
||
1096 | * @return ValidationResult |
||
1097 | */ |
||
1098 | public function doValidate() { |
||
1099 | // validate will be public in 4.0 |
||
1100 | return $this->validate(); |
||
1101 | } |
||
1102 | |||
1103 | /** |
||
1104 | * Event handler called before writing to the database. |
||
1105 | * You can overload this to clean up or otherwise process data before writing it to the |
||
1106 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
1107 | * |
||
1108 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
1109 | * |
||
1110 | * @uses DataExtension->onBeforeWrite() |
||
1111 | */ |
||
1112 | protected function onBeforeWrite() { |
||
1113 | $this->brokenOnWrite = false; |
||
1114 | |||
1115 | $dummy = null; |
||
1116 | $this->extend('onBeforeWrite', $dummy); |
||
1117 | } |
||
1118 | |||
1119 | /** |
||
1120 | * Event handler called after writing to the database. |
||
1121 | * You can overload this to act upon changes made to the data after it is written. |
||
1122 | * $this->changed will have a record |
||
1123 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
1124 | * |
||
1125 | * @uses DataExtension->onAfterWrite() |
||
1126 | */ |
||
1127 | protected function onAfterWrite() { |
||
1128 | $dummy = null; |
||
1129 | $this->extend('onAfterWrite', $dummy); |
||
1130 | } |
||
1131 | |||
1132 | /** |
||
1133 | * Event handler called before deleting from the database. |
||
1134 | * You can overload this to clean up or otherwise process data before delete this |
||
1135 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
1136 | * |
||
1137 | * @uses DataExtension->onBeforeDelete() |
||
1138 | */ |
||
1139 | protected function onBeforeDelete() { |
||
1140 | $this->brokenOnDelete = false; |
||
1141 | |||
1142 | $dummy = null; |
||
1143 | $this->extend('onBeforeDelete', $dummy); |
||
1144 | } |
||
1145 | |||
1146 | protected function onAfterDelete() { |
||
1147 | $this->extend('onAfterDelete'); |
||
1148 | } |
||
1149 | |||
1150 | /** |
||
1151 | * Load the default values in from the self::$defaults array. |
||
1152 | * Will traverse the defaults of the current class and all its parent classes. |
||
1153 | * Called by the constructor when creating new records. |
||
1154 | * |
||
1155 | * @uses DataExtension->populateDefaults() |
||
1156 | * @return DataObject $this |
||
1157 | */ |
||
1158 | public function populateDefaults() { |
||
1159 | $classes = array_reverse(ClassInfo::ancestry($this)); |
||
1160 | |||
1161 | foreach($classes as $class) { |
||
1162 | $defaults = Config::inst()->get($class, 'defaults', Config::UNINHERITED); |
||
1163 | |||
1164 | if($defaults && !is_array($defaults)) { |
||
1165 | user_error("Bad '$this->class' defaults given: " . var_export($defaults, true), |
||
1166 | E_USER_WARNING); |
||
1167 | $defaults = null; |
||
1168 | } |
||
1169 | |||
1170 | if($defaults) foreach($defaults as $fieldName => $fieldValue) { |
||
1171 | // SRM 2007-03-06: Stricter check |
||
1172 | if(!isset($this->$fieldName) || $this->$fieldName === null) { |
||
1173 | $this->$fieldName = $fieldValue; |
||
1174 | } |
||
1175 | // Set many-many defaults with an array of ids |
||
1176 | if(is_array($fieldValue) && $this->manyManyComponent($fieldName)) { |
||
1177 | $manyManyJoin = $this->$fieldName(); |
||
1178 | $manyManyJoin->setByIdList($fieldValue); |
||
1179 | } |
||
1180 | } |
||
1181 | if($class == 'DataObject') { |
||
1182 | break; |
||
1183 | } |
||
1184 | } |
||
1185 | |||
1186 | $this->extend('populateDefaults'); |
||
1187 | return $this; |
||
1188 | } |
||
1189 | |||
1190 | /** |
||
1191 | * Determine validation of this object prior to write |
||
1192 | * |
||
1193 | * @return ValidationException Exception generated by this write, or null if valid |
||
1194 | */ |
||
1195 | protected function validateWrite() { |
||
1196 | if ($this->ObsoleteClassName) { |
||
1197 | return new ValidationException( |
||
1198 | "Object is of class '{$this->ObsoleteClassName}' which doesn't exist - ". |
||
1199 | "you need to change the ClassName before you can write it", |
||
1200 | E_USER_WARNING |
||
1201 | ); |
||
1202 | } |
||
1203 | |||
1204 | if(Config::inst()->get('DataObject', 'validation_enabled')) { |
||
1205 | $result = $this->validate(); |
||
1206 | if (!$result->valid()) { |
||
1207 | return new ValidationException( |
||
1208 | $result, |
||
1209 | $result->message(), |
||
1210 | E_USER_WARNING |
||
1211 | ); |
||
1212 | } |
||
1213 | } |
||
1214 | } |
||
1215 | |||
1216 | /** |
||
1217 | * Prepare an object prior to write |
||
1218 | * |
||
1219 | * @throws ValidationException |
||
1220 | */ |
||
1221 | protected function preWrite() { |
||
1222 | // Validate this object |
||
1223 | if($writeException = $this->validateWrite()) { |
||
1224 | // Used by DODs to clean up after themselves, eg, Versioned |
||
1225 | $this->invokeWithExtensions('onAfterSkippedWrite'); |
||
1226 | throw $writeException; |
||
1227 | } |
||
1228 | |||
1229 | // Check onBeforeWrite |
||
1230 | $this->brokenOnWrite = true; |
||
1231 | $this->onBeforeWrite(); |
||
1232 | if($this->brokenOnWrite) { |
||
1233 | user_error("$this->class has a broken onBeforeWrite() function." |
||
1234 | . " Make sure that you call parent::onBeforeWrite().", E_USER_ERROR); |
||
1235 | } |
||
1236 | } |
||
1237 | |||
1238 | /** |
||
1239 | * Detects and updates all changes made to this object |
||
1240 | * |
||
1241 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
1242 | * @return bool True if any changes are detected |
||
1243 | */ |
||
1244 | protected function updateChanges($forceChanges = false) { |
||
1245 | // Update the changed array with references to changed obj-fields |
||
1246 | foreach($this->record as $field => $value) { |
||
1247 | // Only mark ID as changed if $forceChanges |
||
1248 | if($field === 'ID' && !$forceChanges) continue; |
||
1249 | // Determine if this field should be forced, or can mark itself, changed |
||
1250 | if($forceChanges |
||
1251 | || !$this->isInDB() |
||
1252 | || (is_object($value) && method_exists($value, 'isChanged') && $value->isChanged()) |
||
1253 | ) { |
||
1254 | $this->changed[$field] = self::CHANGE_VALUE; |
||
1255 | } |
||
1256 | } |
||
1257 | |||
1258 | // Check changes exist, abort if there are no changes |
||
1259 | return $this->changed && (bool)array_filter($this->changed); |
||
1260 | } |
||
1261 | |||
1262 | /** |
||
1263 | * Writes a subset of changes for a specific table to the given manipulation |
||
1264 | * |
||
1265 | * @param string $baseTable Base table |
||
1266 | * @param string $now Timestamp to use for the current time |
||
1267 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
1268 | * @param array $manipulation Manipulation to write to |
||
1269 | * @param string $class Table and Class to select and write to |
||
1270 | */ |
||
1271 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
1272 | $manipulation[$class] = array(); |
||
1273 | |||
1274 | // Extract records for this table |
||
1275 | foreach($this->record as $fieldName => $fieldValue) { |
||
1276 | |||
1277 | // Check if this record pertains to this table, and |
||
1278 | // we're not attempting to reset the BaseTable->ID |
||
1279 | if( empty($this->changed[$fieldName]) |
||
1280 | || ($class === $baseTable && $fieldName === 'ID') |
||
1281 | || (!self::has_own_table_database_field($class, $fieldName) |
||
1282 | && !self::is_composite_field($class, $fieldName, false)) |
||
1283 | ) { |
||
1284 | continue; |
||
1285 | } |
||
1286 | |||
1287 | |||
1288 | // if database column doesn't correlate to a DBField instance... |
||
1289 | $fieldObj = $this->dbObject($fieldName); |
||
1290 | if(!$fieldObj) { |
||
1291 | $fieldObj = DBField::create_field('Varchar', $fieldValue, $fieldName); |
||
1292 | } |
||
1293 | |||
1294 | // Ensure DBField is repopulated and written to the manipulation |
||
1295 | $fieldObj->setValue($fieldValue, $this->record); |
||
1296 | $fieldObj->writeToManipulation($manipulation[$class]); |
||
1297 | } |
||
1298 | |||
1299 | // Ensure update of Created and LastEdited columns |
||
1300 | if($baseTable === $class) { |
||
1301 | $manipulation[$class]['fields']['LastEdited'] = $now; |
||
1302 | if($isNewRecord) { |
||
1303 | $manipulation[$class]['fields']['Created'] |
||
1304 | = empty($this->record['Created']) |
||
1305 | ? $now |
||
1306 | : $this->record['Created']; |
||
1307 | $manipulation[$class]['fields']['ClassName'] = $this->class; |
||
1308 | } |
||
1309 | } |
||
1310 | |||
1311 | // Inserts done one the base table are performed in another step, so the manipulation should instead |
||
1312 | // attempt an update, as though it were a normal update. |
||
1313 | $manipulation[$class]['command'] = $isNewRecord ? 'insert' : 'update'; |
||
1314 | $manipulation[$class]['id'] = $this->record['ID']; |
||
1315 | } |
||
1316 | |||
1317 | /** |
||
1318 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
1319 | * |
||
1320 | * Does nothing if an ID is already assigned for this record |
||
1321 | * |
||
1322 | * @param string $baseTable Base table |
||
1323 | * @param string $now Timestamp to use for the current time |
||
1324 | */ |
||
1325 | protected function writeBaseRecord($baseTable, $now) { |
||
1326 | // Generate new ID if not specified |
||
1327 | if($this->isInDB()) return; |
||
1328 | |||
1329 | // Perform an insert on the base table |
||
1330 | $insert = new SQLInsert('"'.$baseTable.'"'); |
||
1331 | $insert |
||
1332 | ->assign('"Created"', $now) |
||
1333 | ->execute(); |
||
1334 | $this->changed['ID'] = self::CHANGE_VALUE; |
||
1335 | $this->record['ID'] = DB::get_generated_id($baseTable); |
||
1336 | } |
||
1337 | |||
1338 | /** |
||
1339 | * Generate and write the database manipulation for all changed fields |
||
1340 | * |
||
1341 | * @param string $baseTable Base table |
||
1342 | * @param string $now Timestamp to use for the current time |
||
1343 | * @param bool $isNewRecord If this is a new record |
||
1344 | */ |
||
1345 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
1346 | // Generate database manipulations for each class |
||
1347 | $manipulation = array(); |
||
1348 | foreach($this->getClassAncestry() as $class) { |
||
1349 | if(self::has_own_table($class)) { |
||
1350 | $this->prepareManipulationTable($baseTable, $now, $isNewRecord, $manipulation, $class); |
||
1351 | } |
||
1352 | } |
||
1353 | |||
1354 | // Allow extensions to extend this manipulation |
||
1355 | $this->extend('augmentWrite', $manipulation); |
||
1356 | |||
1357 | // New records have their insert into the base data table done first, so that they can pass the |
||
1358 | // generated ID on to the rest of the manipulation |
||
1359 | if($isNewRecord) { |
||
1360 | $manipulation[$baseTable]['command'] = 'update'; |
||
1361 | } |
||
1362 | |||
1363 | // Perform the manipulation |
||
1364 | DB::manipulate($manipulation); |
||
1365 | } |
||
1366 | |||
1367 | /** |
||
1368 | * Writes all changes to this object to the database. |
||
1369 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
1370 | * - All relevant tables will be updated. |
||
1371 | * - $this->onBeforeWrite() gets called beforehand. |
||
1372 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
1373 | * |
||
1374 | * @uses DataExtension->augmentWrite() |
||
1375 | * |
||
1376 | * @param boolean $showDebug Show debugging information |
||
1377 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
1378 | * @param boolean $forceWrite Write to database even if there are no changes |
||
1379 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
1380 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
1381 | * {@link getManyManyComponents()} (Default: false) |
||
1382 | * @return int The ID of the record |
||
1383 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
1384 | */ |
||
1385 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
1386 | $now = SS_Datetime::now()->Rfc2822(); |
||
1387 | |||
1388 | // Execute pre-write tasks |
||
1389 | $this->preWrite(); |
||
1390 | |||
1391 | // Check if we are doing an update or an insert |
||
1392 | $isNewRecord = !$this->isInDB() || $forceInsert; |
||
1393 | |||
1394 | // Check changes exist, abort if there are none |
||
1395 | $hasChanges = $this->updateChanges($forceInsert); |
||
1396 | if($hasChanges || $forceWrite || $isNewRecord) { |
||
1397 | // New records have their insert into the base data table done first, so that they can pass the |
||
1398 | // generated primary key on to the rest of the manipulation |
||
1399 | $baseTable = ClassInfo::baseDataClass($this->class); |
||
1400 | $this->writeBaseRecord($baseTable, $now); |
||
1401 | |||
1402 | // Write the DB manipulation for all changed fields |
||
1403 | $this->writeManipulation($baseTable, $now, $isNewRecord); |
||
1404 | |||
1405 | // If there's any relations that couldn't be saved before, save them now (we have an ID here) |
||
1406 | $this->writeRelations(); |
||
1407 | $this->onAfterWrite(); |
||
1408 | $this->changed = array(); |
||
1409 | } else { |
||
1410 | if($showDebug) Debug::message("no changes for DataObject"); |
||
1411 | |||
1412 | // Used by DODs to clean up after themselves, eg, Versioned |
||
1413 | $this->invokeWithExtensions('onAfterSkippedWrite'); |
||
1414 | } |
||
1415 | |||
1416 | // Ensure Created and LastEdited are populated |
||
1417 | if(!isset($this->record['Created'])) { |
||
1418 | $this->record['Created'] = $now; |
||
1419 | } |
||
1420 | $this->record['LastEdited'] = $now; |
||
1421 | |||
1422 | // Write relations as necessary |
||
1423 | if($writeComponents) $this->writeComponents(true); |
||
1424 | |||
1425 | // Clears the cache for this object so get_one returns the correct object. |
||
1426 | $this->flushCache(); |
||
1427 | |||
1428 | return $this->record['ID']; |
||
1429 | } |
||
1430 | |||
1431 | /** |
||
1432 | * Writes cached relation lists to the database, if possible |
||
1433 | */ |
||
1434 | public function writeRelations() { |
||
1435 | if(!$this->isInDB()) return; |
||
1436 | |||
1437 | // If there's any relations that couldn't be saved before, save them now (we have an ID here) |
||
1438 | if($this->unsavedRelations) { |
||
1439 | foreach($this->unsavedRelations as $name => $list) { |
||
1440 | $list->changeToList($this->$name()); |
||
1441 | } |
||
1442 | $this->unsavedRelations = array(); |
||
1443 | } |
||
1444 | } |
||
1445 | |||
1446 | /** |
||
1447 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
1448 | * same record. |
||
1449 | * |
||
1450 | * @param $recursive Recursively write components |
||
1451 | * @return DataObject $this |
||
1452 | */ |
||
1453 | public function writeComponents($recursive = false) { |
||
1454 | if(!$this->components) return $this; |
||
1455 | |||
1456 | foreach($this->components as $component) { |
||
1457 | $component->write(false, false, false, $recursive); |
||
1458 | } |
||
1459 | return $this; |
||
1460 | } |
||
1461 | |||
1462 | /** |
||
1463 | * Delete this data object. |
||
1464 | * $this->onBeforeDelete() gets called. |
||
1465 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
1466 | * @uses DataExtension->augmentSQL() |
||
1467 | */ |
||
1468 | public function delete() { |
||
1469 | $this->brokenOnDelete = true; |
||
1470 | $this->onBeforeDelete(); |
||
1471 | if($this->brokenOnDelete) { |
||
1472 | user_error("$this->class has a broken onBeforeDelete() function." |
||
1473 | . " Make sure that you call parent::onBeforeDelete().", E_USER_ERROR); |
||
1474 | } |
||
1475 | |||
1476 | // Deleting a record without an ID shouldn't do anything |
||
1477 | if(!$this->ID) throw new LogicException("DataObject::delete() called on a DataObject without an ID"); |
||
1478 | |||
1479 | // TODO: This is quite ugly. To improve: |
||
1480 | // - move the details of the delete code in the DataQuery system |
||
1481 | // - update the code to just delete the base table, and rely on cascading deletes in the DB to do the rest |
||
1482 | // obviously, that means getting requireTable() to configure cascading deletes ;-) |
||
1483 | $srcQuery = DataList::create($this->class, $this->model)->where("ID = $this->ID")->dataQuery()->query(); |
||
1484 | foreach($srcQuery->queriedTables() as $table) { |
||
1485 | $delete = new SQLDelete("\"$table\"", array('"ID"' => $this->ID)); |
||
1486 | $delete->execute(); |
||
1487 | } |
||
1488 | // Remove this item out of any caches |
||
1489 | $this->flushCache(); |
||
1490 | |||
1491 | $this->onAfterDelete(); |
||
1492 | |||
1493 | $this->OldID = $this->ID; |
||
1494 | $this->ID = 0; |
||
1495 | } |
||
1496 | |||
1497 | /** |
||
1498 | * Delete the record with the given ID. |
||
1499 | * |
||
1500 | * @param string $className The class name of the record to be deleted |
||
1501 | * @param int $id ID of record to be deleted |
||
1502 | */ |
||
1503 | public static function delete_by_id($className, $id) { |
||
1504 | $obj = DataObject::get_by_id($className, $id); |
||
1505 | if($obj) { |
||
1506 | $obj->delete(); |
||
1507 | } else { |
||
1508 | user_error("$className object #$id wasn't found when calling DataObject::delete_by_id", E_USER_WARNING); |
||
1509 | } |
||
1510 | } |
||
1511 | |||
1512 | /** |
||
1513 | * Get the class ancestry, including the current class name. |
||
1514 | * The ancestry will be returned as an array of class names, where the 0th element |
||
1515 | * will be the class that inherits directly from DataObject, and the last element |
||
1516 | * will be the current class. |
||
1517 | * |
||
1518 | * @return array Class ancestry |
||
1519 | */ |
||
1520 | public function getClassAncestry() { |
||
1521 | if(!isset(DataObject::$_cache_get_class_ancestry[$this->class])) { |
||
1522 | DataObject::$_cache_get_class_ancestry[$this->class] = array($this->class); |
||
1523 | while(($class=get_parent_class(DataObject::$_cache_get_class_ancestry[$this->class][0])) != "DataObject") { |
||
1524 | array_unshift(DataObject::$_cache_get_class_ancestry[$this->class], $class); |
||
1525 | } |
||
1526 | } |
||
1527 | return DataObject::$_cache_get_class_ancestry[$this->class]; |
||
1528 | } |
||
1529 | |||
1530 | /** |
||
1531 | * Return a component object from a one to one relationship, as a DataObject. |
||
1532 | * If no component is available, an 'empty component' will be returned for |
||
1533 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
1534 | * |
||
1535 | * @param string $componentName Name of the component |
||
1536 | * |
||
1537 | * @return DataObject The component object. It's exact type will be that of the component. |
||
1538 | */ |
||
1539 | public function getComponent($componentName) { |
||
1540 | if(isset($this->components[$componentName])) { |
||
1541 | return $this->components[$componentName]; |
||
1542 | } |
||
1543 | |||
1544 | if($class = $this->hasOneComponent($componentName)) { |
||
1545 | $joinField = $componentName . 'ID'; |
||
1546 | $joinID = $this->getField($joinField); |
||
1547 | |||
1548 | // Extract class name for polymorphic relations |
||
1549 | if($class === 'DataObject') { |
||
1550 | $class = $this->getField($componentName . 'Class'); |
||
1551 | if(empty($class)) return null; |
||
1552 | } |
||
1553 | |||
1554 | if($joinID) { |
||
1555 | $component = DataObject::get_by_id($class, $joinID); |
||
1556 | } |
||
1557 | |||
1558 | if(empty($component)) { |
||
1559 | $component = $this->model->$class->newObject(); |
||
1560 | } |
||
1561 | } elseif($class = $this->belongsToComponent($componentName)) { |
||
1562 | |||
1563 | $joinField = $this->getRemoteJoinField($componentName, 'belongs_to', $polymorphic); |
||
1564 | $joinID = $this->ID; |
||
1565 | |||
1566 | if($joinID) { |
||
1567 | |||
1568 | $filter = $polymorphic |
||
1569 | ? array( |
||
1570 | "{$joinField}ID" => $joinID, |
||
1571 | "{$joinField}Class" => $this->class |
||
1572 | ) |
||
1573 | : array( |
||
1574 | $joinField => $joinID |
||
1575 | ); |
||
1576 | $component = DataObject::get($class)->filter($filter)->first(); |
||
1577 | } |
||
1578 | |||
1579 | if(empty($component)) { |
||
1580 | $component = $this->model->$class->newObject(); |
||
1581 | if($polymorphic) { |
||
1582 | $component->{$joinField.'ID'} = $this->ID; |
||
1583 | $component->{$joinField.'Class'} = $this->class; |
||
1584 | } else { |
||
1585 | $component->$joinField = $this->ID; |
||
1586 | } |
||
1587 | } |
||
1588 | } else { |
||
1589 | throw new Exception("DataObject->getComponent(): Could not find component '$componentName'."); |
||
1590 | } |
||
1591 | |||
1592 | $this->components[$componentName] = $component; |
||
1593 | return $component; |
||
1594 | } |
||
1595 | |||
1596 | /** |
||
1597 | * Returns a one-to-many relation as a HasManyList |
||
1598 | * |
||
1599 | * @param string $componentName Name of the component |
||
1600 | * @param string|null $filter Deprecated. A filter to be inserted into the WHERE clause |
||
1601 | * @param string|null|array $sort Deprecated. A sort expression to be inserted into the ORDER BY clause. If omitted, |
||
1602 | * the static field $default_sort on the component class will be used. |
||
1603 | * @param string $join Deprecated, use leftJoin($table, $joinClause) instead |
||
1604 | * @param string|null|array $limit Deprecated. A limit expression to be inserted into the LIMIT clause |
||
1605 | * |
||
1606 | * @return HasManyList The components of the one-to-many relationship. |
||
1607 | */ |
||
1608 | public function getComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
1609 | $result = null; |
||
1610 | |||
1611 | if(!$componentClass = $this->hasManyComponent($componentName)) { |
||
1612 | user_error("DataObject::getComponents(): Unknown 1-to-many component '$componentName'" |
||
1613 | . " on class '$this->class'", E_USER_ERROR); |
||
1614 | } |
||
1615 | |||
1616 | if($join) { |
||
1617 | throw new \InvalidArgumentException( |
||
1618 | 'The $join argument has been removed. Use leftJoin($table, $joinClause) instead.' |
||
1619 | ); |
||
1620 | } |
||
1621 | |||
1622 | if($filter !== null || $sort !== null || $limit !== null) { |
||
1623 | Deprecation::notice('4.0', 'The $filter, $sort and $limit parameters for DataObject::getComponents() |
||
1624 | have been deprecated. Please manipluate the returned list directly.', Deprecation::SCOPE_GLOBAL); |
||
1625 | } |
||
1626 | |||
1627 | // If we haven't been written yet, we can't save these relations, so use a list that handles this case |
||
1628 | if(!$this->ID) { |
||
1629 | if(!isset($this->unsavedRelations[$componentName])) { |
||
1630 | $this->unsavedRelations[$componentName] = |
||
1631 | new UnsavedRelationList($this->class, $componentName, $componentClass); |
||
1632 | } |
||
1633 | return $this->unsavedRelations[$componentName]; |
||
1634 | } |
||
1635 | |||
1636 | // Determine type and nature of foreign relation |
||
1637 | $joinField = $this->getRemoteJoinField($componentName, 'has_many', $polymorphic); |
||
1638 | if($polymorphic) { |
||
1639 | $result = PolymorphicHasManyList::create($componentClass, $joinField, $this->class); |
||
1640 | } else { |
||
1641 | $result = HasManyList::create($componentClass, $joinField); |
||
1642 | } |
||
1643 | |||
1644 | if($this->model) $result->setDataModel($this->model); |
||
1645 | |||
1646 | return $result |
||
1647 | ->forForeignID($this->ID) |
||
1648 | ->where($filter) |
||
1649 | ->limit($limit) |
||
1650 | ->sort($sort); |
||
1651 | } |
||
1652 | |||
1653 | /** |
||
1654 | * @deprecated |
||
1655 | */ |
||
1656 | public function getComponentsQuery($componentName, $filter = "", $sort = "", $join = "", $limit = "") { |
||
1657 | Deprecation::notice('4.0', "Use getComponents to get a filtered DataList for an object's relation"); |
||
1658 | return $this->getComponents($componentName, $filter, $sort, $join, $limit); |
||
1659 | } |
||
1660 | |||
1661 | /** |
||
1662 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
1663 | * |
||
1664 | * @param $relationName Relation name. |
||
1665 | * @return string Class name, or null if not found. |
||
1666 | */ |
||
1667 | public function getRelationClass($relationName) { |
||
1668 | // Go through all relationship configuration fields. |
||
1669 | $candidates = array_merge( |
||
1670 | ($relations = Config::inst()->get($this->class, 'has_one')) ? $relations : array(), |
||
1671 | ($relations = Config::inst()->get($this->class, 'has_many')) ? $relations : array(), |
||
1672 | ($relations = Config::inst()->get($this->class, 'many_many')) ? $relations : array(), |
||
1673 | ($relations = Config::inst()->get($this->class, 'belongs_many_many')) ? $relations : array(), |
||
1674 | ($relations = Config::inst()->get($this->class, 'belongs_to')) ? $relations : array() |
||
1675 | ); |
||
1676 | |||
1677 | if (isset($candidates[$relationName])) { |
||
1678 | $remoteClass = $candidates[$relationName]; |
||
1679 | |||
1680 | // If dot notation is present, extract just the first part that contains the class. |
||
1681 | if(($fieldPos = strpos($remoteClass, '.'))!==false) { |
||
1682 | return substr($remoteClass, 0, $fieldPos); |
||
1683 | } |
||
1684 | |||
1685 | // Otherwise just return the class |
||
1686 | return $remoteClass; |
||
1687 | } |
||
1688 | |||
1689 | return null; |
||
1690 | } |
||
1691 | |||
1692 | /** |
||
1693 | * Tries to find the database key on another object that is used to store a |
||
1694 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
1695 | * |
||
1696 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
1697 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
1698 | * |
||
1699 | * @param string $component Name of the relation on the current object pointing to the |
||
1700 | * remote object. |
||
1701 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
1702 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
1703 | * @return string |
||
1704 | */ |
||
1705 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
1706 | // Extract relation from current object |
||
1707 | if($type === 'has_many') { |
||
1708 | $remoteClass = $this->hasManyComponent($component, false); |
||
1709 | } else { |
||
1710 | $remoteClass = $this->belongsToComponent($component, false); |
||
1711 | } |
||
1712 | |||
1713 | if(empty($remoteClass)) { |
||
1714 | throw new Exception("Unknown $type component '$component' on class '$this->class'"); |
||
1715 | } |
||
1716 | if(!ClassInfo::exists(strtok($remoteClass, '.'))) { |
||
1717 | throw new Exception( |
||
1718 | "Class '$remoteClass' not found, but used in $type component '$component' on class '$this->class'" |
||
1719 | ); |
||
1720 | } |
||
1721 | |||
1722 | // If presented with an explicit field name (using dot notation) then extract field name |
||
1723 | $remoteField = null; |
||
1724 | if(strpos($remoteClass, '.') !== false) { |
||
1725 | list($remoteClass, $remoteField) = explode('.', $remoteClass); |
||
1726 | } |
||
1727 | |||
1728 | // Reference remote has_one to check against |
||
1729 | $remoteRelations = Config::inst()->get($remoteClass, 'has_one'); |
||
1730 | |||
1731 | // Without an explicit field name, attempt to match the first remote field |
||
1732 | // with the same type as the current class |
||
1733 | if(empty($remoteField)) { |
||
1734 | // look for remote has_one joins on this class or any parent classes |
||
1735 | $remoteRelationsMap = array_flip($remoteRelations); |
||
1736 | foreach(array_reverse(ClassInfo::ancestry($this)) as $class) { |
||
1737 | if(array_key_exists($class, $remoteRelationsMap)) { |
||
1738 | $remoteField = $remoteRelationsMap[$class]; |
||
1739 | break; |
||
1740 | } |
||
1741 | } |
||
1742 | } |
||
1743 | |||
1744 | // In case of an indeterminate remote field show an error |
||
1745 | if(empty($remoteField)) { |
||
1746 | $polymorphic = false; |
||
1747 | $message = "No has_one found on class '$remoteClass'"; |
||
1748 | if($type == 'has_many') { |
||
1749 | // include a hint for has_many that is missing a has_one |
||
1750 | $message .= ", the has_many relation from '$this->class' to '$remoteClass'"; |
||
1751 | $message .= " requires a has_one on '$remoteClass'"; |
||
1752 | } |
||
1753 | throw new Exception($message); |
||
1754 | } |
||
1755 | |||
1756 | // If given an explicit field name ensure the related class specifies this |
||
1757 | if(empty($remoteRelations[$remoteField])) { |
||
1758 | throw new Exception("Missing expected has_one named '$remoteField' |
||
1759 | on class '$remoteClass' referenced by $type named '$component' |
||
1760 | on class {$this->class}" |
||
1761 | ); |
||
1762 | } |
||
1763 | |||
1764 | // Inspect resulting found relation |
||
1765 | if($remoteRelations[$remoteField] === 'DataObject') { |
||
1766 | $polymorphic = true; |
||
1767 | return $remoteField; // Composite polymorphic field does not include 'ID' suffix |
||
1768 | } else { |
||
1769 | $polymorphic = false; |
||
1770 | return $remoteField . 'ID'; |
||
1771 | } |
||
1772 | } |
||
1773 | |||
1774 | /** |
||
1775 | * Returns a many-to-many component, as a ManyManyList. |
||
1776 | * @param string $componentName Name of the many-many component |
||
1777 | * @return ManyManyList The set of components |
||
1778 | * |
||
1779 | * @todo Implement query-params |
||
1780 | */ |
||
1781 | public function getManyManyComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { |
||
1782 | list($parentClass, $componentClass, $parentField, $componentField, $table) |
||
1783 | = $this->manyManyComponent($componentName); |
||
1784 | |||
1785 | if($filter !== null || $sort !== null || $join !== null || $limit !== null) { |
||
1786 | Deprecation::notice('4.0', 'The $filter, $sort, $join and $limit parameters for |
||
1787 | DataObject::getManyManyComponents() have been deprecated. |
||
1788 | Please manipluate the returned list directly.', Deprecation::SCOPE_GLOBAL); |
||
1789 | } |
||
1790 | |||
1791 | // If we haven't been written yet, we can't save these relations, so use a list that handles this case |
||
1792 | if(!$this->ID) { |
||
1793 | if(!isset($this->unsavedRelations[$componentName])) { |
||
1794 | $this->unsavedRelations[$componentName] = |
||
1795 | new UnsavedRelationList($parentClass, $componentName, $componentClass); |
||
1796 | } |
||
1797 | return $this->unsavedRelations[$componentName]; |
||
1798 | } |
||
1799 | |||
1800 | $extraFields = $this->manyManyExtraFieldsForComponent($componentName) ?: array(); |
||
1801 | $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields); |
||
1802 | |||
1803 | if($this->model) $result->setDataModel($this->model); |
||
1804 | |||
1805 | $this->extend('updateManyManyComponents', $result); |
||
1806 | |||
1807 | // If this is called on a singleton, then we return an 'orphaned relation' that can have the |
||
1808 | // foreignID set elsewhere. |
||
1809 | return $result |
||
1810 | ->forForeignID($this->ID) |
||
1811 | ->where($filter) |
||
1812 | ->sort($sort) |
||
1813 | ->limit($limit); |
||
1814 | } |
||
1815 | |||
1816 | /** |
||
1817 | * @deprecated 4.0 Method has been replaced by hasOne() and hasOneComponent() |
||
1818 | * @param string $component |
||
1819 | * @return array|null |
||
1820 | */ |
||
1821 | public function has_one($component = null) { |
||
1822 | if($component) { |
||
1823 | Deprecation::notice('4.0', 'Please use hasOneComponent() instead'); |
||
1824 | return $this->hasOneComponent($component); |
||
1825 | } |
||
1826 | |||
1827 | Deprecation::notice('4.0', 'Please use hasOne() instead'); |
||
1828 | return $this->hasOne(); |
||
1829 | } |
||
1830 | |||
1831 | /** |
||
1832 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
1833 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
1834 | * |
||
1835 | * @param string $component Deprecated - Name of component |
||
1836 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
1837 | * their classes. |
||
1838 | */ |
||
1839 | public function hasOne($component = null) { |
||
1840 | if($component) { |
||
1841 | Deprecation::notice( |
||
1842 | '4.0', |
||
1843 | 'Please use DataObject::hasOneComponent() instead of passing a component name to hasOne()', |
||
1844 | Deprecation::SCOPE_GLOBAL |
||
1845 | ); |
||
1846 | return $this->hasOneComponent($component); |
||
1847 | } |
||
1848 | |||
1849 | return (array)Config::inst()->get($this->class, 'has_one', Config::INHERITED); |
||
1850 | } |
||
1851 | |||
1852 | /** |
||
1853 | * Return data for a specific has_one component. |
||
1854 | * @param string $component |
||
1855 | * @return string|null |
||
1856 | */ |
||
1857 | public function hasOneComponent($component) { |
||
1858 | $hasOnes = (array)Config::inst()->get($this->class, 'has_one', Config::INHERITED); |
||
1859 | |||
1860 | if(isset($hasOnes[$component])) { |
||
1861 | return $hasOnes[$component]; |
||
1862 | } |
||
1863 | } |
||
1864 | |||
1865 | /** |
||
1866 | * @deprecated 4.0 Method has been replaced by belongsTo() and belongsToComponent() |
||
1867 | * @param string $component |
||
1868 | * @param bool $classOnly |
||
1869 | * @return array|null |
||
1870 | */ |
||
1871 | public function belongs_to($component = null, $classOnly = true) { |
||
1872 | if($component) { |
||
1873 | Deprecation::notice('4.0', 'Please use belongsToComponent() instead'); |
||
1874 | return $this->belongsToComponent($component, $classOnly); |
||
1875 | } |
||
1876 | |||
1877 | Deprecation::notice('4.0', 'Please use belongsTo() instead'); |
||
1878 | return $this->belongsTo(null, $classOnly); |
||
1879 | } |
||
1880 | |||
1881 | /** |
||
1882 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
1883 | * their class name will be returned. |
||
1884 | * |
||
1885 | * @param string $component - Name of component |
||
1886 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1887 | * the field data stripped off. It defaults to TRUE. |
||
1888 | * @return string|array |
||
1889 | */ |
||
1890 | public function belongsTo($component = null, $classOnly = true) { |
||
1891 | if($component) { |
||
1892 | Deprecation::notice( |
||
1893 | '4.0', |
||
1894 | 'Please use DataObject::belongsToComponent() instead of passing a component name to belongsTo()', |
||
1895 | Deprecation::SCOPE_GLOBAL |
||
1896 | ); |
||
1897 | return $this->belongsToComponent($component, $classOnly); |
||
1898 | } |
||
1899 | |||
1900 | $belongsTo = (array)Config::inst()->get($this->class, 'belongs_to', Config::INHERITED); |
||
1901 | if($belongsTo && $classOnly) { |
||
1902 | return preg_replace('/(.+)?\..+/', '$1', $belongsTo); |
||
1903 | } else { |
||
1904 | return $belongsTo ? $belongsTo : array(); |
||
1905 | } |
||
1906 | } |
||
1907 | |||
1908 | /** |
||
1909 | * Return data for a specific belongs_to component. |
||
1910 | * @param string $component |
||
1911 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1912 | * the field data stripped off. It defaults to TRUE. |
||
1913 | * @return string|false |
||
1914 | */ |
||
1915 | public function belongsToComponent($component, $classOnly = true) { |
||
1916 | $belongsTo = (array)Config::inst()->get($this->class, 'belongs_to', Config::INHERITED); |
||
1917 | |||
1918 | if($belongsTo && array_key_exists($component, $belongsTo)) { |
||
1919 | $belongsTo = $belongsTo[$component]; |
||
1920 | } else { |
||
1921 | return false; |
||
1922 | } |
||
1923 | |||
1924 | return ($classOnly) ? preg_replace('/(.+)?\..+/', '$1', $belongsTo) : $belongsTo; |
||
1925 | } |
||
1926 | |||
1927 | /** |
||
1928 | * Return all of the database fields defined in self::$db and all the parent classes. |
||
1929 | * Doesn't include any fields specified by self::$has_one. Use $this->hasOne() to get these fields |
||
1930 | * |
||
1931 | * @param string $fieldName Limit the output to a specific field name |
||
1932 | * @return array The database fields |
||
1933 | */ |
||
1934 | public function db($fieldName = null) { |
||
1935 | $classes = ClassInfo::ancestry($this, true); |
||
1936 | |||
1937 | // If we're looking for a specific field, we want to hit subclasses first as they may override field types |
||
1938 | if($fieldName) { |
||
1939 | $classes = array_reverse($classes); |
||
1940 | } |
||
1941 | |||
1942 | $items = array(); |
||
1943 | foreach($classes as $class) { |
||
1944 | if(isset(self::$_cache_db[$class])) { |
||
1945 | $dbItems = self::$_cache_db[$class]; |
||
1946 | } else { |
||
1947 | $dbItems = (array) Config::inst()->get($class, 'db', Config::UNINHERITED); |
||
1948 | self::$_cache_db[$class] = $dbItems; |
||
1949 | } |
||
1950 | |||
1951 | if($fieldName) { |
||
1952 | if(isset($dbItems[$fieldName])) { |
||
1953 | return $dbItems[$fieldName]; |
||
1954 | } |
||
1955 | } else { |
||
1956 | $items = isset($items) ? array_merge((array) $items, $dbItems) : $dbItems; |
||
1957 | } |
||
1958 | } |
||
1959 | |||
1960 | return $items; |
||
1961 | } |
||
1962 | |||
1963 | /** |
||
1964 | * @deprecated 4.0 Method has been replaced by hasMany() and hasManyComponent() |
||
1965 | * @param string $component |
||
1966 | * @param bool $classOnly |
||
1967 | * @return array|null |
||
1968 | */ |
||
1969 | public function has_many($component = null, $classOnly = true) { |
||
1970 | if($component) { |
||
1971 | Deprecation::notice('4.0', 'Please use hasManyComponent() instead'); |
||
1972 | return $this->hasManyComponent($component, $classOnly); |
||
1973 | } |
||
1974 | |||
1975 | Deprecation::notice('4.0', 'Please use hasMany() instead'); |
||
1976 | return $this->hasMany(null, $classOnly); |
||
1977 | } |
||
1978 | |||
1979 | /** |
||
1980 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
1981 | * relationships and their classes will be returned. |
||
1982 | * |
||
1983 | * @param string $component Deprecated - Name of component |
||
1984 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
1985 | * the field data stripped off. It defaults to TRUE. |
||
1986 | * @return string|array|false |
||
1987 | */ |
||
1988 | public function hasMany($component = null, $classOnly = true) { |
||
1989 | if($component) { |
||
1990 | Deprecation::notice( |
||
1991 | '4.0', |
||
1992 | 'Please use DataObject::hasManyComponent() instead of passing a component name to hasMany()', |
||
1993 | Deprecation::SCOPE_GLOBAL |
||
1994 | ); |
||
1995 | return $this->hasManyComponent($component, $classOnly); |
||
1996 | } |
||
1997 | |||
1998 | $hasMany = (array)Config::inst()->get($this->class, 'has_many', Config::INHERITED); |
||
1999 | if($hasMany && $classOnly) { |
||
2000 | return preg_replace('/(.+)?\..+/', '$1', $hasMany); |
||
2001 | } else { |
||
2002 | return $hasMany ? $hasMany : array(); |
||
2003 | } |
||
2004 | } |
||
2005 | |||
2006 | /** |
||
2007 | * Return data for a specific has_many component. |
||
2008 | * @param string $component |
||
2009 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
2010 | * the field data stripped off. It defaults to TRUE. |
||
2011 | * @return string|false |
||
2012 | */ |
||
2013 | public function hasManyComponent($component, $classOnly = true) { |
||
2014 | $hasMany = (array)Config::inst()->get($this->class, 'has_many', Config::INHERITED); |
||
2015 | |||
2016 | if($hasMany && array_key_exists($component, $hasMany)) { |
||
2017 | $hasMany = $hasMany[$component]; |
||
2018 | } else { |
||
2019 | return false; |
||
2020 | } |
||
2021 | |||
2022 | return ($classOnly) ? preg_replace('/(.+)?\..+/', '$1', $hasMany) : $hasMany; |
||
2023 | } |
||
2024 | |||
2025 | /** |
||
2026 | * @deprecated 4.0 Method has been replaced by manyManyExtraFields() and |
||
2027 | * manyManyExtraFieldsForComponent() |
||
2028 | * @param string $component |
||
2029 | * @return array |
||
2030 | */ |
||
2031 | public function many_many_extraFields($component = null) { |
||
2032 | if($component) { |
||
2033 | Deprecation::notice('4.0', 'Please use manyManyExtraFieldsForComponent() instead'); |
||
2034 | return $this->manyManyExtraFieldsForComponent($component); |
||
2035 | } |
||
2036 | |||
2037 | Deprecation::notice('4.0', 'Please use manyManyExtraFields() instead'); |
||
2038 | return $this->manyManyExtraFields(); |
||
2039 | } |
||
2040 | |||
2041 | /** |
||
2042 | * Return the many-to-many extra fields specification. |
||
2043 | * |
||
2044 | * If you don't specify a component name, it returns all |
||
2045 | * extra fields for all components available. |
||
2046 | * |
||
2047 | * @param string $component Deprecated - Name of component |
||
2048 | * @return array|null |
||
2049 | */ |
||
2050 | public function manyManyExtraFields($component = null) { |
||
2051 | if($component) { |
||
2052 | Deprecation::notice( |
||
2053 | '4.0', |
||
2054 | 'Please use DataObject::manyManyExtraFieldsForComponent() instead of passing a component name |
||
2055 | to manyManyExtraFields()', |
||
2056 | Deprecation::SCOPE_GLOBAL |
||
2057 | ); |
||
2058 | return $this->manyManyExtraFieldsForComponent($component); |
||
2059 | } |
||
2060 | |||
2061 | return Config::inst()->get($this->class, 'many_many_extraFields', Config::INHERITED); |
||
2062 | } |
||
2063 | |||
2064 | /** |
||
2065 | * Return the many-to-many extra fields specification for a specific component. |
||
2066 | * @param string $component |
||
2067 | * @return array|null |
||
2068 | */ |
||
2069 | public function manyManyExtraFieldsForComponent($component) { |
||
2070 | // Get all many_many_extraFields defined in this class or parent classes |
||
2071 | $extraFields = (array)Config::inst()->get($this->class, 'many_many_extraFields', Config::INHERITED); |
||
2072 | // Extra fields are immediately available |
||
2073 | if(isset($extraFields[$component])) { |
||
2074 | return $extraFields[$component]; |
||
2075 | } |
||
2076 | |||
2077 | // Check this class' belongs_many_manys to see if any of their reverse associations contain extra fields |
||
2078 | $manyMany = (array)Config::inst()->get($this->class, 'belongs_many_many', Config::INHERITED); |
||
2079 | $candidate = (isset($manyMany[$component])) ? $manyMany[$component] : null; |
||
2080 | if($candidate) { |
||
2081 | $relationName = null; |
||
2082 | // Extract class and relation name from dot-notation |
||
2083 | if(strpos($candidate, '.') !== false) { |
||
2084 | list($candidate, $relationName) = explode('.', $candidate, 2); |
||
2085 | } |
||
2086 | |||
2087 | // If we've not already found the relation name from dot notation, we need to find a relation that points |
||
2088 | // back to this class. As there's no dot-notation, there can only be one relation pointing to this class, |
||
2089 | // so it's safe to assume that it's the correct one |
||
2090 | if(!$relationName) { |
||
2091 | $candidateManyManys = (array)Config::inst()->get($candidate, 'many_many', Config::UNINHERITED); |
||
2092 | |||
2093 | foreach($candidateManyManys as $relation => $relatedClass) { |
||
2094 | if (is_a($this, $relatedClass)) { |
||
2095 | $relationName = $relation; |
||
2096 | } |
||
2097 | } |
||
2098 | } |
||
2099 | |||
2100 | // If we've found a matching relation on the target class, see if we can find extra fields for it |
||
2101 | $extraFields = (array)Config::inst()->get($candidate, 'many_many_extraFields', Config::UNINHERITED); |
||
2102 | if(isset($extraFields[$relationName])) { |
||
2103 | return $extraFields[$relationName]; |
||
2104 | } |
||
2105 | } |
||
2106 | |||
2107 | return isset($items) ? $items : null; |
||
2108 | } |
||
2109 | |||
2110 | /** |
||
2111 | * @deprecated 4.0 Method has been renamed to manyMany() |
||
2112 | * @param string $component |
||
2113 | * @return array|null |
||
2114 | */ |
||
2115 | public function many_many($component = null) { |
||
2116 | if($component) { |
||
2117 | Deprecation::notice('4.0', 'Please use manyManyComponent() instead'); |
||
2118 | return $this->manyManyComponent($component); |
||
2119 | } |
||
2120 | |||
2121 | Deprecation::notice('4.0', 'Please use manyMany() instead'); |
||
2122 | return $this->manyMany(); |
||
2123 | } |
||
2124 | |||
2125 | /** |
||
2126 | * Return information about a many-to-many component. |
||
2127 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
2128 | * components are returned. |
||
2129 | * |
||
2130 | * @see DataObject::manyManyComponent() |
||
2131 | * @param string $component Deprecated - Name of component |
||
2132 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
2133 | */ |
||
2134 | public function manyMany($component = null) { |
||
2135 | if($component) { |
||
2136 | Deprecation::notice( |
||
2137 | '4.0', |
||
2138 | 'Please use DataObject::manyManyComponent() instead of passing a component name to manyMany()', |
||
2139 | Deprecation::SCOPE_GLOBAL |
||
2140 | ); |
||
2141 | return $this->manyManyComponent($component); |
||
2142 | } |
||
2143 | |||
2144 | $manyManys = (array)Config::inst()->get($this->class, 'many_many', Config::INHERITED); |
||
2145 | $belongsManyManys = (array)Config::inst()->get($this->class, 'belongs_many_many', Config::INHERITED); |
||
2146 | |||
2147 | $items = array_merge($manyManys, $belongsManyManys); |
||
2148 | return $items; |
||
2149 | } |
||
2150 | |||
2151 | /** |
||
2152 | * Return information about a specific many_many component. Returns a numeric array of: |
||
2153 | * array( |
||
2154 | * <classname>, The class that relation is defined in e.g. "Product" |
||
2155 | * <candidateName>, The target class of the relation e.g. "Category" |
||
2156 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
2157 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
2158 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
2159 | * ) |
||
2160 | * @param string $component The component name |
||
2161 | * @return array|null |
||
2162 | */ |
||
2163 | public function manyManyComponent($component) { |
||
2164 | $classes = $this->getClassAncestry(); |
||
2165 | foreach($classes as $class) { |
||
2166 | $manyMany = Config::inst()->get($class, 'many_many', Config::UNINHERITED); |
||
2167 | // Check if the component is defined in many_many on this class |
||
2168 | $candidate = (isset($manyMany[$component])) ? $manyMany[$component] : null; |
||
2169 | if($candidate) { |
||
2170 | $parentField = $class . "ID"; |
||
2171 | $childField = ($class == $candidate) ? "ChildID" : $candidate . "ID"; |
||
2172 | return array($class, $candidate, $parentField, $childField, "{$class}_$component"); |
||
2173 | } |
||
2174 | |||
2175 | // Check if the component is defined in belongs_many_many on this class |
||
2176 | $belongsManyMany = Config::inst()->get($class, 'belongs_many_many', Config::UNINHERITED); |
||
2177 | $candidate = (isset($belongsManyMany[$component])) ? $belongsManyMany[$component] : null; |
||
2178 | if($candidate) { |
||
2179 | // Extract class and relation name from dot-notation |
||
2180 | if(strpos($candidate, '.') !== false) { |
||
2181 | list($candidate, $relationName) = explode('.', $candidate, 2); |
||
2182 | } |
||
2183 | |||
2184 | $childField = $candidate . "ID"; |
||
2185 | |||
2186 | // We need to find the inverse component name |
||
2187 | $otherManyMany = Config::inst()->get($candidate, 'many_many', Config::UNINHERITED); |
||
2188 | if(!$otherManyMany) { |
||
2189 | throw new LogicException("Inverse component of $candidate not found ({$this->class})"); |
||
2190 | } |
||
2191 | |||
2192 | // If we've got a relation name (extracted from dot-notation), we can already work out |
||
2193 | // the join table and candidate class name... |
||
2194 | if(isset($relationName) && isset($otherManyMany[$relationName])) { |
||
2195 | $candidateClass = $otherManyMany[$relationName]; |
||
2196 | $joinTable = "{$candidate}_{$relationName}"; |
||
2197 | } else { |
||
2198 | // ... otherwise, we need to loop over the many_manys and find a relation that |
||
2199 | // matches up to this class |
||
2200 | foreach($otherManyMany as $inverseComponentName => $candidateClass) { |
||
2201 | if($candidateClass == $class || is_subclass_of($class, $candidateClass)) { |
||
2202 | $joinTable = "{$candidate}_{$inverseComponentName}"; |
||
2203 | break; |
||
2204 | } |
||
2205 | } |
||
2206 | } |
||
2207 | |||
2208 | // If we could work out the join table, we've got all the info we need |
||
2209 | if(isset($joinTable)) { |
||
2210 | $parentField = ($class == $candidate) ? "ChildID" : $candidateClass . "ID"; |
||
2211 | return array($class, $candidate, $parentField, $childField, $joinTable); |
||
2212 | } |
||
2213 | |||
2214 | throw new LogicException("Orphaned \$belongs_many_many value for $this->class.$component"); |
||
2215 | } |
||
2216 | } |
||
2217 | } |
||
2218 | |||
2219 | /** |
||
2220 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
2221 | * |
||
2222 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
2223 | * |
||
2224 | * @return array or false |
||
2225 | */ |
||
2226 | public function database_extensions($class){ |
||
2227 | $extensions = Config::inst()->get($class, 'database_extensions', Config::UNINHERITED); |
||
2228 | |||
2229 | if($extensions) |
||
2230 | return $extensions; |
||
2231 | else |
||
2232 | return false; |
||
2233 | } |
||
2234 | |||
2235 | /** |
||
2236 | * Generates a SearchContext to be used for building and processing |
||
2237 | * a generic search form for properties on this object. |
||
2238 | * |
||
2239 | * @return SearchContext |
||
2240 | */ |
||
2241 | public function getDefaultSearchContext() { |
||
2242 | return new SearchContext( |
||
2243 | $this->class, |
||
2244 | $this->scaffoldSearchFields(), |
||
2245 | $this->defaultSearchFilters() |
||
2246 | ); |
||
2247 | } |
||
2248 | |||
2249 | /** |
||
2250 | * Determine which properties on the DataObject are |
||
2251 | * searchable, and map them to their default {@link FormField} |
||
2252 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
2253 | * |
||
2254 | * Some additional logic is included for switching field labels, based on |
||
2255 | * how generic or specific the field type is. |
||
2256 | * |
||
2257 | * Used by {@link SearchContext}. |
||
2258 | * |
||
2259 | * @param array $_params |
||
2260 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
2261 | * 'restrictFields': Numeric array of a field name whitelist |
||
2262 | * @return FieldList |
||
2263 | */ |
||
2264 | public function scaffoldSearchFields($_params = null) { |
||
2265 | $params = array_merge( |
||
2266 | array( |
||
2267 | 'fieldClasses' => false, |
||
2268 | 'restrictFields' => false |
||
2269 | ), |
||
2270 | (array)$_params |
||
2271 | ); |
||
2272 | $fields = new FieldList(); |
||
2273 | foreach($this->searchableFields() as $fieldName => $spec) { |
||
2274 | if($params['restrictFields'] && !in_array($fieldName, $params['restrictFields'])) continue; |
||
2275 | |||
2276 | // If a custom fieldclass is provided as a string, use it |
||
2277 | if($params['fieldClasses'] && isset($params['fieldClasses'][$fieldName])) { |
||
2278 | $fieldClass = $params['fieldClasses'][$fieldName]; |
||
2279 | $field = new $fieldClass($fieldName); |
||
2280 | // If we explicitly set a field, then construct that |
||
2281 | } else if(isset($spec['field'])) { |
||
2282 | // If it's a string, use it as a class name and construct |
||
2283 | if(is_string($spec['field'])) { |
||
2284 | $fieldClass = $spec['field']; |
||
2285 | $field = new $fieldClass($fieldName); |
||
2286 | |||
2287 | // If it's a FormField object, then just use that object directly. |
||
2288 | } else if($spec['field'] instanceof FormField) { |
||
2289 | $field = $spec['field']; |
||
2290 | |||
2291 | // Otherwise we have a bug |
||
2292 | } else { |
||
2293 | user_error("Bad value for searchable_fields, 'field' value: " |
||
2294 | . var_export($spec['field'], true), E_USER_WARNING); |
||
2295 | } |
||
2296 | |||
2297 | // Otherwise, use the database field's scaffolder |
||
2298 | } else { |
||
2299 | $field = $this->relObject($fieldName)->scaffoldSearchField(); |
||
2300 | } |
||
2301 | |||
2302 | if (strstr($fieldName, '.')) { |
||
2303 | $field->setName(str_replace('.', '__', $fieldName)); |
||
2304 | } |
||
2305 | $field->setTitle($spec['title']); |
||
2306 | |||
2307 | $fields->push($field); |
||
2308 | } |
||
2309 | return $fields; |
||
2310 | } |
||
2311 | |||
2312 | /** |
||
2313 | * Scaffold a simple edit form for all properties on this dataobject, |
||
2314 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
2315 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
2316 | * |
||
2317 | * @uses FormScaffolder |
||
2318 | * |
||
2319 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
2320 | * @return FieldList |
||
2321 | */ |
||
2322 | public function scaffoldFormFields($_params = null) { |
||
2343 | |||
2344 | /** |
||
2345 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
2346 | * being called on extensions |
||
2347 | * |
||
2348 | * @param callable $callback The callback to execute |
||
2349 | */ |
||
2350 | protected function beforeUpdateCMSFields($callback) { |
||
2353 | |||
2354 | /** |
||
2355 | * Centerpiece of every data administration interface in Silverstripe, |
||
2356 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
2357 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
2358 | * generate this set. To customize, overload this method in a subclass |
||
2359 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
2360 | * |
||
2361 | * <code> |
||
2362 | * class MyCustomClass extends DataObject { |
||
2363 | * static $db = array('CustomProperty'=>'Boolean'); |
||
2364 | * |
||
2365 | * function getCMSFields() { |
||
2366 | * $fields = parent::getCMSFields(); |
||
2367 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
2368 | * return $fields; |
||
2369 | * } |
||
2370 | * } |
||
2371 | * </code> |
||
2372 | * |
||
2373 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
2374 | * |
||
2375 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
2376 | */ |
||
2377 | public function getCMSFields() { |
||
2389 | |||
2390 | /** |
||
2391 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
2392 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
2393 | * |
||
2394 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
2395 | */ |
||
2396 | public function getCMSActions() { |
||
2401 | |||
2402 | |||
2403 | /** |
||
2404 | * Used for simple frontend forms without relation editing |
||
2405 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
2406 | * by default. To customize, either overload this method in your |
||
2407 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
2408 | * |
||
2409 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
2410 | * |
||
2411 | * @param array $params See {@link scaffoldFormFields()} |
||
2412 | * @return FieldList Always returns a simple field collection without TabSet. |
||
2413 | */ |
||
2414 | public function getFrontEndFields($params = null) { |
||
2420 | |||
2421 | /** |
||
2422 | * Gets the value of a field. |
||
2423 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
2424 | * |
||
2425 | * @param string $field The name of the field |
||
2426 | * |
||
2427 | * @return mixed The field value |
||
2428 | */ |
||
2429 | public function getField($field) { |
||
2430 | // If we already have an object in $this->record, then we should just return that |
||
2431 | if(isset($this->record[$field]) && is_object($this->record[$field])) return $this->record[$field]; |
||
2432 | |||
2433 | // Do we have a field that needs to be lazy loaded? |
||
2434 | if(isset($this->record[$field.'_Lazy'])) { |
||
2435 | $tableClass = $this->record[$field.'_Lazy']; |
||
2436 | $this->loadLazyFields($tableClass); |
||
2437 | } |
||
2438 | |||
2439 | // Otherwise, we need to determine if this is a complex field |
||
2440 | if(self::is_composite_field($this->class, $field)) { |
||
2441 | $helper = $this->castingHelper($field); |
||
2442 | $fieldObj = Object::create_from_string($helper, $field); |
||
2443 | |||
2444 | $compositeFields = $fieldObj->compositeDatabaseFields(); |
||
2445 | foreach ($compositeFields as $compositeName => $compositeType) { |
||
2446 | if(isset($this->record[$field.$compositeName.'_Lazy'])) { |
||
2447 | $tableClass = $this->record[$field.$compositeName.'_Lazy']; |
||
2448 | $this->loadLazyFields($tableClass); |
||
2449 | } |
||
2450 | } |
||
2451 | |||
2452 | // write value only if either the field value exists, |
||
2453 | // or a valid record has been loaded from the database |
||
2454 | $value = (isset($this->record[$field])) ? $this->record[$field] : null; |
||
2455 | if($value || $this->exists()) $fieldObj->setValue($value, $this->record, false); |
||
2456 | |||
2457 | $this->record[$field] = $fieldObj; |
||
2458 | |||
2459 | return $this->record[$field]; |
||
2460 | } |
||
2461 | |||
2462 | return isset($this->record[$field]) ? $this->record[$field] : null; |
||
2463 | } |
||
2464 | |||
2465 | /** |
||
2466 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
2467 | * |
||
2468 | * @param tableClass Base table to load the values from. Others are joined as required. |
||
2469 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
2470 | */ |
||
2471 | protected function loadLazyFields($tableClass = null) { |
||
2472 | if (!$tableClass) { |
||
2473 | $loaded = array(); |
||
2474 | |||
2475 | foreach ($this->record as $key => $value) { |
||
2476 | if (strlen($key) > 5 && substr($key, -5) == '_Lazy' && !array_key_exists($value, $loaded)) { |
||
2477 | $this->loadLazyFields($value); |
||
2478 | $loaded[$value] = $value; |
||
2479 | } |
||
2480 | } |
||
2481 | |||
2482 | return; |
||
2483 | } |
||
2484 | |||
2485 | $dataQuery = new DataQuery($tableClass); |
||
2486 | |||
2487 | // Reset query parameter context to that of this DataObject |
||
2488 | if($params = $this->getSourceQueryParams()) { |
||
2489 | foreach($params as $key => $value) $dataQuery->setQueryParam($key, $value); |
||
2490 | } |
||
2491 | |||
2540 | |||
2541 | /** |
||
2542 | * Return the fields that have changed. |
||
2543 | * |
||
2544 | * The change level affects what the functions defines as "changed": |
||
2545 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
2546 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
2547 | * for example a change from 0 to null would not be included. |
||
2548 | * |
||
2549 | * Example return: |
||
2550 | * <code> |
||
2551 | * array( |
||
2552 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
2553 | * ) |
||
2554 | * </code> |
||
2555 | * |
||
2556 | * @param boolean $databaseFieldsOnly Get only database fields that have changed |
||
2557 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
2558 | * @return array |
||
2559 | */ |
||
2560 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
2600 | |||
2601 | /** |
||
2602 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
2603 | * since loading them from the database. |
||
2604 | * |
||
2605 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
2606 | * @param int $changeLevel See {@link getChangedFields()} |
||
2607 | * @return boolean |
||
2608 | */ |
||
2609 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
2618 | |||
2619 | /** |
||
2620 | * Set the value of the field |
||
2621 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
2622 | * |
||
2623 | * @param string $fieldName Name of the field |
||
2624 | * @param mixed $val New field value |
||
2625 | * @return DataObject $this |
||
2626 | */ |
||
2627 | public function setField($fieldName, $val) { |
||
2674 | |||
2675 | /** |
||
2676 | * Set the value of the field, using a casting object. |
||
2677 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
2678 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
2679 | * can be saved into the Image table. |
||
2680 | * |
||
2681 | * @param string $fieldName Name of the field |
||
2682 | * @param mixed $value New field value |
||
2683 | * @return DataObject $this |
||
2684 | */ |
||
2685 | public function setCastedField($fieldName, $val) { |
||
2699 | |||
2700 | /** |
||
2701 | * Returns true if the given field exists in a database column on any of |
||
2702 | * the objects tables and optionally look up a dynamic getter with |
||
2703 | * get<fieldName>(). |
||
2704 | * |
||
2705 | * @param string $field Name of the field |
||
2706 | * @return boolean True if the given field exists |
||
2707 | */ |
||
2708 | public function hasField($field) { |
||
2716 | |||
2717 | /** |
||
2718 | * Returns true if the given field exists as a database column |
||
2719 | * |
||
2720 | * @param string $field Name of the field |
||
2721 | * |
||
2722 | * @return boolean |
||
2723 | */ |
||
2724 | public function hasDatabaseField($field) { |
||
2729 | |||
2730 | /** |
||
2731 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2732 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2733 | * |
||
2734 | * @param string $field Name of the field |
||
2735 | * @return string The field type of the given field |
||
2736 | */ |
||
2737 | public function hasOwnTableDatabaseField($field) { |
||
2740 | |||
2741 | /** |
||
2742 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
2743 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
2744 | * |
||
2745 | * @param string $class Class name to check |
||
2746 | * @param string $field Name of the field |
||
2747 | * @return string The field type of the given field |
||
2748 | */ |
||
2749 | public static function has_own_table_database_field($class, $field) { |
||
2762 | |||
2763 | /** |
||
2764 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
2765 | * actually looking in the database. |
||
2766 | * |
||
2767 | * @param string $dataClass |
||
2768 | * @return bool |
||
2769 | */ |
||
2770 | public static function has_own_table($dataClass) { |
||
2785 | |||
2786 | /** |
||
2787 | * Returns true if the member is allowed to do the given action. |
||
2788 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
2789 | * |
||
2790 | * @param string $perm The permission to be checked, such as 'View'. |
||
2791 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
2792 | * in user. |
||
2793 | * |
||
2794 | * @return boolean True if the the member is allowed to do the given action |
||
2795 | */ |
||
2796 | public function can($perm, $member = null) { |
||
2855 | |||
2856 | /** |
||
2857 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
2858 | * expected to return one of three values: |
||
2859 | * |
||
2860 | * - false: Disallow this permission, regardless of what other extensions say |
||
2861 | * - true: Allow this permission, as long as no other extensions return false |
||
2862 | * - NULL: Don't affect the outcome |
||
2863 | * |
||
2864 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
2865 | * |
||
2866 | * <code> |
||
2867 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
2868 | * if($extended !== null) return $extended; |
||
2869 | * else return $normalValue; |
||
2870 | * </code> |
||
2871 | * |
||
2872 | * @param String $methodName Method on the same object, e.g. {@link canEdit()} |
||
2873 | * @param Member|int $member |
||
2874 | * @return boolean|null |
||
2875 | */ |
||
2876 | public function extendedCan($methodName, $member) { |
||
2887 | |||
2888 | /** |
||
2889 | * @param Member $member |
||
2890 | * @return boolean |
||
2891 | */ |
||
2892 | public function canView($member = null) { |
||
2899 | |||
2900 | /** |
||
2901 | * @param Member $member |
||
2902 | * @return boolean |
||
2903 | */ |
||
2904 | public function canEdit($member = null) { |
||
2911 | |||
2912 | /** |
||
2913 | * @param Member $member |
||
2914 | * @return boolean |
||
2915 | */ |
||
2916 | public function canDelete($member = null) { |
||
2923 | |||
2924 | /** |
||
2925 | * @todo Should canCreate be a static method? |
||
2926 | * |
||
2927 | * @param Member $member |
||
2928 | * @return boolean |
||
2929 | */ |
||
2930 | public function canCreate($member = null) { |
||
2937 | |||
2938 | /** |
||
2939 | * Debugging used by Debug::show() |
||
2940 | * |
||
2941 | * @return string HTML data representing this object |
||
2942 | */ |
||
2943 | public function debug() { |
||
2951 | |||
2952 | /** |
||
2953 | * Return the DBField object that represents the given field. |
||
2954 | * This works similarly to obj() with 2 key differences: |
||
2955 | * - it still returns an object even when the field has no value. |
||
2956 | * - it only matches fields and not methods |
||
2957 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
2958 | * |
||
2959 | * @param string $fieldName Name of the field |
||
2960 | * @return DBField The field as a DBField object |
||
2961 | */ |
||
2962 | public function dbObject($fieldName) { |
||
2997 | |||
2998 | /** |
||
2999 | * Traverses to a DBField referenced by relationships between data objects. |
||
3000 | * |
||
3001 | * The path to the related field is specified with dot separated syntax |
||
3002 | * (eg: Parent.Child.Child.FieldName). |
||
3003 | * |
||
3004 | * @param string $fieldPath |
||
3005 | * |
||
3006 | * @return mixed DBField of the field on the object or a DataList instance. |
||
3007 | */ |
||
3008 | public function relObject($fieldPath) { |
||
3038 | |||
3039 | /** |
||
3040 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
3041 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
3042 | * |
||
3043 | * @param $fieldPath string |
||
3044 | * @return string | null - will return null on a missing value |
||
3045 | */ |
||
3046 | public function relField($fieldName) { |
||
3081 | |||
3082 | /** |
||
3083 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
3084 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
3085 | * |
||
3086 | * @return String |
||
3087 | */ |
||
3088 | public function getReverseAssociation($className) { |
||
3104 | |||
3105 | /** |
||
3106 | * Return all objects matching the filter |
||
3107 | * sub-classes are automatically selected and included |
||
3108 | * |
||
3109 | * @param string $callerClass The class of objects to be returned |
||
3110 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3111 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
3112 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
3113 | * BY clause. If omitted, self::$default_sort will be used. |
||
3114 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
3115 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
3116 | * @param string $containerClass The container class to return the results in. |
||
3117 | * |
||
3118 | * @todo $containerClass is Ignored, why? |
||
3119 | * |
||
3120 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
3121 | */ |
||
3122 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
3159 | |||
3160 | |||
3161 | /** |
||
3162 | * @deprecated |
||
3163 | */ |
||
3164 | public function Aggregate($class = null) { |
||
3181 | |||
3182 | /** |
||
3183 | * @deprecated |
||
3184 | */ |
||
3185 | public function RelationshipAggregate($relationship) { |
||
3190 | |||
3191 | /** |
||
3192 | * Return the first item matching the given query. |
||
3193 | * All calls to get_one() are cached. |
||
3194 | * |
||
3195 | * @param string $callerClass The class of objects to be returned |
||
3196 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
3197 | * Supports parameterised queries. See SQLQuery::addWhere() for syntax examples. |
||
3198 | * @param boolean $cache Use caching |
||
3199 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
3200 | * |
||
3201 | * @return DataObject The first item matching the query |
||
3202 | */ |
||
3203 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
3229 | |||
3230 | /** |
||
3231 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
3232 | * Also clears any cached aggregate data. |
||
3233 | * |
||
3234 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
3235 | * When false will just clear session-local cached data |
||
3236 | * @return DataObject $this |
||
3237 | */ |
||
3238 | public function flushCache($persistent = true) { |
||
3256 | |||
3257 | /** |
||
3258 | * Flush the get_one global cache and destroy associated objects. |
||
3259 | */ |
||
3260 | public static function flush_and_destroy_cache() { |
||
3268 | |||
3269 | /** |
||
3270 | * Reset all global caches associated with DataObject. |
||
3271 | */ |
||
3272 | public static function reset() { |
||
3283 | |||
3284 | /** |
||
3285 | * Return the given element, searching by ID |
||
3286 | * |
||
3287 | * @param string $callerClass The class of the object to be returned |
||
3288 | * @param int $id The id of the element |
||
3289 | * @param boolean $cache See {@link get_one()} |
||
3290 | * |
||
3291 | * @return DataObject The element |
||
3292 | */ |
||
3293 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
3310 | |||
3311 | /** |
||
3312 | * Get the name of the base table for this object |
||
3313 | */ |
||
3314 | public function baseTable() { |
||
3318 | |||
3319 | /** |
||
3320 | * @var Array Parameters used in the query that built this object. |
||
3321 | * This can be used by decorators (e.g. lazy loading) to |
||
3322 | * run additional queries using the same context. |
||
3323 | */ |
||
3324 | protected $sourceQueryParams; |
||
3325 | |||
3326 | /** |
||
3327 | * @see $sourceQueryParams |
||
3328 | * @return array |
||
3329 | */ |
||
3330 | public function getSourceQueryParams() { |
||
3333 | |||
3334 | /** |
||
3335 | * @see $sourceQueryParams |
||
3336 | * @param array |
||
3337 | */ |
||
3338 | public function setSourceQueryParams($array) { |
||
3341 | |||
3342 | /** |
||
3343 | * @see $sourceQueryParams |
||
3344 | * @param array |
||
3345 | */ |
||
3346 | public function setSourceQueryParam($key, $value) { |
||
3349 | |||
3350 | /** |
||
3351 | * @see $sourceQueryParams |
||
3352 | * @return Mixed |
||
3353 | */ |
||
3354 | public function getSourceQueryParam($key) { |
||
3358 | |||
3359 | //-------------------------------------------------------------------------------------------// |
||
3360 | |||
3361 | /** |
||
3362 | * Return the database indexes on this table. |
||
3363 | * This array is indexed by the name of the field with the index, and |
||
3364 | * the value is the type of index. |
||
3365 | */ |
||
3366 | public function databaseIndexes() { |
||
3391 | |||
3392 | /** |
||
3393 | * Check the database schema and update it as necessary. |
||
3394 | * |
||
3395 | * @uses DataExtension->augmentDatabase() |
||
3396 | */ |
||
3397 | public function requireTable() { |
||
3442 | |||
3443 | /** |
||
3444 | * Validate that the configured relations for this class use the correct syntaxes |
||
3445 | * @throws LogicException |
||
3446 | */ |
||
3447 | protected function validateModelDefinitions() { |
||
3478 | |||
3479 | /** |
||
3480 | * Add default records to database. This function is called whenever the |
||
3481 | * database is built, after the database tables have all been created. Overload |
||
3482 | * this to add default records when the database is built, but make sure you |
||
3483 | * call parent::requireDefaultRecords(). |
||
3484 | * |
||
3485 | * @uses DataExtension->requireDefaultRecords() |
||
3486 | */ |
||
3487 | public function requireDefaultRecords() { |
||
3505 | |||
3506 | /** |
||
3507 | * Returns fields bu traversing the class heirachy in a bottom-up direction. |
||
3508 | * |
||
3509 | * Needed to avoid getCMSFields being empty when customDatabaseFields overlooks |
||
3510 | * the inheritance chain of the $db array, where a child data object has no $db array, |
||
3511 | * but still needs to know the properties of its parent. This should be merged into databaseFields or |
||
3512 | * customDatabaseFields. |
||
3513 | * |
||
3514 | * @todo review whether this is still needed after recent API changes |
||
3515 | */ |
||
3516 | public function inheritedDatabaseFields() { |
||
3527 | |||
3528 | /** |
||
3529 | * Get the default searchable fields for this object, as defined in the |
||
3530 | * $searchable_fields list. If searchable fields are not defined on the |
||
3531 | * data object, uses a default selection of summary fields. |
||
3532 | * |
||
3533 | * @return array |
||
3534 | */ |
||
3535 | public function searchableFields() { |
||
3609 | |||
3610 | /** |
||
3611 | * Get any user defined searchable fields labels that |
||
3612 | * exist. Allows overriding of default field names in the form |
||
3613 | * interface actually presented to the user. |
||
3614 | * |
||
3615 | * The reason for keeping this separate from searchable_fields, |
||
3616 | * which would be a logical place for this functionality, is to |
||
3617 | * avoid bloating and complicating the configuration array. Currently |
||
3618 | * much of this system is based on sensible defaults, and this property |
||
3619 | * would generally only be set in the case of more complex relationships |
||
3620 | * between data object being required in the search interface. |
||
3621 | * |
||
3622 | * Generates labels based on name of the field itself, if no static property |
||
3623 | * {@link self::field_labels} exists. |
||
3624 | * |
||
3625 | * @uses $field_labels |
||
3626 | * @uses FormField::name_to_label() |
||
3627 | * |
||
3628 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
3629 | * |
||
3630 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
3631 | */ |
||
3632 | public function fieldLabels($includerelations = true) { |
||
3667 | |||
3668 | /** |
||
3669 | * Get a human-readable label for a single field, |
||
3670 | * see {@link fieldLabels()} for more details. |
||
3671 | * |
||
3672 | * @uses fieldLabels() |
||
3673 | * @uses FormField::name_to_label() |
||
3674 | * |
||
3675 | * @param string $name Name of the field |
||
3676 | * @return string Label of the field |
||
3677 | */ |
||
3678 | public function fieldLabel($name) { |
||
3682 | |||
3683 | /** |
||
3684 | * Get the default summary fields for this object. |
||
3685 | * |
||
3686 | * @todo use the translation apparatus to return a default field selection for the language |
||
3687 | * |
||
3688 | * @return array |
||
3689 | */ |
||
3690 | public function summaryFields() { |
||
3723 | |||
3724 | /** |
||
3725 | * Defines a default list of filters for the search context. |
||
3726 | * |
||
3727 | * If a filter class mapping is defined on the data object, |
||
3728 | * it is constructed here. Otherwise, the default filter specified in |
||
3729 | * {@link DBField} is used. |
||
3730 | * |
||
3731 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
3732 | * |
||
3733 | * @return array |
||
3734 | */ |
||
3735 | public function defaultSearchFilters() { |
||
3756 | |||
3757 | /** |
||
3758 | * @return boolean True if the object is in the database |
||
3759 | */ |
||
3760 | public function isInDB() { |
||
3763 | |||
3764 | /* |
||
3765 | * @ignore |
||
3766 | */ |
||
3767 | private static $subclass_access = true; |
||
3768 | |||
3769 | /** |
||
3770 | * Temporarily disable subclass access in data object qeur |
||
3771 | */ |
||
3772 | public static function disable_subclass_access() { |
||
3778 | |||
3779 | //-------------------------------------------------------------------------------------------// |
||
3780 | |||
3781 | /** |
||
3782 | * Database field definitions. |
||
3783 | * This is a map from field names to field type. The field |
||
3784 | * type should be a class that extends . |
||
3785 | * @var array |
||
3786 | * @config |
||
3787 | */ |
||
3788 | private static $db = null; |
||
3789 | |||
3790 | /** |
||
3791 | * Use a casting object for a field. This is a map from |
||
3792 | * field name to class name of the casting object. |
||
3793 | * @var array |
||
3794 | */ |
||
3795 | private static $casting = array( |
||
3796 | "ID" => 'Int', |
||
3797 | "ClassName" => 'Varchar', |
||
3798 | "LastEdited" => "SS_Datetime", |
||
3799 | "Created" => "SS_Datetime", |
||
3800 | "Title" => 'Text', |
||
3801 | ); |
||
3802 | |||
3803 | /** |
||
3804 | * Specify custom options for a CREATE TABLE call. |
||
3805 | * Can be used to specify a custom storage engine for specific database table. |
||
3806 | * All options have to be keyed for a specific database implementation, |
||
3807 | * identified by their class name (extending from {@link SS_Database}). |
||
3808 | * |
||
3809 | * <code> |
||
3810 | * array( |
||
3811 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
3812 | * ) |
||
3813 | * </code> |
||
3814 | * |
||
3815 | * Caution: This API is experimental, and might not be |
||
3816 | * included in the next major release. Please use with care. |
||
3817 | * |
||
3818 | * @var array |
||
3819 | * @config |
||
3820 | */ |
||
3821 | private static $create_table_options = array( |
||
3822 | 'MySQLDatabase' => 'ENGINE=InnoDB' |
||
3823 | ); |
||
3824 | |||
3825 | /** |
||
3826 | * If a field is in this array, then create a database index |
||
3827 | * on that field. This is a map from fieldname to index type. |
||
3828 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
3829 | * |
||
3830 | * @var array |
||
3831 | * @config |
||
3832 | */ |
||
3833 | private static $indexes = null; |
||
3834 | |||
3835 | /** |
||
3836 | * Inserts standard column-values when a DataObject |
||
3837 | * is instanciated. Does not insert default records {@see $default_records}. |
||
3838 | * This is a map from fieldname to default value. |
||
3839 | * |
||
3840 | * - If you would like to change a default value in a sub-class, just specify it. |
||
3841 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
3842 | * or false in your subclass. Setting it to null won't work. |
||
3843 | * |
||
3844 | * @var array |
||
3845 | * @config |
||
3846 | */ |
||
3847 | private static $defaults = null; |
||
3848 | |||
3849 | /** |
||
3850 | * Multidimensional array which inserts default data into the database |
||
3851 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
3852 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
3853 | * behaviour such as publishing and ParentNodes. |
||
3854 | * |
||
3855 | * Example: |
||
3856 | * array( |
||
3857 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
3858 | * array('Title' => "DefaultPage2") |
||
3859 | * ). |
||
3860 | * |
||
3861 | * @var array |
||
3862 | * @config |
||
3863 | */ |
||
3864 | private static $default_records = null; |
||
3865 | |||
3866 | /** |
||
3867 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
3868 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
3869 | * |
||
3870 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3871 | * |
||
3872 | * @var array |
||
3873 | * @config |
||
3874 | */ |
||
3875 | private static $has_one = null; |
||
3876 | |||
3877 | /** |
||
3878 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
3879 | * |
||
3880 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
3881 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
3882 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
3883 | * |
||
3884 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
3885 | * |
||
3886 | * @var array |
||
3887 | * @config |
||
3888 | */ |
||
3889 | private static $belongs_to; |
||
3890 | |||
3891 | /** |
||
3892 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
3893 | * |
||
3894 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
3895 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
3896 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
3897 | * which foreign key to use. |
||
3898 | * |
||
3899 | * @var array |
||
3900 | * @config |
||
3901 | */ |
||
3902 | private static $has_many = null; |
||
3903 | |||
3904 | /** |
||
3905 | * many-many relationship definitions. |
||
3906 | * This is a map from component name to data type. |
||
3907 | * @var array |
||
3908 | * @config |
||
3909 | */ |
||
3910 | private static $many_many = null; |
||
3911 | |||
3912 | /** |
||
3913 | * Extra fields to include on the connecting many-many table. |
||
3914 | * This is a map from field name to field type. |
||
3915 | * |
||
3916 | * Example code: |
||
3917 | * <code> |
||
3918 | * public static $many_many_extraFields = array( |
||
3919 | * 'Members' => array( |
||
3920 | * 'Role' => 'Varchar(100)' |
||
3921 | * ) |
||
3922 | * ); |
||
3923 | * </code> |
||
3924 | * |
||
3925 | * @var array |
||
3926 | * @config |
||
3927 | */ |
||
3928 | private static $many_many_extraFields = null; |
||
3929 | |||
3930 | /** |
||
3931 | * The inverse side of a many-many relationship. |
||
3932 | * This is a map from component name to data type. |
||
3933 | * @var array |
||
3934 | * @config |
||
3935 | */ |
||
3936 | private static $belongs_many_many = null; |
||
3937 | |||
3938 | /** |
||
3939 | * The default sort expression. This will be inserted in the ORDER BY |
||
3940 | * clause of a SQL query if no other sort expression is provided. |
||
3941 | * @var string |
||
3942 | * @config |
||
3943 | */ |
||
3944 | private static $default_sort = null; |
||
3945 | |||
3946 | /** |
||
3947 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
3948 | * search interface. |
||
3949 | * |
||
3950 | * Overriding the default filter, with a custom defined filter: |
||
3951 | * <code> |
||
3952 | * static $searchable_fields = array( |
||
3953 | * "Name" => "PartialMatchFilter" |
||
3954 | * ); |
||
3955 | * </code> |
||
3956 | * |
||
3957 | * Overriding the default form fields, with a custom defined field. |
||
3958 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
3959 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
3960 | * <code> |
||
3961 | * static $searchable_fields = array( |
||
3962 | * "Name" => array( |
||
3963 | * "field" => "TextField" |
||
3964 | * ) |
||
3965 | * ); |
||
3966 | * </code> |
||
3967 | * |
||
3968 | * Overriding the default form field, filter and title: |
||
3969 | * <code> |
||
3970 | * static $searchable_fields = array( |
||
3971 | * "Organisation.ZipCode" => array( |
||
3972 | * "field" => "TextField", |
||
3973 | * "filter" => "PartialMatchFilter", |
||
3974 | * "title" => 'Organisation ZIP' |
||
3975 | * ) |
||
3976 | * ); |
||
3977 | * </code> |
||
3978 | * @config |
||
3979 | */ |
||
3980 | private static $searchable_fields = null; |
||
3981 | |||
3982 | /** |
||
3983 | * User defined labels for searchable_fields, used to override |
||
3984 | * default display in the search form. |
||
3985 | * @config |
||
3986 | */ |
||
3987 | private static $field_labels = null; |
||
3988 | |||
3989 | /** |
||
3990 | * Provides a default list of fields to be used by a 'summary' |
||
3991 | * view of this object. |
||
3992 | * @config |
||
3993 | */ |
||
3994 | private static $summary_fields = null; |
||
3995 | |||
3996 | /** |
||
3997 | * Provides a list of allowed methods that can be called via RESTful api. |
||
3998 | */ |
||
3999 | public static $allowed_actions = null; |
||
4000 | |||
4001 | /** |
||
4002 | * Collect all static properties on the object |
||
4003 | * which contain natural language, and need to be translated. |
||
4004 | * The full entity name is composed from the class name and a custom identifier. |
||
4005 | * |
||
4006 | * @return array A numerical array which contains one or more entities in array-form. |
||
4007 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
4008 | * $entity, $string, $priority, $context. |
||
4009 | */ |
||
4010 | public function provideI18nEntities() { |
||
4028 | |||
4029 | /** |
||
4030 | * Returns true if the given method/parameter has a value |
||
4031 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
4032 | * |
||
4033 | * @param string $field The field name |
||
4034 | * @param array $arguments |
||
4035 | * @param bool $cache |
||
4036 | * @return boolean |
||
4037 | */ |
||
4038 | public function hasValue($field, $arguments = null, $cache = true) { |
||
4046 | |||
4047 | } |
||
4048 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.