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