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 |
||
| 109 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Human-readable singular name. |
||
| 113 | * @var string |
||
| 114 | * @config |
||
| 115 | */ |
||
| 116 | private static $singular_name = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Human-readable plural name |
||
| 120 | * @var string |
||
| 121 | * @config |
||
| 122 | */ |
||
| 123 | private static $plural_name = null; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Allow API access to this object? |
||
| 127 | * @todo Define the options that can be set here |
||
| 128 | * @config |
||
| 129 | */ |
||
| 130 | private static $api_access = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Allows specification of a default value for the ClassName field. |
||
| 134 | * Configure this value only in subclasses of DataObject. |
||
| 135 | * |
||
| 136 | * @config |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | private static $default_classname = null; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * True if this DataObject has been destroyed. |
||
| 143 | * @var boolean |
||
| 144 | */ |
||
| 145 | public $destroyed = false; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The DataModel from this this object comes |
||
| 149 | */ |
||
| 150 | protected $model; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 154 | * |
||
| 155 | * Use {@link toMap()} if you want an array representation |
||
| 156 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 157 | * |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected $record; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 164 | */ |
||
| 165 | const CHANGE_NONE = 0; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 169 | * (before !== after && before == after) |
||
| 170 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 171 | * Value changes are by nature also considered strict changes. |
||
| 172 | */ |
||
| 173 | const CHANGE_STRICT = 1; |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Represents a field that has changed the loosely defined value |
||
| 177 | * (before != after, thus, before !== after)) |
||
| 178 | * E.g. change false to true, but not false to 0 |
||
| 179 | */ |
||
| 180 | const CHANGE_VALUE = 2; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * An array indexed by fieldname, true if the field has been changed. |
||
| 184 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 185 | * the changed state. |
||
| 186 | * |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | private $changed; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * The database record (in the same format as $record), before |
||
| 193 | * any changes. |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | protected $original; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 200 | * @var boolean |
||
| 201 | */ |
||
| 202 | protected $brokenOnDelete = false; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 206 | * @var boolean |
||
| 207 | */ |
||
| 208 | protected $brokenOnWrite = false; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @config |
||
| 212 | * @var boolean Should dataobjects be validated before they are written? |
||
| 213 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 214 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 215 | * to only disable validation for very specific use cases. |
||
| 216 | */ |
||
| 217 | private static $validation_enabled = true; |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Static caches used by relevant functions. |
||
| 221 | */ |
||
| 222 | protected static $_cache_has_own_table = array(); |
||
| 223 | protected static $_cache_get_one; |
||
| 224 | protected static $_cache_get_class_ancestry; |
||
| 225 | protected static $_cache_field_labels = array(); |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Base fields which are not defined in static $db |
||
| 229 | * |
||
| 230 | * @config |
||
| 231 | * @var array |
||
| 232 | */ |
||
| 233 | private static $fixed_fields = array( |
||
| 234 | 'ID' => 'PrimaryKey', |
||
| 235 | 'ClassName' => 'DBClassName', |
||
| 236 | 'LastEdited' => 'DBDatetime', |
||
| 237 | 'Created' => 'DBDatetime', |
||
| 238 | ); |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Core dataobject extensions |
||
| 242 | * |
||
| 243 | * @config |
||
| 244 | * @var array |
||
| 245 | */ |
||
| 246 | private static $extensions = array( |
||
| 247 | 'AssetControl' => '\\SilverStripe\\Filesystem\\AssetControlExtension' |
||
| 248 | ); |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Override table name for this class. If ignored will default to FQN of class. |
||
| 252 | * This option is not inheritable, and must be set on each class. |
||
| 253 | * If left blank naming will default to the legacy (3.x) behaviour. |
||
| 254 | * |
||
| 255 | * @var string |
||
| 256 | */ |
||
| 257 | private static $table_name = null; |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Non-static relationship cache, indexed by component name. |
||
| 261 | */ |
||
| 262 | protected $components; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 266 | */ |
||
| 267 | protected $unsavedRelations; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get schema object |
||
| 271 | * |
||
| 272 | * @return DataObjectSchema |
||
| 273 | */ |
||
| 274 | public static function getSchema() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
| 280 | * "ID" will be included on every table. |
||
| 281 | * |
||
| 282 | * Composite DB field specifications are returned by reference if necessary, but not in the return |
||
| 283 | * array. |
||
| 284 | * |
||
| 285 | * Can be called directly on an object. E.g. Member::database_fields() |
||
| 286 | * |
||
| 287 | * @param string $class Class name to query from |
||
| 288 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 289 | */ |
||
| 290 | public static function database_fields($class = null) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
| 299 | * and {@link DataObject::$has_one}. Resolves instances of {@link DBComposite} |
||
| 300 | * into the actual database fields, rather than the name of the field which |
||
| 301 | * might not equate a database column. |
||
| 302 | * |
||
| 303 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
| 304 | * see {@link database_fields()}. |
||
| 305 | * |
||
| 306 | * Can be called directly on an object. E.g. Member::custom_database_fields() |
||
| 307 | * |
||
| 308 | * @uses DBComposite->compositeDatabaseFields() |
||
| 309 | * |
||
| 310 | * @param string $class Class name to query from |
||
| 311 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 312 | */ |
||
| 313 | public static function custom_database_fields($class = null) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the field class if the given db field on the class is a composite field. |
||
| 326 | * Will check all applicable ancestor classes and aggregate results. |
||
| 327 | * |
||
| 328 | * @param string $class Class to check |
||
| 329 | * @param string $name Field to check |
||
| 330 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
| 331 | * @return string|false Class spec name of composite field if it exists, or false if not |
||
| 332 | */ |
||
| 333 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 340 | * Will check all applicable ancestor classes and aggregate results. |
||
| 341 | * |
||
| 342 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
| 343 | * to aggregate. |
||
| 344 | * |
||
| 345 | * Includes composite has_one (Polymorphic) fields |
||
| 346 | * |
||
| 347 | * @param string $class Name of class to check |
||
| 348 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 349 | * @return array List of composite fields and their class spec |
||
| 350 | */ |
||
| 351 | public static function composite_fields($class = null, $aggregated = true) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Construct a new DataObject. |
||
| 361 | * |
||
| 362 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
| 363 | * field values. Normally this constructor is only used by the internal systems that get objects from the database. |
||
| 364 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 365 | * Singletons don't have their defaults set. |
||
| 366 | * @param DataModel $model |
||
| 367 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 368 | */ |
||
| 369 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set the DataModel |
||
| 445 | * @param DataModel $model |
||
| 446 | * @return DataObject $this |
||
| 447 | */ |
||
| 448 | public function setDataModel(DataModel $model) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Destroy all of this objects dependant objects and local caches. |
||
| 455 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 456 | */ |
||
| 457 | public function destroy() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Create a duplicate of this node. |
||
| 465 | * Note: now also duplicates relations. |
||
| 466 | * |
||
| 467 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 468 | * If this is true, it will create the duplicate in the database. |
||
| 469 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 470 | */ |
||
| 471 | public function duplicate($doWrite = true) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
| 488 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
| 489 | * automatically when adding the new relations. |
||
| 490 | * |
||
| 491 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 492 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 493 | * @return DataObject with the new many_many relations copied in |
||
| 494 | */ |
||
| 495 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Helper function to duplicate relations from one object to another |
||
| 518 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 519 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 520 | * @param string $name the name of the relation to duplicate (e.g. members) |
||
| 521 | */ |
||
| 522 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
| 536 | |||
| 537 | public function getObsoleteClassName() { |
||
| 541 | |||
| 542 | public function getClassName() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 550 | * Warning: This will produce an inconsistent record, as the object |
||
| 551 | * instance will not automatically switch to the new subclass. |
||
| 552 | * Please use {@link newClassInstance()} for this purpose, |
||
| 553 | * or destroy and reinstanciate the record. |
||
| 554 | * |
||
| 555 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 556 | * @return DataObject $this |
||
| 557 | */ |
||
| 558 | public function setClassName($className) { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Create a new instance of a different class from this object's record. |
||
| 569 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 570 | * it ensures that the instance of the class is a match for the className of the |
||
| 571 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 572 | * property manually before calling this method, as it will confuse change detection. |
||
| 573 | * |
||
| 574 | * If the new class is different to the original class, defaults are populated again |
||
| 575 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 576 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 577 | * we still need to repopulate the defaults. |
||
| 578 | * |
||
| 579 | * @param string $newClassName The name of the new class |
||
| 580 | * |
||
| 581 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 582 | */ |
||
| 583 | public function newClassInstance($newClassName) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Adds methods from the extensions. |
||
| 604 | * Called by Object::__construct() once per class. |
||
| 605 | */ |
||
| 606 | public function defineMethods() { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 648 | * The default behaviour for a DataObject is to return true if |
||
| 649 | * the object exists in the database, you can override this in subclasses. |
||
| 650 | * |
||
| 651 | * @return boolean true if this object exists |
||
| 652 | */ |
||
| 653 | public function exists() { |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Returns TRUE if all values (other than "ID") are |
||
| 659 | * considered empty (by weak boolean comparison). |
||
| 660 | * |
||
| 661 | * @return boolean |
||
| 662 | */ |
||
| 663 | public function isEmpty() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Pluralise this item given a specific count. |
||
| 684 | * |
||
| 685 | * E.g. "0 Pages", "1 File", "3 Images" |
||
| 686 | * |
||
| 687 | * @param string $count |
||
| 688 | * @param bool $prependNumber Include number in result. Defaults to true. |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | public function i18n_pluralise($count, $prependNumber = true) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get the user friendly singular name of this DataObject. |
||
| 702 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 703 | * this returns the class name. |
||
| 704 | * |
||
| 705 | * @return string User friendly singular name of this DataObject |
||
| 706 | */ |
||
| 707 | public function singular_name() { |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get the translated user friendly singular name of this DataObject |
||
| 718 | * same as singular_name() but runs it through the translating function |
||
| 719 | * |
||
| 720 | * Translating string is in the form: |
||
| 721 | * $this->class.SINGULARNAME |
||
| 722 | * Example: |
||
| 723 | * Page.SINGULARNAME |
||
| 724 | * |
||
| 725 | * @return string User friendly translated singular name of this DataObject |
||
| 726 | */ |
||
| 727 | public function i18n_singular_name() { |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get the user friendly plural name of this DataObject |
||
| 733 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 734 | * this returns a pluralised version of the class name. |
||
| 735 | * |
||
| 736 | * @return string User friendly plural name of this DataObject |
||
| 737 | */ |
||
| 738 | public function plural_name() { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Get the translated user friendly plural name of this DataObject |
||
| 753 | * Same as plural_name but runs it through the translation function |
||
| 754 | * Translation string is in the form: |
||
| 755 | * $this->class.PLURALNAME |
||
| 756 | * Example: |
||
| 757 | * Page.PLURALNAME |
||
| 758 | * |
||
| 759 | * @return string User friendly translated plural name of this DataObject |
||
| 760 | */ |
||
| 761 | public function i18n_plural_name() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Standard implementation of a title/label for a specific |
||
| 769 | * record. Tries to find properties 'Title' or 'Name', |
||
| 770 | * and falls back to the 'ID'. Useful to provide |
||
| 771 | * user-friendly identification of a record, e.g. in errormessages |
||
| 772 | * or UI-selections. |
||
| 773 | * |
||
| 774 | * Overload this method to have a more specialized implementation, |
||
| 775 | * e.g. for an Address record this could be: |
||
| 776 | * <code> |
||
| 777 | * function getTitle() { |
||
| 778 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 779 | * } |
||
| 780 | * </code> |
||
| 781 | * |
||
| 782 | * @return string |
||
| 783 | */ |
||
| 784 | public function getTitle() { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Returns the associated database record - in this case, the object itself. |
||
| 793 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 794 | * |
||
| 795 | * @return DataObject Associated database record |
||
| 796 | */ |
||
| 797 | public function data() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Convert this object to a map. |
||
| 803 | * |
||
| 804 | * @return array The data as a map. |
||
| 805 | */ |
||
| 806 | public function toMap() { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Return all currently fetched database fields. |
||
| 813 | * |
||
| 814 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 815 | * Obviously, this makes it a lot faster. |
||
| 816 | * |
||
| 817 | * @return array The data as a map. |
||
| 818 | */ |
||
| 819 | public function getQueriedDatabaseFields() { |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 825 | * |
||
| 826 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 827 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 828 | * |
||
| 829 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 830 | * the related objects that it alters. |
||
| 831 | * |
||
| 832 | * @param array $data A map of field name to data values to update. |
||
| 833 | * @return DataObject $this |
||
| 834 | */ |
||
| 835 | public function update($data) { |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Pass changes as a map, and try to |
||
| 885 | * get automatic casting for these fields. |
||
| 886 | * Doesn't write to the database. To write the data, |
||
| 887 | * use the write() method. |
||
| 888 | * |
||
| 889 | * @param array $data A map of field name to data values to update. |
||
| 890 | * @return DataObject $this |
||
| 891 | */ |
||
| 892 | public function castedUpdate($data) { |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Merges data and relations from another object of same class, |
||
| 901 | * without conflict resolution. Allows to specify which |
||
| 902 | * dataset takes priority in case its not empty. |
||
| 903 | * has_one-relations are just transferred with priority 'right'. |
||
| 904 | * has_many and many_many-relations are added regardless of priority. |
||
| 905 | * |
||
| 906 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 907 | * meaning they are not connected to the merged object any longer. |
||
| 908 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 909 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 910 | * Caution: Does not delete the merged object. |
||
| 911 | * Caution: Does now overwrite Created date on the original object. |
||
| 912 | * |
||
| 913 | * @param DataObject $rightObj |
||
| 914 | * @param string $priority left|right Determines who wins in case of a conflict (optional) |
||
| 915 | * @param bool $includeRelations Merge any existing relations (optional) |
||
| 916 | * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values. |
||
| 917 | * Only applicable with $priority='right'. (optional) |
||
| 918 | * @return Boolean |
||
| 919 | */ |
||
| 920 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Forces the record to think that all its data has changed. |
||
| 996 | * Doesn't write to the database. Only sets fields as changed |
||
| 997 | * if they are not already marked as changed. |
||
| 998 | * |
||
| 999 | * @return $this |
||
| 1000 | */ |
||
| 1001 | public function forceChange() { |
||
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Validate the current object. |
||
| 1024 | * |
||
| 1025 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1026 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1027 | * |
||
| 1028 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1029 | * and onAfterWrite() won't get called either. |
||
| 1030 | * |
||
| 1031 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1032 | * attempting a write, and respond appropriately if it isn't. |
||
| 1033 | * |
||
| 1034 | * @see {@link ValidationResult} |
||
| 1035 | * @return ValidationResult |
||
| 1036 | */ |
||
| 1037 | public function validate() { |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * Public accessor for {@see DataObject::validate()} |
||
| 1045 | * |
||
| 1046 | * @return ValidationResult |
||
| 1047 | */ |
||
| 1048 | public function doValidate() { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Event handler called before writing to the database. |
||
| 1055 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1056 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1057 | * |
||
| 1058 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1059 | * |
||
| 1060 | * @uses DataExtension->onBeforeWrite() |
||
| 1061 | */ |
||
| 1062 | protected function onBeforeWrite() { |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Event handler called after writing to the database. |
||
| 1071 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1072 | * $this->changed will have a record |
||
| 1073 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1074 | * |
||
| 1075 | * @uses DataExtension->onAfterWrite() |
||
| 1076 | */ |
||
| 1077 | protected function onAfterWrite() { |
||
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Event handler called before deleting from the database. |
||
| 1084 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1085 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1086 | * |
||
| 1087 | * @uses DataExtension->onBeforeDelete() |
||
| 1088 | */ |
||
| 1089 | protected function onBeforeDelete() { |
||
| 1095 | |||
| 1096 | protected function onAfterDelete() { |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * Load the default values in from the self::$defaults array. |
||
| 1102 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1103 | * Called by the constructor when creating new records. |
||
| 1104 | * |
||
| 1105 | * @uses DataExtension->populateDefaults() |
||
| 1106 | * @return DataObject $this |
||
| 1107 | */ |
||
| 1108 | public function populateDefaults() { |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * Determine validation of this object prior to write |
||
| 1142 | * |
||
| 1143 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1144 | */ |
||
| 1145 | protected function validateWrite() { |
||
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Prepare an object prior to write |
||
| 1168 | * |
||
| 1169 | * @throws ValidationException |
||
| 1170 | */ |
||
| 1171 | protected function preWrite() { |
||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Detects and updates all changes made to this object |
||
| 1190 | * |
||
| 1191 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1192 | * @return bool True if any changes are detected |
||
| 1193 | */ |
||
| 1194 | protected function updateChanges($forceChanges = false) |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1208 | * |
||
| 1209 | * @param string $baseTable Base table |
||
| 1210 | * @param string $now Timestamp to use for the current time |
||
| 1211 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1212 | * @param array $manipulation Manipulation to write to |
||
| 1213 | * @param string $class Class of table to manipulate |
||
| 1214 | */ |
||
| 1215 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1264 | * |
||
| 1265 | * Does nothing if an ID is already assigned for this record |
||
| 1266 | * |
||
| 1267 | * @param string $baseTable Base table |
||
| 1268 | * @param string $now Timestamp to use for the current time |
||
| 1269 | */ |
||
| 1270 | protected function writeBaseRecord($baseTable, $now) { |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Generate and write the database manipulation for all changed fields |
||
| 1285 | * |
||
| 1286 | * @param string $baseTable Base table |
||
| 1287 | * @param string $now Timestamp to use for the current time |
||
| 1288 | * @param bool $isNewRecord If this is a new record |
||
| 1289 | */ |
||
| 1290 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Writes all changes to this object to the database. |
||
| 1314 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1315 | * - All relevant tables will be updated. |
||
| 1316 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1317 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1318 | * |
||
| 1319 | * @uses DataExtension->augmentWrite() |
||
| 1320 | * |
||
| 1321 | * @param boolean $showDebug Show debugging information |
||
| 1322 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1323 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1324 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1325 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1326 | * {@link getManyManyComponents()} (Default: false) |
||
| 1327 | * @return int The ID of the record |
||
| 1328 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1329 | */ |
||
| 1330 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
| 1375 | |||
| 1376 | /** |
||
| 1377 | * Writes cached relation lists to the database, if possible |
||
| 1378 | */ |
||
| 1379 | public function writeRelations() { |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1393 | * same record. |
||
| 1394 | * |
||
| 1395 | * @param bool $recursive Recursively write components |
||
| 1396 | * @return DataObject $this |
||
| 1397 | */ |
||
| 1398 | public function writeComponents($recursive = false) { |
||
| 1406 | |||
| 1407 | /** |
||
| 1408 | * Delete this data object. |
||
| 1409 | * $this->onBeforeDelete() gets called. |
||
| 1410 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1411 | * @uses DataExtension->augmentSQL() |
||
| 1412 | */ |
||
| 1413 | public function delete() { |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Delete the record with the given ID. |
||
| 1444 | * |
||
| 1445 | * @param string $className The class name of the record to be deleted |
||
| 1446 | * @param int $id ID of record to be deleted |
||
| 1447 | */ |
||
| 1448 | public static function delete_by_id($className, $id) { |
||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Get the class ancestry, including the current class name. |
||
| 1459 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1460 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1461 | * will be the current class. |
||
| 1462 | * |
||
| 1463 | * @return array Class ancestry |
||
| 1464 | */ |
||
| 1465 | public function getClassAncestry() { |
||
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1471 | * If no component is available, an 'empty component' will be returned for |
||
| 1472 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1473 | * |
||
| 1474 | * @param string $componentName Name of the component |
||
| 1475 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1476 | * @throws Exception |
||
| 1477 | */ |
||
| 1478 | public function getComponent($componentName) { |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Returns a one-to-many relation as a HasManyList |
||
| 1549 | * |
||
| 1550 | * @param string $componentName Name of the component |
||
| 1551 | * @return HasManyList The components of the one-to-many relationship. |
||
| 1552 | */ |
||
| 1553 | public function getComponents($componentName) { |
||
| 1591 | |||
| 1592 | /** |
||
| 1593 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1594 | * |
||
| 1595 | * @param string $relationName Relation name. |
||
| 1596 | * @return string Class name, or null if not found. |
||
| 1597 | */ |
||
| 1598 | public function getRelationClass($relationName) { |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Given a relation name, determine the relation type |
||
| 1625 | * |
||
| 1626 | * @param string $component Name of component |
||
| 1627 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 1628 | */ |
||
| 1629 | public function getRelationType($component) { |
||
| 1639 | |||
| 1640 | /** |
||
| 1641 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 1642 | * side of the relation. |
||
| 1643 | * |
||
| 1644 | * Notes on behaviour: |
||
| 1645 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 1646 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 1647 | * - Cannot be used on polymorphic relationships |
||
| 1648 | * - Cannot be used on unsaved objects. |
||
| 1649 | * |
||
| 1650 | * @param string $remoteClass |
||
| 1651 | * @param string $remoteRelation |
||
| 1652 | * @return DataList|DataObject The component, either as a list or single object |
||
| 1653 | * @throws BadMethodCallException |
||
| 1654 | * @throws InvalidArgumentException |
||
| 1655 | */ |
||
| 1656 | public function inferReciprocalComponent($remoteClass, $remoteRelation) { |
||
| 1753 | |||
| 1754 | /** |
||
| 1755 | * Tries to find the database key on another object that is used to store a |
||
| 1756 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
| 1757 | * |
||
| 1758 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
| 1759 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
| 1760 | * |
||
| 1761 | * @param string $component Name of the relation on the current object pointing to the |
||
| 1762 | * remote object. |
||
| 1763 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
| 1764 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
| 1765 | * @return string |
||
| 1766 | * @throws Exception |
||
| 1767 | */ |
||
| 1768 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1839 | * @param string $componentName Name of the many-many component |
||
| 1840 | * @return ManyManyList The set of components |
||
| 1841 | */ |
||
| 1842 | public function getManyManyComponents($componentName) { |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1888 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1889 | * |
||
| 1890 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1891 | * their classes. |
||
| 1892 | */ |
||
| 1893 | public function hasOne() { |
||
| 1896 | |||
| 1897 | /** |
||
| 1898 | * Return data for a specific has_one component. |
||
| 1899 | * @param string $component |
||
| 1900 | * @return string|null |
||
| 1901 | */ |
||
| 1902 | public function hasOneComponent($component) { |
||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1915 | * their class name will be returned. |
||
| 1916 | * |
||
| 1917 | * @param string $component - Name of component |
||
| 1918 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1919 | * the field data stripped off. It defaults to TRUE. |
||
| 1920 | * @return string|array |
||
| 1921 | */ |
||
| 1922 | public function belongsTo($component = null, $classOnly = true) { |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * Return data for a specific belongs_to component. |
||
| 1942 | * @param string $component |
||
| 1943 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1944 | * the field data stripped off. It defaults to TRUE. |
||
| 1945 | * @return string|null |
||
| 1946 | */ |
||
| 1947 | public function belongsToComponent($component, $classOnly = true) { |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Return all of the database fields in this object |
||
| 1961 | * |
||
| 1962 | * @param string $fieldName Limit the output to a specific field name |
||
| 1963 | * @param bool $includeClass If returning a single column, prefix the column with the class name |
||
| 1964 | * in Table.Column(spec) format |
||
| 1965 | * @return array|string|null The database fields, or if searching a single field, |
||
| 1966 | * just this one field if found. Field will be a string in FieldClass(args) |
||
| 1967 | * format, or RecordClass.FieldClass(args) format if $includeClass is true |
||
| 1968 | */ |
||
| 1969 | public function db($fieldName = null, $includeClass = false) { |
||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 2004 | * relationships and their classes will be returned. |
||
| 2005 | * |
||
| 2006 | * @param string $component Deprecated - Name of component |
||
| 2007 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2008 | * the field data stripped off. It defaults to TRUE. |
||
| 2009 | * @return string|array|false |
||
| 2010 | */ |
||
| 2011 | public function hasMany($component = null, $classOnly = true) { |
||
| 2028 | |||
| 2029 | /** |
||
| 2030 | * Return data for a specific has_many component. |
||
| 2031 | * @param string $component |
||
| 2032 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2033 | * the field data stripped off. It defaults to TRUE. |
||
| 2034 | * @return string|null |
||
| 2035 | */ |
||
| 2036 | public function hasManyComponent($component, $classOnly = true) { |
||
| 2047 | |||
| 2048 | /** |
||
| 2049 | * Return the many-to-many extra fields specification. |
||
| 2050 | * |
||
| 2051 | * If you don't specify a component name, it returns all |
||
| 2052 | * extra fields for all components available. |
||
| 2053 | * |
||
| 2054 | * @return array|null |
||
| 2055 | */ |
||
| 2056 | public function manyManyExtraFields() { |
||
| 2059 | |||
| 2060 | /** |
||
| 2061 | * Return the many-to-many extra fields specification for a specific component. |
||
| 2062 | * @param string $component |
||
| 2063 | * @return array|null |
||
| 2064 | */ |
||
| 2065 | public function manyManyExtraFieldsForComponent($component) { |
||
| 2105 | |||
| 2106 | /** |
||
| 2107 | * Return information about a many-to-many component. |
||
| 2108 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 2109 | * components are returned. |
||
| 2110 | * |
||
| 2111 | * @see DataObject::manyManyComponent() |
||
| 2112 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 2113 | */ |
||
| 2114 | public function manyMany() { |
||
| 2120 | |||
| 2121 | /** |
||
| 2122 | * Return information about a specific many_many component. Returns a numeric array of: |
||
| 2123 | * array( |
||
| 2124 | * <classname>, The class that relation is defined in e.g. "Product" |
||
| 2125 | * <candidateName>, The target class of the relation e.g. "Category" |
||
| 2126 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
| 2127 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
| 2128 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
| 2129 | * ) |
||
| 2130 | * @param string $component The component name |
||
| 2131 | * @return array|null |
||
| 2132 | */ |
||
| 2133 | public function manyManyComponent($component) { |
||
| 2194 | |||
| 2195 | /** |
||
| 2196 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 2197 | * |
||
| 2198 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 2199 | * |
||
| 2200 | * @param $class |
||
| 2201 | * @return array or false |
||
| 2202 | */ |
||
| 2203 | public function database_extensions($class){ |
||
| 2212 | |||
| 2213 | /** |
||
| 2214 | * Generates a SearchContext to be used for building and processing |
||
| 2215 | * a generic search form for properties on this object. |
||
| 2216 | * |
||
| 2217 | * @return SearchContext |
||
| 2218 | */ |
||
| 2219 | public function getDefaultSearchContext() { |
||
| 2226 | |||
| 2227 | /** |
||
| 2228 | * Determine which properties on the DataObject are |
||
| 2229 | * searchable, and map them to their default {@link FormField} |
||
| 2230 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 2231 | * |
||
| 2232 | * Some additional logic is included for switching field labels, based on |
||
| 2233 | * how generic or specific the field type is. |
||
| 2234 | * |
||
| 2235 | * Used by {@link SearchContext}. |
||
| 2236 | * |
||
| 2237 | * @param array $_params |
||
| 2238 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 2239 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 2240 | * @return FieldList |
||
| 2241 | */ |
||
| 2242 | public function scaffoldSearchFields($_params = null) { |
||
| 2294 | |||
| 2295 | /** |
||
| 2296 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2297 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2298 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2299 | * |
||
| 2300 | * @uses FormScaffolder |
||
| 2301 | * |
||
| 2302 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2303 | * @return FieldList |
||
| 2304 | */ |
||
| 2305 | public function scaffoldFormFields($_params = null) { |
||
| 2326 | |||
| 2327 | /** |
||
| 2328 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2329 | * being called on extensions |
||
| 2330 | * |
||
| 2331 | * @param callable $callback The callback to execute |
||
| 2332 | */ |
||
| 2333 | protected function beforeUpdateCMSFields($callback) { |
||
| 2336 | |||
| 2337 | /** |
||
| 2338 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2339 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2340 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2341 | * generate this set. To customize, overload this method in a subclass |
||
| 2342 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2343 | * |
||
| 2344 | * <code> |
||
| 2345 | * class MyCustomClass extends DataObject { |
||
| 2346 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2347 | * |
||
| 2348 | * function getCMSFields() { |
||
| 2349 | * $fields = parent::getCMSFields(); |
||
| 2350 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2351 | * return $fields; |
||
| 2352 | * } |
||
| 2353 | * } |
||
| 2354 | * </code> |
||
| 2355 | * |
||
| 2356 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2357 | * |
||
| 2358 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2359 | */ |
||
| 2360 | public function getCMSFields() { |
||
| 2372 | |||
| 2373 | /** |
||
| 2374 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2375 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2376 | * |
||
| 2377 | * @return FieldList an Empty FieldList(); need to be overload by solid subclass |
||
| 2378 | */ |
||
| 2379 | public function getCMSActions() { |
||
| 2384 | |||
| 2385 | |||
| 2386 | /** |
||
| 2387 | * Used for simple frontend forms without relation editing |
||
| 2388 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2389 | * by default. To customize, either overload this method in your |
||
| 2390 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2391 | * |
||
| 2392 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2393 | * |
||
| 2394 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2395 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2396 | */ |
||
| 2397 | public function getFrontEndFields($params = null) { |
||
| 2403 | |||
| 2404 | /** |
||
| 2405 | * Gets the value of a field. |
||
| 2406 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2407 | * |
||
| 2408 | * @param string $field The name of the field |
||
| 2409 | * |
||
| 2410 | * @return mixed The field value |
||
| 2411 | */ |
||
| 2412 | public function getField($field) { |
||
| 2431 | |||
| 2432 | /** |
||
| 2433 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2434 | * |
||
| 2435 | * @param string $class Class to load the values from. Others are joined as required. |
||
| 2436 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2437 | * @return bool Flag if lazy loading succeeded |
||
| 2438 | */ |
||
| 2439 | protected function loadLazyFields($class = null) { |
||
| 2513 | |||
| 2514 | /** |
||
| 2515 | * Return the fields that have changed. |
||
| 2516 | * |
||
| 2517 | * The change level affects what the functions defines as "changed": |
||
| 2518 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2519 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2520 | * for example a change from 0 to null would not be included. |
||
| 2521 | * |
||
| 2522 | * Example return: |
||
| 2523 | * <code> |
||
| 2524 | * array( |
||
| 2525 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2526 | * ) |
||
| 2527 | * </code> |
||
| 2528 | * |
||
| 2529 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2530 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2531 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2532 | * @return array |
||
| 2533 | */ |
||
| 2534 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
| 2575 | |||
| 2576 | /** |
||
| 2577 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2578 | * since loading them from the database. |
||
| 2579 | * |
||
| 2580 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2581 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2582 | * @return boolean |
||
| 2583 | */ |
||
| 2584 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
| 2594 | |||
| 2595 | /** |
||
| 2596 | * Set the value of the field |
||
| 2597 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2598 | * |
||
| 2599 | * @param string $fieldName Name of the field |
||
| 2600 | * @param mixed $val New field value |
||
| 2601 | * @return $this |
||
| 2602 | */ |
||
| 2603 | public function setField($fieldName, $val) { |
||
| 2654 | |||
| 2655 | /** |
||
| 2656 | * Set the value of the field, using a casting object. |
||
| 2657 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2658 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2659 | * can be saved into the Image table. |
||
| 2660 | * |
||
| 2661 | * @param string $fieldName Name of the field |
||
| 2662 | * @param mixed $value New field value |
||
| 2663 | * @return $this |
||
| 2664 | */ |
||
| 2665 | public function setCastedField($fieldName, $value) { |
||
| 2678 | |||
| 2679 | /** |
||
| 2680 | * {@inheritdoc} |
||
| 2681 | */ |
||
| 2682 | public function castingHelper($field) { |
||
| 2700 | |||
| 2701 | /** |
||
| 2702 | * Returns true if the given field exists in a database column on any of |
||
| 2703 | * the objects tables and optionally look up a dynamic getter with |
||
| 2704 | * get<fieldName>(). |
||
| 2705 | * |
||
| 2706 | * @param string $field Name of the field |
||
| 2707 | * @return boolean True if the given field exists |
||
| 2708 | */ |
||
| 2709 | public function hasField($field) { |
||
| 2717 | |||
| 2718 | /** |
||
| 2719 | * Returns true if the given field exists as a database column |
||
| 2720 | * |
||
| 2721 | * @param string $field Name of the field |
||
| 2722 | * |
||
| 2723 | * @return boolean |
||
| 2724 | */ |
||
| 2725 | 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) { |
||
| 2777 | |||
| 2778 | /** |
||
| 2779 | * Returns true if the member is allowed to do the given action. |
||
| 2780 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2781 | * |
||
| 2782 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2783 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2784 | * in user. |
||
| 2785 | * @param array $context Additional $context to pass to extendedCan() |
||
| 2786 | * |
||
| 2787 | * @return boolean True if the the member is allowed to do the given action |
||
| 2788 | */ |
||
| 2789 | public function can($perm, $member = null, $context = array()) { |
||
| 2848 | |||
| 2849 | /** |
||
| 2850 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2851 | * expected to return one of three values: |
||
| 2852 | * |
||
| 2853 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2854 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2855 | * - NULL: Don't affect the outcome |
||
| 2856 | * |
||
| 2857 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2858 | * |
||
| 2859 | * <code> |
||
| 2860 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2861 | * if($extended !== null) return $extended; |
||
| 2862 | * else return $normalValue; |
||
| 2863 | * </code> |
||
| 2864 | * |
||
| 2865 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2866 | * @param Member|int $member |
||
| 2867 | * @param array $context Optional context |
||
| 2868 | * @return boolean|null |
||
| 2869 | */ |
||
| 2870 | public function extendedCan($methodName, $member, $context = array()) { |
||
| 2881 | |||
| 2882 | /** |
||
| 2883 | * @param Member $member |
||
| 2884 | * @return boolean |
||
| 2885 | */ |
||
| 2886 | public function canView($member = null) { |
||
| 2893 | |||
| 2894 | /** |
||
| 2895 | * @param Member $member |
||
| 2896 | * @return boolean |
||
| 2897 | */ |
||
| 2898 | public function canEdit($member = null) { |
||
| 2905 | |||
| 2906 | /** |
||
| 2907 | * @param Member $member |
||
| 2908 | * @return boolean |
||
| 2909 | */ |
||
| 2910 | public function canDelete($member = null) { |
||
| 2917 | |||
| 2918 | /** |
||
| 2919 | * @param Member $member |
||
| 2920 | * @param array $context Additional context-specific data which might |
||
| 2921 | * affect whether (or where) this object could be created. |
||
| 2922 | * @return boolean |
||
| 2923 | */ |
||
| 2924 | public function canCreate($member = null, $context = array()) { |
||
| 2931 | |||
| 2932 | /** |
||
| 2933 | * Debugging used by Debug::show() |
||
| 2934 | * |
||
| 2935 | * @return string HTML data representing this object |
||
| 2936 | */ |
||
| 2937 | public function debug() { |
||
| 2945 | |||
| 2946 | /** |
||
| 2947 | * Return the DBField object that represents the given field. |
||
| 2948 | * This works similarly to obj() with 2 key differences: |
||
| 2949 | * - it still returns an object even when the field has no value. |
||
| 2950 | * - it only matches fields and not methods |
||
| 2951 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 2952 | * |
||
| 2953 | * @param string $fieldName Name of the field |
||
| 2954 | * @return DBField The field as a DBField object |
||
| 2955 | */ |
||
| 2956 | public function dbObject($fieldName) { |
||
| 2979 | |||
| 2980 | /** |
||
| 2981 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 2982 | * |
||
| 2983 | * The path to the related field is specified with dot separated syntax |
||
| 2984 | * (eg: Parent.Child.Child.FieldName). |
||
| 2985 | * |
||
| 2986 | * @param string $fieldPath |
||
| 2987 | * |
||
| 2988 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 2989 | */ |
||
| 2990 | public function relObject($fieldPath) { |
||
| 3020 | |||
| 3021 | /** |
||
| 3022 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 3023 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 3024 | * |
||
| 3025 | * @param $fieldName string |
||
| 3026 | * @return string | null - will return null on a missing value |
||
| 3027 | */ |
||
| 3028 | public function relField($fieldName) { |
||
| 3063 | |||
| 3064 | /** |
||
| 3065 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 3066 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 3067 | * |
||
| 3068 | * @param string $className |
||
| 3069 | * @return string |
||
| 3070 | */ |
||
| 3071 | public function getReverseAssociation($className) { |
||
| 3087 | |||
| 3088 | /** |
||
| 3089 | * Return all objects matching the filter |
||
| 3090 | * sub-classes are automatically selected and included |
||
| 3091 | * |
||
| 3092 | * @param string $callerClass The class of objects to be returned |
||
| 3093 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3094 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3095 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 3096 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 3097 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 3098 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 3099 | * @param string $containerClass The container class to return the results in. |
||
| 3100 | * |
||
| 3101 | * @todo $containerClass is Ignored, why? |
||
| 3102 | * |
||
| 3103 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 3104 | */ |
||
| 3105 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
| 3142 | |||
| 3143 | |||
| 3144 | /** |
||
| 3145 | * Return the first item matching the given query. |
||
| 3146 | * All calls to get_one() are cached. |
||
| 3147 | * |
||
| 3148 | * @param string $callerClass The class of objects to be returned |
||
| 3149 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3150 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3151 | * @param boolean $cache Use caching |
||
| 3152 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 3153 | * |
||
| 3154 | * @return DataObject The first item matching the query |
||
| 3155 | */ |
||
| 3156 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
| 3182 | |||
| 3183 | /** |
||
| 3184 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 3185 | * Also clears any cached aggregate data. |
||
| 3186 | * |
||
| 3187 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 3188 | * When false will just clear session-local cached data |
||
| 3189 | * @return DataObject $this |
||
| 3190 | */ |
||
| 3191 | public function flushCache($persistent = true) { |
||
| 3207 | |||
| 3208 | /** |
||
| 3209 | * Flush the get_one global cache and destroy associated objects. |
||
| 3210 | */ |
||
| 3211 | public static function flush_and_destroy_cache() { |
||
| 3219 | |||
| 3220 | /** |
||
| 3221 | * Reset all global caches associated with DataObject. |
||
| 3222 | */ |
||
| 3223 | public static function reset() { |
||
| 3232 | |||
| 3233 | /** |
||
| 3234 | * Return the given element, searching by ID |
||
| 3235 | * |
||
| 3236 | * @param string $callerClass The class of the object to be returned |
||
| 3237 | * @param int $id The id of the element |
||
| 3238 | * @param boolean $cache See {@link get_one()} |
||
| 3239 | * |
||
| 3240 | * @return DataObject The element |
||
| 3241 | */ |
||
| 3242 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
| 3251 | |||
| 3252 | /** |
||
| 3253 | * Get the name of the base table for this object |
||
| 3254 | * |
||
| 3255 | * @return string |
||
| 3256 | */ |
||
| 3257 | public function baseTable() { |
||
| 3260 | |||
| 3261 | /** |
||
| 3262 | * Get the base class for this object |
||
| 3263 | * |
||
| 3264 | * @return string |
||
| 3265 | */ |
||
| 3266 | public function baseClass() { |
||
| 3269 | |||
| 3270 | /** |
||
| 3271 | * @var array Parameters used in the query that built this object. |
||
| 3272 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3273 | * run additional queries using the same context. |
||
| 3274 | */ |
||
| 3275 | protected $sourceQueryParams; |
||
| 3276 | |||
| 3277 | /** |
||
| 3278 | * @see $sourceQueryParams |
||
| 3279 | * @return array |
||
| 3280 | */ |
||
| 3281 | public function getSourceQueryParams() { |
||
| 3284 | |||
| 3285 | /** |
||
| 3286 | * Get list of parameters that should be inherited to relations on this object |
||
| 3287 | * |
||
| 3288 | * @return array |
||
| 3289 | */ |
||
| 3290 | public function getInheritableQueryParams() { |
||
| 3295 | |||
| 3296 | /** |
||
| 3297 | * @see $sourceQueryParams |
||
| 3298 | * @param array |
||
| 3299 | */ |
||
| 3300 | public function setSourceQueryParams($array) { |
||
| 3303 | |||
| 3304 | /** |
||
| 3305 | * @see $sourceQueryParams |
||
| 3306 | * @param string $key |
||
| 3307 | * @param string $value |
||
| 3308 | */ |
||
| 3309 | public function setSourceQueryParam($key, $value) { |
||
| 3312 | |||
| 3313 | /** |
||
| 3314 | * @see $sourceQueryParams |
||
| 3315 | * @param string $key |
||
| 3316 | * @return string |
||
| 3317 | */ |
||
| 3318 | public function getSourceQueryParam($key) { |
||
| 3324 | |||
| 3325 | //-------------------------------------------------------------------------------------------// |
||
| 3326 | |||
| 3327 | /** |
||
| 3328 | * Return the database indexes on this table. |
||
| 3329 | * This array is indexed by the name of the field with the index, and |
||
| 3330 | * the value is the type of index. |
||
| 3331 | */ |
||
| 3332 | public function databaseIndexes() { |
||
| 3357 | |||
| 3358 | /** |
||
| 3359 | * Check the database schema and update it as necessary. |
||
| 3360 | * |
||
| 3361 | * @uses DataExtension->augmentDatabase() |
||
| 3362 | */ |
||
| 3363 | public function requireTable() { |
||
| 3414 | |||
| 3415 | /** |
||
| 3416 | * Validate that the configured relations for this class use the correct syntaxes |
||
| 3417 | * @throws LogicException |
||
| 3418 | */ |
||
| 3419 | protected function validateModelDefinitions() { |
||
| 3450 | |||
| 3451 | /** |
||
| 3452 | * Add default records to database. This function is called whenever the |
||
| 3453 | * database is built, after the database tables have all been created. Overload |
||
| 3454 | * this to add default records when the database is built, but make sure you |
||
| 3455 | * call parent::requireDefaultRecords(). |
||
| 3456 | * |
||
| 3457 | * @uses DataExtension->requireDefaultRecords() |
||
| 3458 | */ |
||
| 3459 | public function requireDefaultRecords() { |
||
| 3477 | |||
| 3478 | /** |
||
| 3479 | * Get the default searchable fields for this object, as defined in the |
||
| 3480 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3481 | * data object, uses a default selection of summary fields. |
||
| 3482 | * |
||
| 3483 | * @return array |
||
| 3484 | */ |
||
| 3485 | public function searchableFields() { |
||
| 3559 | |||
| 3560 | /** |
||
| 3561 | * Get any user defined searchable fields labels that |
||
| 3562 | * exist. Allows overriding of default field names in the form |
||
| 3563 | * interface actually presented to the user. |
||
| 3564 | * |
||
| 3565 | * The reason for keeping this separate from searchable_fields, |
||
| 3566 | * which would be a logical place for this functionality, is to |
||
| 3567 | * avoid bloating and complicating the configuration array. Currently |
||
| 3568 | * much of this system is based on sensible defaults, and this property |
||
| 3569 | * would generally only be set in the case of more complex relationships |
||
| 3570 | * between data object being required in the search interface. |
||
| 3571 | * |
||
| 3572 | * Generates labels based on name of the field itself, if no static property |
||
| 3573 | * {@link self::field_labels} exists. |
||
| 3574 | * |
||
| 3575 | * @uses $field_labels |
||
| 3576 | * @uses FormField::name_to_label() |
||
| 3577 | * |
||
| 3578 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3579 | * |
||
| 3580 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3581 | */ |
||
| 3582 | public function fieldLabels($includerelations = true) { |
||
| 3617 | |||
| 3618 | /** |
||
| 3619 | * Get a human-readable label for a single field, |
||
| 3620 | * see {@link fieldLabels()} for more details. |
||
| 3621 | * |
||
| 3622 | * @uses fieldLabels() |
||
| 3623 | * @uses FormField::name_to_label() |
||
| 3624 | * |
||
| 3625 | * @param string $name Name of the field |
||
| 3626 | * @return string Label of the field |
||
| 3627 | */ |
||
| 3628 | public function fieldLabel($name) { |
||
| 3632 | |||
| 3633 | /** |
||
| 3634 | * Get the default summary fields for this object. |
||
| 3635 | * |
||
| 3636 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3637 | * |
||
| 3638 | * @return array |
||
| 3639 | */ |
||
| 3640 | public function summaryFields() { |
||
| 3673 | |||
| 3674 | /** |
||
| 3675 | * Defines a default list of filters for the search context. |
||
| 3676 | * |
||
| 3677 | * If a filter class mapping is defined on the data object, |
||
| 3678 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3679 | * {@link DBField} is used. |
||
| 3680 | * |
||
| 3681 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3682 | * |
||
| 3683 | * @return array |
||
| 3684 | */ |
||
| 3685 | public function defaultSearchFilters() { |
||
| 3704 | |||
| 3705 | /** |
||
| 3706 | * @return boolean True if the object is in the database |
||
| 3707 | */ |
||
| 3708 | public function isInDB() { |
||
| 3711 | |||
| 3712 | /* |
||
| 3713 | * @ignore |
||
| 3714 | */ |
||
| 3715 | private static $subclass_access = true; |
||
| 3716 | |||
| 3717 | /** |
||
| 3718 | * Temporarily disable subclass access in data object qeur |
||
| 3719 | */ |
||
| 3720 | public static function disable_subclass_access() { |
||
| 3726 | |||
| 3727 | //-------------------------------------------------------------------------------------------// |
||
| 3728 | |||
| 3729 | /** |
||
| 3730 | * Database field definitions. |
||
| 3731 | * This is a map from field names to field type. The field |
||
| 3732 | * type should be a class that extends . |
||
| 3733 | * @var array |
||
| 3734 | * @config |
||
| 3735 | */ |
||
| 3736 | private static $db = null; |
||
| 3737 | |||
| 3738 | /** |
||
| 3739 | * Use a casting object for a field. This is a map from |
||
| 3740 | * field name to class name of the casting object. |
||
| 3741 | * |
||
| 3742 | * @var array |
||
| 3743 | */ |
||
| 3744 | private static $casting = array( |
||
| 3745 | "Title" => 'Text', |
||
| 3746 | ); |
||
| 3747 | |||
| 3748 | /** |
||
| 3749 | * Specify custom options for a CREATE TABLE call. |
||
| 3750 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3751 | * All options have to be keyed for a specific database implementation, |
||
| 3752 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3753 | * |
||
| 3754 | * <code> |
||
| 3755 | * array( |
||
| 3756 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3757 | * ) |
||
| 3758 | * </code> |
||
| 3759 | * |
||
| 3760 | * Caution: This API is experimental, and might not be |
||
| 3761 | * included in the next major release. Please use with care. |
||
| 3762 | * |
||
| 3763 | * @var array |
||
| 3764 | * @config |
||
| 3765 | */ |
||
| 3766 | private static $create_table_options = array( |
||
| 3767 | 'SilverStripe\ORM\Connect\MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3768 | ); |
||
| 3769 | |||
| 3770 | /** |
||
| 3771 | * If a field is in this array, then create a database index |
||
| 3772 | * on that field. This is a map from fieldname to index type. |
||
| 3773 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3774 | * |
||
| 3775 | * @var array |
||
| 3776 | * @config |
||
| 3777 | */ |
||
| 3778 | private static $indexes = null; |
||
| 3779 | |||
| 3780 | /** |
||
| 3781 | * Inserts standard column-values when a DataObject |
||
| 3782 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3783 | * This is a map from fieldname to default value. |
||
| 3784 | * |
||
| 3785 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3786 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3787 | * or false in your subclass. Setting it to null won't work. |
||
| 3788 | * |
||
| 3789 | * @var array |
||
| 3790 | * @config |
||
| 3791 | */ |
||
| 3792 | private static $defaults = null; |
||
| 3793 | |||
| 3794 | /** |
||
| 3795 | * Multidimensional array which inserts default data into the database |
||
| 3796 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3797 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3798 | * behaviour such as publishing and ParentNodes. |
||
| 3799 | * |
||
| 3800 | * Example: |
||
| 3801 | * array( |
||
| 3802 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3803 | * array('Title' => "DefaultPage2") |
||
| 3804 | * ). |
||
| 3805 | * |
||
| 3806 | * @var array |
||
| 3807 | * @config |
||
| 3808 | */ |
||
| 3809 | private static $default_records = null; |
||
| 3810 | |||
| 3811 | /** |
||
| 3812 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3813 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3814 | * |
||
| 3815 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3816 | * |
||
| 3817 | * @var array |
||
| 3818 | * @config |
||
| 3819 | */ |
||
| 3820 | private static $has_one = null; |
||
| 3821 | |||
| 3822 | /** |
||
| 3823 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3824 | * |
||
| 3825 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3826 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3827 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3828 | * |
||
| 3829 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3830 | * |
||
| 3831 | * @var array |
||
| 3832 | * @config |
||
| 3833 | */ |
||
| 3834 | private static $belongs_to; |
||
| 3835 | |||
| 3836 | /** |
||
| 3837 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3838 | * |
||
| 3839 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3840 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3841 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3842 | * which foreign key to use. |
||
| 3843 | * |
||
| 3844 | * @var array |
||
| 3845 | * @config |
||
| 3846 | */ |
||
| 3847 | private static $has_many = null; |
||
| 3848 | |||
| 3849 | /** |
||
| 3850 | * many-many relationship definitions. |
||
| 3851 | * This is a map from component name to data type. |
||
| 3852 | * @var array |
||
| 3853 | * @config |
||
| 3854 | */ |
||
| 3855 | private static $many_many = null; |
||
| 3856 | |||
| 3857 | /** |
||
| 3858 | * Extra fields to include on the connecting many-many table. |
||
| 3859 | * This is a map from field name to field type. |
||
| 3860 | * |
||
| 3861 | * Example code: |
||
| 3862 | * <code> |
||
| 3863 | * public static $many_many_extraFields = array( |
||
| 3864 | * 'Members' => array( |
||
| 3865 | * 'Role' => 'Varchar(100)' |
||
| 3866 | * ) |
||
| 3867 | * ); |
||
| 3868 | * </code> |
||
| 3869 | * |
||
| 3870 | * @var array |
||
| 3871 | * @config |
||
| 3872 | */ |
||
| 3873 | private static $many_many_extraFields = null; |
||
| 3874 | |||
| 3875 | /** |
||
| 3876 | * The inverse side of a many-many relationship. |
||
| 3877 | * This is a map from component name to data type. |
||
| 3878 | * @var array |
||
| 3879 | * @config |
||
| 3880 | */ |
||
| 3881 | private static $belongs_many_many = null; |
||
| 3882 | |||
| 3883 | /** |
||
| 3884 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3885 | * clause of a SQL query if no other sort expression is provided. |
||
| 3886 | * @var string |
||
| 3887 | * @config |
||
| 3888 | */ |
||
| 3889 | private static $default_sort = null; |
||
| 3890 | |||
| 3891 | /** |
||
| 3892 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3893 | * search interface. |
||
| 3894 | * |
||
| 3895 | * Overriding the default filter, with a custom defined filter: |
||
| 3896 | * <code> |
||
| 3897 | * static $searchable_fields = array( |
||
| 3898 | * "Name" => "PartialMatchFilter" |
||
| 3899 | * ); |
||
| 3900 | * </code> |
||
| 3901 | * |
||
| 3902 | * Overriding the default form fields, with a custom defined field. |
||
| 3903 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3904 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3905 | * <code> |
||
| 3906 | * static $searchable_fields = array( |
||
| 3907 | * "Name" => array( |
||
| 3908 | * "field" => "TextField" |
||
| 3909 | * ) |
||
| 3910 | * ); |
||
| 3911 | * </code> |
||
| 3912 | * |
||
| 3913 | * Overriding the default form field, filter and title: |
||
| 3914 | * <code> |
||
| 3915 | * static $searchable_fields = array( |
||
| 3916 | * "Organisation.ZipCode" => array( |
||
| 3917 | * "field" => "TextField", |
||
| 3918 | * "filter" => "PartialMatchFilter", |
||
| 3919 | * "title" => 'Organisation ZIP' |
||
| 3920 | * ) |
||
| 3921 | * ); |
||
| 3922 | * </code> |
||
| 3923 | * @config |
||
| 3924 | */ |
||
| 3925 | private static $searchable_fields = null; |
||
| 3926 | |||
| 3927 | /** |
||
| 3928 | * User defined labels for searchable_fields, used to override |
||
| 3929 | * default display in the search form. |
||
| 3930 | * @config |
||
| 3931 | */ |
||
| 3932 | private static $field_labels = null; |
||
| 3933 | |||
| 3934 | /** |
||
| 3935 | * Provides a default list of fields to be used by a 'summary' |
||
| 3936 | * view of this object. |
||
| 3937 | * @config |
||
| 3938 | */ |
||
| 3939 | private static $summary_fields = null; |
||
| 3940 | |||
| 3941 | /** |
||
| 3942 | * Collect all static properties on the object |
||
| 3943 | * which contain natural language, and need to be translated. |
||
| 3944 | * The full entity name is composed from the class name and a custom identifier. |
||
| 3945 | * |
||
| 3946 | * @return array A numerical array which contains one or more entities in array-form. |
||
| 3947 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
| 3948 | * $entity, $string, $priority, $context. |
||
| 3949 | */ |
||
| 3950 | public function provideI18nEntities() { |
||
| 3968 | |||
| 3969 | /** |
||
| 3970 | * Returns true if the given method/parameter has a value |
||
| 3971 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 3972 | * |
||
| 3973 | * @param string $field The field name |
||
| 3974 | * @param array $arguments |
||
| 3975 | * @param bool $cache |
||
| 3976 | * @return boolean |
||
| 3977 | */ |
||
| 3978 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 3986 | |||
| 3987 | } |
||
| 3988 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.