| Total Complexity | 578 |
| Total Lines | 4309 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 111 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider, Resettable |
||
| 112 | { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Human-readable singular name. |
||
| 116 | * @var string |
||
| 117 | * @config |
||
| 118 | */ |
||
| 119 | private static $singular_name = null; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Human-readable plural name |
||
| 123 | * @var string |
||
| 124 | * @config |
||
| 125 | */ |
||
| 126 | private static $plural_name = null; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Allow API access to this object? |
||
| 130 | * @todo Define the options that can be set here |
||
| 131 | * @config |
||
| 132 | */ |
||
| 133 | private static $api_access = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Allows specification of a default value for the ClassName field. |
||
| 137 | * Configure this value only in subclasses of DataObject. |
||
| 138 | * |
||
| 139 | * @config |
||
| 140 | * @var string |
||
| 141 | */ |
||
| 142 | private static $default_classname = null; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @deprecated 4.0.0:5.0.0 |
||
| 146 | * @var bool |
||
| 147 | */ |
||
| 148 | public $destroyed = false; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 152 | * |
||
| 153 | * Use {@link toMap()} if you want an array representation |
||
| 154 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 155 | * |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected $record; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * If selected through a many_many through relation, this is the instance of the through record |
||
| 162 | * |
||
| 163 | * @var DataObject |
||
| 164 | */ |
||
| 165 | protected $joinRecord; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 169 | */ |
||
| 170 | const CHANGE_NONE = 0; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 174 | * (before !== after && before == after) |
||
| 175 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 176 | * Value changes are by nature also considered strict changes. |
||
| 177 | */ |
||
| 178 | const CHANGE_STRICT = 1; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Represents a field that has changed the loosely defined value |
||
| 182 | * (before != after, thus, before !== after)) |
||
| 183 | * E.g. change false to true, but not false to 0 |
||
| 184 | */ |
||
| 185 | const CHANGE_VALUE = 2; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Value for 2nd argument to constructor, indicating that a new record is being created |
||
| 189 | * Setters will be called on fields passed, and defaults will be populated |
||
| 190 | */ |
||
| 191 | const CREATE_OBJECT = 0; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Value for 2nd argument to constructor, indicating that a record is a singleton representing the whole type, |
||
| 195 | * e.g. to call requireTable() in dev/build |
||
| 196 | * Defaults will not be populated and data passed will be ignored |
||
| 197 | */ |
||
| 198 | const CREATE_SINGLETON = 1; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Value for 2nd argument to constructor, indicating that a record is being hydrated from the database |
||
| 202 | * Setter methods are not called, and population via private static $defaults will not occur. |
||
| 203 | */ |
||
| 204 | const CREATE_HYDRATED = 2; |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Value for 2nd argument to constructor, indicating that a record is being hydrated from memory. This can be used |
||
| 208 | * to initialised a record that doesn't yet have an ID. Setter methods are not called, and population via private |
||
| 209 | * static $defaults will not occur. |
||
| 210 | */ |
||
| 211 | const CREATE_MEMORY_HYDRATED = 3; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * An array indexed by fieldname, true if the field has been changed. |
||
| 215 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 216 | * the changed state. |
||
| 217 | * |
||
| 218 | * @var array |
||
| 219 | */ |
||
| 220 | private $changed = []; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * A flag to indicate that a "strict" change of the entire record been forced |
||
| 224 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 225 | * the changed state. |
||
| 226 | * |
||
| 227 | * @var boolean |
||
| 228 | */ |
||
| 229 | private $changeForced = false; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * The database record (in the same format as $record), before |
||
| 233 | * any changes. |
||
| 234 | * @var array |
||
| 235 | */ |
||
| 236 | protected $original = []; |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 240 | * @var boolean |
||
| 241 | */ |
||
| 242 | protected $brokenOnDelete = false; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 246 | * @var boolean |
||
| 247 | */ |
||
| 248 | protected $brokenOnWrite = false; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Should dataobjects be validated before they are written? |
||
| 252 | * |
||
| 253 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 254 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 255 | * to only disable validation for very specific use cases. |
||
| 256 | * |
||
| 257 | * @config |
||
| 258 | * @var boolean |
||
| 259 | */ |
||
| 260 | private static $validation_enabled = true; |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Static caches used by relevant functions. |
||
| 264 | * |
||
| 265 | * @var array |
||
| 266 | */ |
||
| 267 | protected static $_cache_get_one; |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Cache of field labels |
||
| 271 | * |
||
| 272 | * @var array |
||
| 273 | */ |
||
| 274 | protected static $_cache_field_labels = []; |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Base fields which are not defined in static $db |
||
| 278 | * |
||
| 279 | * @config |
||
| 280 | * @var array |
||
| 281 | */ |
||
| 282 | private static $fixed_fields = [ |
||
| 283 | 'ID' => 'PrimaryKey', |
||
| 284 | 'ClassName' => 'DBClassName', |
||
| 285 | 'LastEdited' => 'DBDatetime', |
||
| 286 | 'Created' => 'DBDatetime', |
||
| 287 | ]; |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Override table name for this class. If ignored will default to FQN of class. |
||
| 291 | * This option is not inheritable, and must be set on each class. |
||
| 292 | * If left blank naming will default to the legacy (3.x) behaviour. |
||
| 293 | * |
||
| 294 | * @var string |
||
| 295 | */ |
||
| 296 | private static $table_name = null; |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Non-static relationship cache, indexed by component name. |
||
| 300 | * |
||
| 301 | * @var DataObject[] |
||
| 302 | */ |
||
| 303 | protected $components = []; |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 307 | * |
||
| 308 | * @var UnsavedRelationList[] |
||
| 309 | */ |
||
| 310 | protected $unsavedRelations; |
||
| 311 | |||
| 312 | /** |
||
| 313 | * List of relations that should be cascade deleted, similar to `owns` |
||
| 314 | * Note: This will trigger delete on many_many objects, not only the mapping table. |
||
| 315 | * For many_many through you can specify the components you want to delete separately |
||
| 316 | * (many_many or has_many sub-component) |
||
| 317 | * |
||
| 318 | * @config |
||
| 319 | * @var array |
||
| 320 | */ |
||
| 321 | private static $cascade_deletes = []; |
||
| 322 | |||
| 323 | /** |
||
| 324 | * List of relations that should be cascade duplicate. |
||
| 325 | * many_many duplications are shallow only. |
||
| 326 | * |
||
| 327 | * Note: If duplicating a many_many through you should refer to the |
||
| 328 | * has_many intermediary relation instead, otherwise extra fields |
||
| 329 | * will be omitted from the duplicated relation. |
||
| 330 | * |
||
| 331 | * @var array |
||
| 332 | */ |
||
| 333 | private static $cascade_duplicates = []; |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get schema object |
||
| 337 | * |
||
| 338 | * @return DataObjectSchema |
||
| 339 | */ |
||
| 340 | public static function getSchema() |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Construct a new DataObject. |
||
| 347 | * |
||
| 348 | * @param array $record Initial record content, or rehydrated record content, depending on $creationType |
||
| 349 | * @param int|boolean $creationType Set to DataObject::CREATE_OBJECT, DataObject::CREATE_HYDRATED, |
||
| 350 | * DataObject::CREATE_MEMORY_HYDRATED or DataObject::CREATE_SINGLETON. Used by Silverstripe internals and best |
||
| 351 | * left as the default by regular users. |
||
| 352 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 353 | */ |
||
| 354 | public function __construct($record = [], $creationType = self::CREATE_OBJECT, $queryParams = []) |
||
| 437 | } |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Constructor hydration logic for CREATE_HYDRATED and CREATE_MEMORY_HYDRATED. |
||
| 442 | * @param array $record |
||
| 443 | * @param bool $mustHaveID If true, an exception will be thrown if $record doesn't have an ID. |
||
| 444 | */ |
||
| 445 | private function hydrate(array $record, bool $mustHaveID) |
||
| 446 | { |
||
| 447 | if ($mustHaveID && empty($record['ID'])) { |
||
| 448 | // CREATE_HYDRATED requires an ID to be included in the record |
||
| 449 | throw new \InvalidArgumentException( |
||
| 450 | "Hydrated records must be passed a record array including an ID." |
||
| 451 | ); |
||
| 452 | } elseif (empty($record['ID'])) { |
||
| 453 | // CREATE_MEMORY_HYDRATED implicitely set the record ID to 0 if not provided |
||
| 454 | $record['ID'] = 0; |
||
| 455 | } |
||
| 456 | |||
| 457 | $this->record = $record; |
||
| 458 | |||
| 459 | // Identify fields that should be lazy loaded, but only on existing records |
||
| 460 | // Get all field specs scoped to class for later lazy loading |
||
| 461 | $fields = static::getSchema()->fieldSpecs( |
||
| 462 | static::class, |
||
| 463 | DataObjectSchema::INCLUDE_CLASS | DataObjectSchema::DB_ONLY |
||
| 464 | ); |
||
| 465 | |||
| 466 | foreach ($fields as $field => $fieldSpec) { |
||
| 467 | $fieldClass = strtok($fieldSpec, "."); |
||
| 468 | if (!array_key_exists($field, $record)) { |
||
| 469 | $this->record[$field . '_Lazy'] = $fieldClass; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | // Extension point to hydrate additional fields into this object during construction. |
||
| 474 | // Return an array of field names => raw values from your augmentHydrateFields extension method. |
||
| 475 | $extendedAdditionalFields = $this->extend('augmentHydrateFields'); |
||
| 476 | foreach ($extendedAdditionalFields as $additionalFields) { |
||
| 477 | foreach ($additionalFields as $field => $value) { |
||
| 478 | $this->record[$field] = $value; |
||
| 479 | |||
| 480 | // If a corresponding lazy-load field exists, remove it as the value has been provided |
||
| 481 | $lazyName = $field . '_Lazy'; |
||
| 482 | if (array_key_exists($lazyName, $this->record)) { |
||
| 483 | unset($this->record[$lazyName]); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | $this->original = $this->record; |
||
| 489 | $this->changed = []; |
||
| 490 | $this->changeForced = false; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Destroy all of this objects dependant objects and local caches. |
||
| 495 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 496 | */ |
||
| 497 | public function destroy() |
||
| 498 | { |
||
| 499 | $this->flushCache(false); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Create a duplicate of this node. Can duplicate many_many relations |
||
| 504 | * |
||
| 505 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 506 | * If this is true, it will create the duplicate in the database. |
||
| 507 | * @param array|null|false $relations List of relations to duplicate. |
||
| 508 | * Will default to `cascade_duplicates` if null. |
||
| 509 | * Set to 'false' to force none. |
||
| 510 | * Set to specific array of names to duplicate to override these. |
||
| 511 | * Note: If using versioned, this will additionally failover to `owns` config. |
||
| 512 | * @return static A duplicate of this node. The exact type will be the type of this node. |
||
| 513 | */ |
||
| 514 | public function duplicate($doWrite = true, $relations = null) |
||
| 515 | { |
||
| 516 | // Handle legacy behaviour |
||
| 517 | if (is_string($relations) || $relations === true) { |
||
| 518 | if ($relations === true) { |
||
| 519 | $relations = 'many_many'; |
||
| 520 | } |
||
| 521 | Deprecation::notice('5.0', 'Use cascade_duplicates config instead of providing a string to duplicate()'); |
||
| 522 | $relations = array_keys($this->config()->get($relations)) ?: []; |
||
| 523 | } |
||
| 524 | |||
| 525 | // Get duplicates |
||
| 526 | if ($relations === null) { |
||
| 527 | $relations = $this->config()->get('cascade_duplicates'); |
||
| 528 | // Remove any duplicate entries before duplicating them |
||
| 529 | if (is_array($relations)) { |
||
| 530 | $relations = array_unique($relations); |
||
| 531 | } |
||
| 532 | } |
||
| 533 | |||
| 534 | // Create unsaved raw duplicate |
||
| 535 | $map = $this->toMap(); |
||
| 536 | unset($map['Created']); |
||
| 537 | /** @var static $clone */ |
||
| 538 | $clone = Injector::inst()->create(static::class, $map, false, $this->getSourceQueryParams()); |
||
| 539 | $clone->ID = 0; |
||
| 540 | |||
| 541 | // Note: Extensions such as versioned may update $relations here |
||
| 542 | $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite, $relations); |
||
| 543 | if ($relations) { |
||
| 544 | $this->duplicateRelations($this, $clone, $relations); |
||
| 545 | } |
||
| 546 | if ($doWrite) { |
||
| 547 | $clone->write(); |
||
| 548 | } |
||
| 549 | $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite, $relations); |
||
| 550 | |||
| 551 | return $clone; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Copies the given relations from this object to the destination |
||
| 556 | * |
||
| 557 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 558 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 559 | * @param array $relations List of relations |
||
| 560 | */ |
||
| 561 | protected function duplicateRelations($sourceObject, $destinationObject, $relations) |
||
| 562 | { |
||
| 563 | // Get list of duplicable relation types |
||
| 564 | $manyMany = $sourceObject->manyMany(); |
||
| 565 | $hasMany = $sourceObject->hasMany(); |
||
| 566 | $hasOne = $sourceObject->hasOne(); |
||
| 567 | $belongsTo = $sourceObject->belongsTo(); |
||
| 568 | |||
| 569 | // Duplicate each relation based on type |
||
| 570 | foreach ($relations as $relation) { |
||
| 571 | switch (true) { |
||
| 572 | case array_key_exists($relation, $manyMany): { |
||
| 573 | $this->duplicateManyManyRelation($sourceObject, $destinationObject, $relation); |
||
| 574 | break; |
||
| 575 | } |
||
| 576 | case array_key_exists($relation, $hasMany): { |
||
| 577 | $this->duplicateHasManyRelation($sourceObject, $destinationObject, $relation); |
||
| 578 | break; |
||
| 579 | } |
||
| 580 | case array_key_exists($relation, $hasOne): { |
||
| 581 | $this->duplicateHasOneRelation($sourceObject, $destinationObject, $relation); |
||
| 582 | break; |
||
| 583 | } |
||
| 584 | case array_key_exists($relation, $belongsTo): { |
||
| 585 | $this->duplicateBelongsToRelation($sourceObject, $destinationObject, $relation); |
||
| 586 | break; |
||
| 587 | } |
||
| 588 | default: { |
||
| 589 | $sourceType = get_class($sourceObject); |
||
| 590 | throw new InvalidArgumentException( |
||
| 591 | "Cannot duplicate unknown relation {$relation} on parent type {$sourceType}" |
||
| 592 | ); |
||
| 593 | } |
||
| 594 | } |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object. |
||
| 600 | * |
||
| 601 | * @deprecated 4.1.0:5.0.0 Use duplicateRelations() instead |
||
| 602 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 603 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 604 | * @param bool|string $filter |
||
| 605 | */ |
||
| 606 | protected function duplicateManyManyRelations($sourceObject, $destinationObject, $filter) |
||
| 607 | { |
||
| 608 | Deprecation::notice('5.0', 'Use duplicateRelations() instead'); |
||
| 609 | |||
| 610 | // Get list of relations to duplicate |
||
| 611 | if ($filter === 'many_many' || $filter === 'belongs_many_many') { |
||
| 612 | $relations = $sourceObject->config()->get($filter); |
||
| 613 | } elseif ($filter === true) { |
||
| 614 | $relations = $sourceObject->manyMany(); |
||
| 615 | } else { |
||
| 616 | throw new InvalidArgumentException("Invalid many_many duplication filter"); |
||
| 617 | } |
||
| 618 | foreach ($relations as $manyManyName => $type) { |
||
| 619 | $this->duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName); |
||
| 620 | } |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Duplicates a single many_many relation from one object to another. |
||
| 625 | * |
||
| 626 | * @param DataObject $sourceObject |
||
| 627 | * @param DataObject $destinationObject |
||
| 628 | * @param string $relation |
||
| 629 | */ |
||
| 630 | protected function duplicateManyManyRelation($sourceObject, $destinationObject, $relation) |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Duplicates a single many_many relation from one object to another. |
||
| 654 | * |
||
| 655 | * @param DataObject $sourceObject |
||
| 656 | * @param DataObject $destinationObject |
||
| 657 | * @param string $relation |
||
| 658 | */ |
||
| 659 | protected function duplicateHasManyRelation($sourceObject, $destinationObject, $relation) |
||
| 660 | { |
||
| 661 | // Copy all components from source to destination |
||
| 662 | $source = $sourceObject->getComponents($relation); |
||
| 663 | $dest = $destinationObject->getComponents($relation); |
||
| 664 | |||
| 665 | /** @var DataObject $item */ |
||
| 666 | foreach ($source as $item) { |
||
| 667 | // Don't write on duplicate; Wait until ParentID is available later. |
||
| 668 | // writeRelations() will eventually write these records when converting |
||
| 669 | // from UnsavedRelationList |
||
| 670 | $clonedItem = $item->duplicate(false); |
||
| 671 | $dest->add($clonedItem); |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Duplicates a single has_one relation from one object to another. |
||
| 677 | * Note: Child object will be force written. |
||
| 678 | * |
||
| 679 | * @param DataObject $sourceObject |
||
| 680 | * @param DataObject $destinationObject |
||
| 681 | * @param string $relation |
||
| 682 | */ |
||
| 683 | protected function duplicateHasOneRelation($sourceObject, $destinationObject, $relation) |
||
| 684 | { |
||
| 685 | // Check if original object exists |
||
| 686 | $item = $sourceObject->getComponent($relation); |
||
| 687 | if (!$item->isInDB()) { |
||
| 688 | return; |
||
| 689 | } |
||
| 690 | |||
| 691 | $clonedItem = $item->duplicate(false); |
||
| 692 | $destinationObject->setComponent($relation, $clonedItem); |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Duplicates a single belongs_to relation from one object to another. |
||
| 697 | * Note: This will force a write on both parent / child objects. |
||
| 698 | * |
||
| 699 | * @param DataObject $sourceObject |
||
| 700 | * @param DataObject $destinationObject |
||
| 701 | * @param string $relation |
||
| 702 | */ |
||
| 703 | protected function duplicateBelongsToRelation($sourceObject, $destinationObject, $relation) |
||
| 704 | { |
||
| 705 | // Check if original object exists |
||
| 706 | $item = $sourceObject->getComponent($relation); |
||
| 707 | if (!$item->isInDB()) { |
||
| 708 | return; |
||
| 709 | } |
||
| 710 | |||
| 711 | $clonedItem = $item->duplicate(false); |
||
| 712 | $destinationObject->setComponent($relation, $clonedItem); |
||
| 713 | // After $clonedItem is assigned the appropriate FieldID / FieldClass, force write |
||
| 714 | // @todo Write this component in onAfterWrite instead, assigning the FieldID then |
||
| 715 | // https://github.com/silverstripe/silverstripe-framework/issues/7818 |
||
| 716 | $clonedItem->write(); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Return obsolete class name, if this is no longer a valid class |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | public function getObsoleteClassName() |
||
| 725 | { |
||
| 726 | $className = $this->getField("ClassName"); |
||
| 727 | if (!ClassInfo::exists($className)) { |
||
| 728 | return $className; |
||
| 729 | } |
||
| 730 | return null; |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Gets name of this class |
||
| 735 | * |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | public function getClassName() |
||
| 739 | { |
||
| 740 | $className = $this->getField("ClassName"); |
||
| 741 | if (!ClassInfo::exists($className)) { |
||
| 742 | return static::class; |
||
| 743 | } |
||
| 744 | return $className; |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 749 | * Warning: This will produce an inconsistent record, as the object |
||
| 750 | * instance will not automatically switch to the new subclass. |
||
| 751 | * Please use {@link newClassInstance()} for this purpose, |
||
| 752 | * or destroy and reinstanciate the record. |
||
| 753 | * |
||
| 754 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 755 | * @return $this |
||
| 756 | */ |
||
| 757 | public function setClassName($className) |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Create a new instance of a different class from this object's record. |
||
| 771 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 772 | * it ensures that the instance of the class is a match for the className of the |
||
| 773 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 774 | * property manually before calling this method, as it will confuse change detection. |
||
| 775 | * |
||
| 776 | * If the new class is different to the original class, defaults are populated again |
||
| 777 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 778 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 779 | * we still need to repopulate the defaults. |
||
| 780 | * |
||
| 781 | * @param string $newClassName The name of the new class |
||
| 782 | * |
||
| 783 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 784 | */ |
||
| 785 | public function newClassInstance($newClassName) |
||
| 786 | { |
||
| 787 | if (!is_subclass_of($newClassName, self::class)) { |
||
| 788 | throw new InvalidArgumentException("$newClassName is not a valid subclass of DataObject"); |
||
| 789 | } |
||
| 790 | |||
| 791 | $originalClass = $this->ClassName; |
||
| 792 | |||
| 793 | /** @var DataObject $newInstance */ |
||
| 794 | $newInstance = Injector::inst()->create($newClassName, $this->record, self::CREATE_MEMORY_HYDRATED); |
||
| 795 | |||
| 796 | // Modify ClassName |
||
| 797 | if ($newClassName != $originalClass) { |
||
| 798 | $newInstance->setClassName($newClassName); |
||
| 799 | $newInstance->populateDefaults(); |
||
| 800 | $newInstance->forceChange(); |
||
| 801 | } |
||
| 802 | |||
| 803 | return $newInstance; |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Adds methods from the extensions. |
||
| 808 | * Called by Object::__construct() once per class. |
||
| 809 | */ |
||
| 810 | public function defineMethods() |
||
| 811 | { |
||
| 812 | parent::defineMethods(); |
||
| 813 | |||
| 814 | if (static::class === self::class) { |
||
| 815 | return; |
||
| 816 | } |
||
| 817 | |||
| 818 | // Set up accessors for joined items |
||
| 819 | if ($manyMany = $this->manyMany()) { |
||
| 820 | foreach ($manyMany as $relationship => $class) { |
||
| 821 | $this->addWrapperMethod($relationship, 'getManyManyComponents'); |
||
| 822 | } |
||
| 823 | } |
||
| 824 | if ($hasMany = $this->hasMany()) { |
||
| 825 | foreach ($hasMany as $relationship => $class) { |
||
| 826 | $this->addWrapperMethod($relationship, 'getComponents'); |
||
| 827 | } |
||
| 828 | } |
||
| 829 | if ($hasOne = $this->hasOne()) { |
||
| 830 | foreach ($hasOne as $relationship => $class) { |
||
| 831 | $this->addWrapperMethod($relationship, 'getComponent'); |
||
| 832 | } |
||
| 833 | } |
||
| 834 | if ($belongsTo = $this->belongsTo()) { |
||
| 835 | foreach (array_keys($belongsTo) as $relationship) { |
||
| 836 | $this->addWrapperMethod($relationship, 'getComponent'); |
||
| 837 | } |
||
| 838 | } |
||
| 839 | } |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 843 | * The default behaviour for a DataObject is to return true if |
||
| 844 | * the object exists in the database, you can override this in subclasses. |
||
| 845 | * |
||
| 846 | * @return boolean true if this object exists |
||
| 847 | */ |
||
| 848 | public function exists() |
||
| 849 | { |
||
| 850 | return (isset($this->record['ID']) && $this->record['ID'] > 0); |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Returns TRUE if all values (other than "ID") are |
||
| 855 | * considered empty (by weak boolean comparison). |
||
| 856 | * |
||
| 857 | * @return boolean |
||
| 858 | */ |
||
| 859 | public function isEmpty() |
||
| 860 | { |
||
| 861 | $fixed = DataObject::config()->uninherited('fixed_fields'); |
||
| 862 | foreach ($this->toMap() as $field => $value) { |
||
| 863 | // only look at custom fields |
||
| 864 | if (isset($fixed[$field])) { |
||
| 865 | continue; |
||
| 866 | } |
||
| 867 | |||
| 868 | $dbObject = $this->dbObject($field); |
||
| 869 | if (!$dbObject) { |
||
| 870 | continue; |
||
| 871 | } |
||
| 872 | if ($dbObject->exists()) { |
||
| 873 | return false; |
||
| 874 | } |
||
| 875 | } |
||
| 876 | return true; |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Pluralise this item given a specific count. |
||
| 881 | * |
||
| 882 | * E.g. "0 Pages", "1 File", "3 Images" |
||
| 883 | * |
||
| 884 | * @param string $count |
||
| 885 | * @return string |
||
| 886 | */ |
||
| 887 | public function i18n_pluralise($count) |
||
| 888 | { |
||
| 889 | $default = 'one ' . $this->i18n_singular_name() . '|{count} ' . $this->i18n_plural_name(); |
||
| 890 | return i18n::_t( |
||
| 891 | static::class . '.PLURALS', |
||
| 892 | $default, |
||
| 893 | ['count' => $count] |
||
| 894 | ); |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Get the user friendly singular name of this DataObject. |
||
| 899 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 900 | * this returns the class name. |
||
| 901 | * |
||
| 902 | * @return string User friendly singular name of this DataObject |
||
| 903 | */ |
||
| 904 | public function singular_name() |
||
| 905 | { |
||
| 906 | $name = $this->config()->get('singular_name'); |
||
| 907 | if ($name) { |
||
| 908 | return $name; |
||
| 909 | } |
||
| 910 | return ucwords(trim(strtolower(preg_replace( |
||
| 911 | '/_?([A-Z])/', |
||
| 912 | ' $1', |
||
| 913 | ClassInfo::shortName($this) |
||
| 914 | )))); |
||
| 915 | } |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Get the translated user friendly singular name of this DataObject |
||
| 919 | * same as singular_name() but runs it through the translating function |
||
| 920 | * |
||
| 921 | * Translating string is in the form: |
||
| 922 | * $this->class.SINGULARNAME |
||
| 923 | * Example: |
||
| 924 | * Page.SINGULARNAME |
||
| 925 | * |
||
| 926 | * @return string User friendly translated singular name of this DataObject |
||
| 927 | */ |
||
| 928 | public function i18n_singular_name() |
||
| 929 | { |
||
| 930 | return _t(static::class . '.SINGULARNAME', $this->singular_name()); |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get the user friendly plural name of this DataObject |
||
| 935 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 936 | * this returns a pluralised version of the class name. |
||
| 937 | * |
||
| 938 | * @return string User friendly plural name of this DataObject |
||
| 939 | */ |
||
| 940 | public function plural_name() |
||
| 941 | { |
||
| 942 | if ($name = $this->config()->get('plural_name')) { |
||
| 943 | return $name; |
||
| 944 | } |
||
| 945 | $name = $this->singular_name(); |
||
| 946 | //if the penultimate character is not a vowel, replace "y" with "ies" |
||
| 947 | if (preg_match('/[^aeiou]y$/i', $name)) { |
||
| 948 | $name = substr($name, 0, -1) . 'ie'; |
||
| 949 | } |
||
| 950 | return ucfirst($name . 's'); |
||
| 951 | } |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Get the translated user friendly plural name of this DataObject |
||
| 955 | * Same as plural_name but runs it through the translation function |
||
| 956 | * Translation string is in the form: |
||
| 957 | * $this->class.PLURALNAME |
||
| 958 | * Example: |
||
| 959 | * Page.PLURALNAME |
||
| 960 | * |
||
| 961 | * @return string User friendly translated plural name of this DataObject |
||
| 962 | */ |
||
| 963 | public function i18n_plural_name() |
||
| 964 | { |
||
| 965 | return _t(static::class . '.PLURALNAME', $this->plural_name()); |
||
| 966 | } |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Standard implementation of a title/label for a specific |
||
| 970 | * record. Tries to find properties 'Title' or 'Name', |
||
| 971 | * and falls back to the 'ID'. Useful to provide |
||
| 972 | * user-friendly identification of a record, e.g. in errormessages |
||
| 973 | * or UI-selections. |
||
| 974 | * |
||
| 975 | * Overload this method to have a more specialized implementation, |
||
| 976 | * e.g. for an Address record this could be: |
||
| 977 | * <code> |
||
| 978 | * function getTitle() { |
||
| 979 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 980 | * } |
||
| 981 | * </code> |
||
| 982 | * |
||
| 983 | * @return string |
||
| 984 | */ |
||
| 985 | public function getTitle() |
||
| 986 | { |
||
| 987 | $schema = static::getSchema(); |
||
| 988 | if ($schema->fieldSpec($this, 'Title')) { |
||
| 989 | return $this->getField('Title'); |
||
| 990 | } |
||
| 991 | if ($schema->fieldSpec($this, 'Name')) { |
||
| 992 | return $this->getField('Name'); |
||
| 993 | } |
||
| 994 | |||
| 995 | return "#{$this->ID}"; |
||
| 996 | } |
||
| 997 | |||
| 998 | /** |
||
| 999 | * Returns the associated database record - in this case, the object itself. |
||
| 1000 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 1001 | * |
||
| 1002 | * @return DataObject Associated database record |
||
| 1003 | */ |
||
| 1004 | public function data() |
||
| 1005 | { |
||
| 1006 | return $this; |
||
| 1007 | } |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Convert this object to a map. |
||
| 1011 | * Note that it has the following quirks: |
||
| 1012 | * - custom getters, including those that adjust the result of database fields, won't be executed |
||
| 1013 | * - NULL values won't be returned. |
||
| 1014 | * |
||
| 1015 | * @return array The data as a map. |
||
| 1016 | */ |
||
| 1017 | public function toMap() |
||
| 1018 | { |
||
| 1019 | $this->loadLazyFields(); |
||
| 1020 | return array_filter($this->record, function ($val) { |
||
| 1021 | return $val !== null; |
||
| 1022 | }); |
||
| 1023 | } |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Return all currently fetched database fields. |
||
| 1027 | * |
||
| 1028 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 1029 | * Obviously, this makes it a lot faster. |
||
| 1030 | * |
||
| 1031 | * @return array The data as a map. |
||
| 1032 | */ |
||
| 1033 | public function getQueriedDatabaseFields() |
||
| 1034 | { |
||
| 1035 | return $this->record; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 1040 | * |
||
| 1041 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 1042 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 1043 | * |
||
| 1044 | * Doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 1045 | * the related objects that it alters. |
||
| 1046 | * |
||
| 1047 | * When using this method with user supplied data, it's very important to |
||
| 1048 | * whitelist the allowed keys. |
||
| 1049 | * |
||
| 1050 | * @param array $data A map of field name to data values to update. |
||
| 1051 | * @return DataObject $this |
||
| 1052 | */ |
||
| 1053 | public function update($data) |
||
| 1054 | { |
||
| 1055 | foreach ($data as $key => $value) { |
||
| 1056 | // Implement dot syntax for updates |
||
| 1057 | if (strpos($key, '.') !== false) { |
||
| 1058 | $relations = explode('.', $key); |
||
| 1059 | $fieldName = array_pop($relations); |
||
| 1060 | /** @var static $relObj */ |
||
| 1061 | $relObj = $this; |
||
| 1062 | $relation = null; |
||
| 1063 | foreach ($relations as $i => $relation) { |
||
| 1064 | // no support for has_many or many_many relationships, |
||
| 1065 | // as the updater wouldn't know which object to write to (or create) |
||
| 1066 | if ($relObj->$relation() instanceof DataObject) { |
||
| 1067 | $parentObj = $relObj; |
||
| 1068 | $relObj = $relObj->$relation(); |
||
| 1069 | // If the intermediate relationship objects haven't been created, then write them |
||
| 1070 | if ($i < sizeof($relations) - 1 && !$relObj->ID || (!$relObj->ID && $parentObj !== $this)) { |
||
| 1071 | $relObj->write(); |
||
| 1072 | $relatedFieldName = $relation . "ID"; |
||
| 1073 | $parentObj->$relatedFieldName = $relObj->ID; |
||
| 1074 | $parentObj->write(); |
||
| 1075 | } |
||
| 1076 | } else { |
||
| 1077 | user_error( |
||
| 1078 | "DataObject::update(): Can't traverse relationship '$relation'," . |
||
| 1079 | "it has to be a has_one relationship or return a single DataObject", |
||
| 1080 | E_USER_NOTICE |
||
| 1081 | ); |
||
| 1082 | // unset relation object so we don't write properties to the wrong object |
||
| 1083 | $relObj = null; |
||
| 1084 | break; |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | if ($relObj) { |
||
| 1089 | $relObj->$fieldName = $value; |
||
| 1090 | $relObj->write(); |
||
| 1091 | $relatedFieldName = $relation . "ID"; |
||
| 1092 | $this->$relatedFieldName = $relObj->ID; |
||
| 1093 | $relObj->flushCache(); |
||
| 1094 | } else { |
||
| 1095 | $class = static::class; |
||
| 1096 | user_error("Couldn't follow dot syntax '{$key}' on '{$class}' object", E_USER_WARNING); |
||
| 1097 | } |
||
| 1098 | } else { |
||
| 1099 | $this->$key = $value; |
||
| 1100 | } |
||
| 1101 | } |
||
| 1102 | return $this; |
||
| 1103 | } |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Pass changes as a map, and try to |
||
| 1107 | * get automatic casting for these fields. |
||
| 1108 | * Doesn't write to the database. To write the data, |
||
| 1109 | * use the write() method. |
||
| 1110 | * |
||
| 1111 | * @param array $data A map of field name to data values to update. |
||
| 1112 | * @return DataObject $this |
||
| 1113 | */ |
||
| 1114 | public function castedUpdate($data) |
||
| 1115 | { |
||
| 1116 | foreach ($data as $k => $v) { |
||
| 1117 | $this->setCastedField($k, $v); |
||
| 1118 | } |
||
| 1119 | return $this; |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * Merges data and relations from another object of same class, |
||
| 1124 | * without conflict resolution. Allows to specify which |
||
| 1125 | * dataset takes priority in case its not empty. |
||
| 1126 | * has_one-relations are just transferred with priority 'right'. |
||
| 1127 | * has_many and many_many-relations are added regardless of priority. |
||
| 1128 | * |
||
| 1129 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 1130 | * meaning they are not connected to the merged object any longer. |
||
| 1131 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 1132 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 1133 | * Caution: Does not delete the merged object. |
||
| 1134 | * Caution: Does now overwrite Created date on the original object. |
||
| 1135 | * |
||
| 1136 | * @param DataObject $rightObj |
||
| 1137 | * @param string $priority left|right Determines who wins in case of a conflict (optional) |
||
| 1138 | * @param bool $includeRelations Merge any existing relations (optional) |
||
| 1139 | * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values. |
||
| 1140 | * Only applicable with $priority='right'. (optional) |
||
| 1141 | * @return Boolean |
||
| 1142 | */ |
||
| 1143 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) |
||
| 1144 | { |
||
| 1145 | $leftObj = $this; |
||
| 1146 | |||
| 1147 | if ($leftObj->ClassName != $rightObj->ClassName) { |
||
| 1148 | // we can't merge similiar subclasses because they might have additional relations |
||
| 1149 | user_error("DataObject->merge(): Invalid object class '{$rightObj->ClassName}' |
||
| 1150 | (expected '{$leftObj->ClassName}').", E_USER_WARNING); |
||
| 1151 | return false; |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | if (!$rightObj->ID) { |
||
| 1155 | user_error("DataObject->merge(): Please write your merged-in object to the database before merging, |
||
| 1156 | to make sure all relations are transferred properly.').", E_USER_WARNING); |
||
| 1157 | return false; |
||
| 1158 | } |
||
| 1159 | |||
| 1160 | // makes sure we don't merge data like ID or ClassName |
||
| 1161 | $rightData = DataObject::getSchema()->fieldSpecs(get_class($rightObj)); |
||
| 1162 | foreach ($rightData as $key => $rightSpec) { |
||
| 1163 | // Don't merge ID |
||
| 1164 | if ($key === 'ID') { |
||
| 1165 | continue; |
||
| 1166 | } |
||
| 1167 | |||
| 1168 | // Only merge relations if allowed |
||
| 1169 | if ($rightSpec === 'ForeignKey' && !$includeRelations) { |
||
| 1170 | continue; |
||
| 1171 | } |
||
| 1172 | |||
| 1173 | // don't merge conflicting values if priority is 'left' |
||
| 1174 | if ($priority == 'left' && $leftObj->{$key} !== $rightObj->{$key}) { |
||
| 1175 | continue; |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | // don't overwrite existing left values with empty right values (if $overwriteWithEmpty is set) |
||
| 1179 | if ($priority == 'right' && !$overwriteWithEmpty && empty($rightObj->{$key})) { |
||
| 1180 | continue; |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | // TODO remove redundant merge of has_one fields |
||
| 1184 | $leftObj->{$key} = $rightObj->{$key}; |
||
| 1185 | } |
||
| 1186 | |||
| 1187 | // merge relations |
||
| 1188 | if ($includeRelations) { |
||
| 1189 | if ($manyMany = $this->manyMany()) { |
||
| 1190 | foreach ($manyMany as $relationship => $class) { |
||
| 1191 | /** @var DataObject $leftComponents */ |
||
| 1192 | $leftComponents = $leftObj->getManyManyComponents($relationship); |
||
| 1193 | $rightComponents = $rightObj->getManyManyComponents($relationship); |
||
| 1194 | if ($rightComponents && $rightComponents->exists()) { |
||
| 1195 | $leftComponents->addMany($rightComponents->column('ID')); |
||
| 1196 | } |
||
| 1197 | $leftComponents->write(); |
||
| 1198 | } |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | if ($hasMany = $this->hasMany()) { |
||
| 1202 | foreach ($hasMany as $relationship => $class) { |
||
| 1203 | $leftComponents = $leftObj->getComponents($relationship); |
||
| 1204 | $rightComponents = $rightObj->getComponents($relationship); |
||
| 1205 | if ($rightComponents && $rightComponents->exists()) { |
||
| 1206 | $leftComponents->addMany($rightComponents->column('ID')); |
||
| 1207 | } |
||
| 1208 | $leftComponents->write(); |
||
| 1209 | } |
||
| 1210 | } |
||
| 1211 | } |
||
| 1212 | |||
| 1213 | return true; |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Forces the record to think that all its data has changed. |
||
| 1218 | * Doesn't write to the database. Force-change preseved until |
||
| 1219 | * next write. Existing CHANGE_VALUE or CHANGE_STRICT values |
||
| 1220 | * are preserved. |
||
| 1221 | * |
||
| 1222 | * @return $this |
||
| 1223 | */ |
||
| 1224 | public function forceChange() |
||
| 1225 | { |
||
| 1226 | // Ensure lazy fields loaded |
||
| 1227 | $this->loadLazyFields(); |
||
| 1228 | |||
| 1229 | // Populate the null values in record so that they actually get written |
||
| 1230 | foreach (array_keys(static::getSchema()->fieldSpecs(static::class)) as $fieldName) { |
||
| 1231 | if (!isset($this->record[$fieldName])) { |
||
| 1232 | $this->record[$fieldName] = null; |
||
| 1233 | } |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | $this->changeForced = true; |
||
| 1237 | |||
| 1238 | return $this; |
||
| 1239 | } |
||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * Validate the current object. |
||
| 1243 | * |
||
| 1244 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1245 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1246 | * |
||
| 1247 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1248 | * and onAfterWrite() won't get called either. |
||
| 1249 | * |
||
| 1250 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1251 | * attempting a write, and respond appropriately if it isn't. |
||
| 1252 | * |
||
| 1253 | * @see {@link ValidationResult} |
||
| 1254 | * @return ValidationResult |
||
| 1255 | */ |
||
| 1256 | public function validate() |
||
| 1257 | { |
||
| 1258 | $result = ValidationResult::create(); |
||
| 1259 | $this->extend('validate', $result); |
||
| 1260 | return $result; |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Public accessor for {@see DataObject::validate()} |
||
| 1265 | * |
||
| 1266 | * @return ValidationResult |
||
| 1267 | */ |
||
| 1268 | public function doValidate() |
||
| 1269 | { |
||
| 1270 | Deprecation::notice('5.0', 'Use validate'); |
||
| 1271 | return $this->validate(); |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Event handler called before writing to the database. |
||
| 1276 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1277 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1278 | * |
||
| 1279 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1280 | * |
||
| 1281 | * @uses DataExtension::onBeforeWrite() |
||
| 1282 | */ |
||
| 1283 | protected function onBeforeWrite() |
||
| 1284 | { |
||
| 1285 | $this->brokenOnWrite = false; |
||
| 1286 | |||
| 1287 | $dummy = null; |
||
| 1288 | $this->extend('onBeforeWrite', $dummy); |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Event handler called after writing to the database. |
||
| 1293 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1294 | * $this->changed will have a record |
||
| 1295 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1296 | * |
||
| 1297 | * @uses DataExtension::onAfterWrite() |
||
| 1298 | */ |
||
| 1299 | protected function onAfterWrite() |
||
| 1300 | { |
||
| 1301 | $dummy = null; |
||
| 1302 | $this->extend('onAfterWrite', $dummy); |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Find all objects that will be cascade deleted if this object is deleted |
||
| 1307 | * |
||
| 1308 | * Notes: |
||
| 1309 | * - If this object is versioned, objects will only be searched in the same stage as the given record. |
||
| 1310 | * - This will only be useful prior to deletion, as post-deletion this record will no longer exist. |
||
| 1311 | * |
||
| 1312 | * @param bool $recursive True if recursive |
||
| 1313 | * @param ArrayList $list Optional list to add items to |
||
| 1314 | * @return ArrayList list of objects |
||
| 1315 | */ |
||
| 1316 | public function findCascadeDeletes($recursive = true, $list = null) |
||
| 1317 | { |
||
| 1318 | // Find objects in these relationships |
||
| 1319 | return $this->findRelatedObjects('cascade_deletes', $recursive, $list); |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * Event handler called before deleting from the database. |
||
| 1324 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1325 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1326 | * |
||
| 1327 | * @uses DataExtension::onBeforeDelete() |
||
| 1328 | */ |
||
| 1329 | protected function onBeforeDelete() |
||
| 1330 | { |
||
| 1331 | $this->brokenOnDelete = false; |
||
| 1332 | |||
| 1333 | $dummy = null; |
||
| 1334 | $this->extend('onBeforeDelete', $dummy); |
||
| 1335 | |||
| 1336 | // Cascade deletes |
||
| 1337 | $deletes = $this->findCascadeDeletes(false); |
||
| 1338 | foreach ($deletes as $delete) { |
||
| 1339 | $delete->delete(); |
||
| 1340 | } |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | protected function onAfterDelete() |
||
| 1344 | { |
||
| 1345 | $this->extend('onAfterDelete'); |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Load the default values in from the self::$defaults array. |
||
| 1350 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1351 | * Called by the constructor when creating new records. |
||
| 1352 | * |
||
| 1353 | * @uses DataExtension::populateDefaults() |
||
| 1354 | * @return DataObject $this |
||
| 1355 | */ |
||
| 1356 | public function populateDefaults() |
||
| 1357 | { |
||
| 1358 | $classes = array_reverse(ClassInfo::ancestry($this)); |
||
| 1359 | |||
| 1360 | foreach ($classes as $class) { |
||
| 1361 | $defaults = Config::inst()->get($class, 'defaults', Config::UNINHERITED); |
||
| 1362 | |||
| 1363 | if ($defaults && !is_array($defaults)) { |
||
| 1364 | user_error( |
||
| 1365 | "Bad '" . static::class . "' defaults given: " . var_export($defaults, true), |
||
| 1366 | E_USER_WARNING |
||
| 1367 | ); |
||
| 1368 | $defaults = null; |
||
| 1369 | } |
||
| 1370 | |||
| 1371 | if ($defaults) { |
||
| 1372 | foreach ($defaults as $fieldName => $fieldValue) { |
||
| 1373 | // SRM 2007-03-06: Stricter check |
||
| 1374 | if (!isset($this->$fieldName) || $this->$fieldName === null) { |
||
| 1375 | $this->$fieldName = $fieldValue; |
||
| 1376 | } |
||
| 1377 | // Set many-many defaults with an array of ids |
||
| 1378 | if (is_array($fieldValue) && $this->getSchema()->manyManyComponent(static::class, $fieldName)) { |
||
| 1379 | /** @var ManyManyList $manyManyJoin */ |
||
| 1380 | $manyManyJoin = $this->$fieldName(); |
||
| 1381 | $manyManyJoin->setByIDList($fieldValue); |
||
| 1382 | } |
||
| 1383 | } |
||
| 1384 | } |
||
| 1385 | if ($class == self::class) { |
||
| 1386 | break; |
||
| 1387 | } |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | $this->extend('populateDefaults'); |
||
| 1391 | return $this; |
||
| 1392 | } |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Determine validation of this object prior to write |
||
| 1396 | * |
||
| 1397 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1398 | */ |
||
| 1399 | protected function validateWrite() |
||
| 1400 | { |
||
| 1401 | if ($this->ObsoleteClassName) { |
||
| 1402 | return new ValidationException( |
||
| 1403 | "Object is of class '{$this->ObsoleteClassName}' which doesn't exist - " . |
||
| 1404 | "you need to change the ClassName before you can write it" |
||
| 1405 | ); |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | // Note: Validation can only be disabled at the global level, not per-model |
||
| 1409 | if (DataObject::config()->uninherited('validation_enabled')) { |
||
| 1410 | $result = $this->validate(); |
||
| 1411 | if (!$result->isValid()) { |
||
| 1412 | return new ValidationException($result); |
||
| 1413 | } |
||
| 1414 | } |
||
| 1415 | return null; |
||
| 1416 | } |
||
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Prepare an object prior to write |
||
| 1420 | * |
||
| 1421 | * @throws ValidationException |
||
| 1422 | */ |
||
| 1423 | protected function preWrite() |
||
| 1424 | { |
||
| 1425 | // Validate this object |
||
| 1426 | if ($writeException = $this->validateWrite()) { |
||
| 1427 | // Used by DODs to clean up after themselves, eg, Versioned |
||
| 1428 | $this->invokeWithExtensions('onAfterSkippedWrite'); |
||
| 1429 | throw $writeException; |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | // Check onBeforeWrite |
||
| 1433 | $this->brokenOnWrite = true; |
||
| 1434 | $this->onBeforeWrite(); |
||
| 1435 | if ($this->brokenOnWrite) { |
||
| 1436 | throw new LogicException( |
||
| 1437 | static::class . " has a broken onBeforeWrite() function." |
||
| 1438 | . " Make sure that you call parent::onBeforeWrite()." |
||
| 1439 | ); |
||
| 1440 | } |
||
| 1441 | } |
||
| 1442 | |||
| 1443 | /** |
||
| 1444 | * Detects and updates all changes made to this object |
||
| 1445 | * |
||
| 1446 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1447 | * @return bool True if any changes are detected |
||
| 1448 | */ |
||
| 1449 | protected function updateChanges($forceChanges = false) |
||
| 1450 | { |
||
| 1451 | if ($forceChanges) { |
||
| 1452 | // Force changes, but only for loaded fields |
||
| 1453 | foreach ($this->record as $field => $value) { |
||
| 1454 | $this->changed[$field] = static::CHANGE_VALUE; |
||
| 1455 | } |
||
| 1456 | return true; |
||
| 1457 | } |
||
| 1458 | return $this->isChanged(); |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1463 | * |
||
| 1464 | * @param string $baseTable Base table |
||
| 1465 | * @param string $now Timestamp to use for the current time |
||
| 1466 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1467 | * @param array $manipulation Manipulation to write to |
||
| 1468 | * @param string $class Class of table to manipulate |
||
| 1469 | */ |
||
| 1470 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) |
||
| 1471 | { |
||
| 1472 | $schema = $this->getSchema(); |
||
| 1473 | $table = $schema->tableName($class); |
||
| 1474 | $manipulation[$table] = []; |
||
| 1475 | |||
| 1476 | $changed = $this->getChangedFields(); |
||
| 1477 | |||
| 1478 | // Extract records for this table |
||
| 1479 | foreach ($this->record as $fieldName => $fieldValue) { |
||
| 1480 | // we're not attempting to reset the BaseTable->ID |
||
| 1481 | // Ignore unchanged fields or attempts to reset the BaseTable->ID |
||
| 1482 | if (empty($changed[$fieldName]) || ($table === $baseTable && $fieldName === 'ID')) { |
||
| 1483 | continue; |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | // Ensure this field pertains to this table |
||
| 1487 | $specification = $schema->fieldSpec( |
||
| 1488 | $class, |
||
| 1489 | $fieldName, |
||
| 1490 | DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED |
||
| 1491 | ); |
||
| 1492 | if (!$specification) { |
||
| 1493 | continue; |
||
| 1494 | } |
||
| 1495 | |||
| 1496 | // if database column doesn't correlate to a DBField instance... |
||
| 1497 | $fieldObj = $this->dbObject($fieldName); |
||
| 1498 | if (!$fieldObj) { |
||
| 1499 | $fieldObj = DBField::create_field('Varchar', $fieldValue, $fieldName); |
||
| 1500 | } |
||
| 1501 | |||
| 1502 | // Write to manipulation |
||
| 1503 | $fieldObj->writeToManipulation($manipulation[$table]); |
||
| 1504 | } |
||
| 1505 | |||
| 1506 | // Ensure update of Created and LastEdited columns |
||
| 1507 | if ($baseTable === $table) { |
||
| 1508 | $manipulation[$table]['fields']['LastEdited'] = $now; |
||
| 1509 | if ($isNewRecord) { |
||
| 1510 | $manipulation[$table]['fields']['Created'] = empty($this->record['Created']) |
||
| 1511 | ? $now |
||
| 1512 | : $this->record['Created']; |
||
| 1513 | $manipulation[$table]['fields']['ClassName'] = static::class; |
||
| 1514 | } |
||
| 1515 | } |
||
| 1516 | |||
| 1517 | // Inserts done one the base table are performed in another step, so the manipulation should instead |
||
| 1518 | // attempt an update, as though it were a normal update. |
||
| 1519 | $manipulation[$table]['command'] = $isNewRecord ? 'insert' : 'update'; |
||
| 1520 | $manipulation[$table]['class'] = $class; |
||
| 1521 | if ($this->isInDB()) { |
||
| 1522 | $manipulation[$table]['id'] = $this->record['ID']; |
||
| 1523 | } |
||
| 1524 | } |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1528 | * |
||
| 1529 | * Does nothing if an ID is already assigned for this record |
||
| 1530 | * |
||
| 1531 | * @param string $baseTable Base table |
||
| 1532 | * @param string $now Timestamp to use for the current time |
||
| 1533 | */ |
||
| 1534 | protected function writeBaseRecord($baseTable, $now) |
||
| 1535 | { |
||
| 1536 | // Generate new ID if not specified |
||
| 1537 | if ($this->isInDB()) { |
||
| 1538 | return; |
||
| 1539 | } |
||
| 1540 | |||
| 1541 | // Perform an insert on the base table |
||
| 1542 | $manipulation = []; |
||
| 1543 | $this->prepareManipulationTable($baseTable, $now, true, $manipulation, $this->baseClass()); |
||
| 1544 | DB::manipulate($manipulation); |
||
| 1545 | |||
| 1546 | $this->changed['ID'] = self::CHANGE_VALUE; |
||
| 1547 | $this->record['ID'] = DB::get_generated_id($baseTable); |
||
| 1548 | } |
||
| 1549 | |||
| 1550 | /** |
||
| 1551 | * Generate and write the database manipulation for all changed fields |
||
| 1552 | * |
||
| 1553 | * @param string $baseTable Base table |
||
| 1554 | * @param string $now Timestamp to use for the current time |
||
| 1555 | * @param bool $isNewRecord If this is a new record |
||
| 1556 | * @throws InvalidArgumentException |
||
| 1557 | */ |
||
| 1558 | protected function writeManipulation($baseTable, $now, $isNewRecord) |
||
| 1559 | { |
||
| 1560 | // Generate database manipulations for each class |
||
| 1561 | $manipulation = []; |
||
| 1562 | foreach (ClassInfo::ancestry(static::class, true) as $class) { |
||
| 1563 | $this->prepareManipulationTable($baseTable, $now, $isNewRecord, $manipulation, $class); |
||
| 1564 | } |
||
| 1565 | |||
| 1566 | // Allow extensions to extend this manipulation |
||
| 1567 | $this->extend('augmentWrite', $manipulation); |
||
| 1568 | |||
| 1569 | // New records have their insert into the base data table done first, so that they can pass the |
||
| 1570 | // generated ID on to the rest of the manipulation |
||
| 1571 | if ($isNewRecord) { |
||
| 1572 | $manipulation[$baseTable]['command'] = 'update'; |
||
| 1573 | } |
||
| 1574 | |||
| 1575 | // Make sure none of our field assignment are arrays |
||
| 1576 | foreach ($manipulation as $tableManipulation) { |
||
| 1577 | if (!isset($tableManipulation['fields'])) { |
||
| 1578 | continue; |
||
| 1579 | } |
||
| 1580 | foreach ($tableManipulation['fields'] as $fieldName => $fieldValue) { |
||
| 1581 | if (is_array($fieldValue)) { |
||
| 1582 | $dbObject = $this->dbObject($fieldName); |
||
| 1583 | // If the field allows non-scalar values we'll let it do dynamic assignments |
||
| 1584 | if ($dbObject && $dbObject->scalarValueOnly()) { |
||
| 1585 | throw new InvalidArgumentException( |
||
| 1586 | 'DataObject::writeManipulation: parameterised field assignments are disallowed' |
||
| 1587 | ); |
||
| 1588 | } |
||
| 1589 | } |
||
| 1590 | } |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | // Perform the manipulation |
||
| 1594 | DB::manipulate($manipulation); |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * Writes all changes to this object to the database. |
||
| 1599 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1600 | * - All relevant tables will be updated. |
||
| 1601 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1602 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1603 | * |
||
| 1604 | * @uses DataExtension::augmentWrite() |
||
| 1605 | * |
||
| 1606 | * @param boolean $showDebug Show debugging information |
||
| 1607 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1608 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1609 | * @param boolean|array $writeComponents Call write() on all associated component instances which were previously |
||
| 1610 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1611 | * {@link getManyManyComponents()}. Default to `false`. The parameter can also be provided in |
||
| 1612 | * the form of an array: `['recursive' => true, skip => ['Page'=>[1,2,3]]`. This avoid infinite |
||
| 1613 | * loops when one DataObject are components of each other. |
||
| 1614 | * @return int The ID of the record |
||
| 1615 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1616 | */ |
||
| 1617 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) |
||
| 1618 | { |
||
| 1619 | $now = DBDatetime::now()->Rfc2822(); |
||
| 1620 | |||
| 1621 | // Execute pre-write tasks |
||
| 1622 | $this->preWrite(); |
||
| 1623 | |||
| 1624 | // Check if we are doing an update or an insert |
||
| 1625 | $isNewRecord = !$this->isInDB() || $forceInsert; |
||
| 1626 | |||
| 1627 | // Check changes exist, abort if there are none |
||
| 1628 | $hasChanges = $this->updateChanges($isNewRecord); |
||
| 1629 | if ($hasChanges || $forceWrite || $isNewRecord) { |
||
| 1630 | // Ensure Created and LastEdited are populated |
||
| 1631 | if (!isset($this->record['Created'])) { |
||
| 1632 | $this->record['Created'] = $now; |
||
| 1633 | } |
||
| 1634 | $this->record['LastEdited'] = $now; |
||
| 1635 | |||
| 1636 | // New records have their insert into the base data table done first, so that they can pass the |
||
| 1637 | // generated primary key on to the rest of the manipulation |
||
| 1638 | $baseTable = $this->baseTable(); |
||
| 1639 | $this->writeBaseRecord($baseTable, $now); |
||
| 1640 | |||
| 1641 | // Write the DB manipulation for all changed fields |
||
| 1642 | $this->writeManipulation($baseTable, $now, $isNewRecord); |
||
| 1643 | |||
| 1644 | // If there's any relations that couldn't be saved before, save them now (we have an ID here) |
||
| 1645 | $this->writeRelations(); |
||
| 1646 | $this->onAfterWrite(); |
||
| 1647 | |||
| 1648 | // Reset isChanged data |
||
| 1649 | // DBComposites properly bound to the parent record will also have their isChanged value reset |
||
| 1650 | $this->changed = []; |
||
| 1651 | $this->changeForced = false; |
||
| 1652 | $this->original = $this->record; |
||
| 1653 | } else { |
||
| 1654 | if ($showDebug) { |
||
| 1655 | Debug::message("no changes for DataObject"); |
||
| 1656 | } |
||
| 1657 | |||
| 1658 | // Used by DODs to clean up after themselves, eg, Versioned |
||
| 1659 | $this->invokeWithExtensions('onAfterSkippedWrite'); |
||
| 1660 | } |
||
| 1661 | |||
| 1662 | // Write relations as necessary |
||
| 1663 | if ($writeComponents) { |
||
| 1664 | $recursive = true; |
||
| 1665 | $skip = []; |
||
| 1666 | if (is_array($writeComponents)) { |
||
| 1667 | $recursive = isset($writeComponents['recursive']) && $writeComponents['recursive']; |
||
| 1668 | $skip = isset($writeComponents['skip']) && is_array($writeComponents['skip']) |
||
| 1669 | ? $writeComponents['skip'] |
||
| 1670 | : []; |
||
| 1671 | } |
||
| 1672 | $this->writeComponents($recursive, $skip); |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | // Clears the cache for this object so get_one returns the correct object. |
||
| 1676 | $this->flushCache(); |
||
| 1677 | |||
| 1678 | return $this->record['ID']; |
||
| 1679 | } |
||
| 1680 | |||
| 1681 | /** |
||
| 1682 | * Writes cached relation lists to the database, if possible |
||
| 1683 | */ |
||
| 1684 | public function writeRelations() |
||
| 1685 | { |
||
| 1686 | if (!$this->isInDB()) { |
||
| 1687 | return; |
||
| 1688 | } |
||
| 1689 | |||
| 1690 | // If there's any relations that couldn't be saved before, save them now (we have an ID here) |
||
| 1691 | if ($this->unsavedRelations) { |
||
| 1692 | foreach ($this->unsavedRelations as $name => $list) { |
||
| 1693 | $list->changeToList($this->$name()); |
||
| 1694 | } |
||
| 1695 | $this->unsavedRelations = []; |
||
| 1696 | } |
||
| 1697 | } |
||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1701 | * same record. |
||
| 1702 | * |
||
| 1703 | * @param bool $recursive Recursively write components |
||
| 1704 | * @param array $skip List of DataObject references to skip |
||
| 1705 | * @return DataObject $this |
||
| 1706 | */ |
||
| 1707 | public function writeComponents($recursive = false, $skip = []) |
||
| 1708 | { |
||
| 1709 | // Make sure we add our current object to the skip list |
||
| 1710 | $this->skipWriteComponents($recursive, $this, $skip); |
||
| 1711 | |||
| 1712 | // All our write calls have the same arguments ... just need make sure the skip list is pass by reference |
||
| 1713 | $args = [ |
||
| 1714 | false, false, false, |
||
| 1715 | $recursive ? ["recursive" => $recursive, "skip" => &$skip] : false |
||
| 1716 | ]; |
||
| 1717 | |||
| 1718 | foreach ($this->components as $component) { |
||
| 1719 | if (!$this->skipWriteComponents($recursive, $component, $skip)) { |
||
| 1720 | $component->write(...$args); |
||
| 1721 | } |
||
| 1722 | } |
||
| 1723 | |||
| 1724 | if ($join = $this->getJoin()) { |
||
| 1725 | if (!$this->skipWriteComponents($recursive, $join, $skip)) { |
||
| 1726 | $join->write(...$args); |
||
| 1727 | } |
||
| 1728 | } |
||
| 1729 | |||
| 1730 | return $this; |
||
| 1731 | } |
||
| 1732 | |||
| 1733 | /** |
||
| 1734 | * Check if target is in the skip list and add it if it isn't. |
||
| 1735 | * @param bool $recursive |
||
| 1736 | * @param DataObject $target |
||
| 1737 | * @param array $skip |
||
| 1738 | * @return bool Whether the target is already in the list |
||
| 1739 | */ |
||
| 1740 | private function skipWriteComponents($recursive, DataObject $target, array &$skip) |
||
| 1741 | { |
||
| 1742 | // skip writing component if it doesn't exist |
||
| 1743 | if (!$target->exists()) { |
||
| 1744 | return true; |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | // We only care about the skip list if our call is meant to be recursive |
||
| 1748 | if (!$recursive) { |
||
| 1749 | return false; |
||
| 1750 | } |
||
| 1751 | |||
| 1752 | // Get our Skip array keys |
||
| 1753 | $classname = get_class($target); |
||
| 1754 | $id = $target->ID; |
||
| 1755 | |||
| 1756 | // Check if the target is in the skip list |
||
| 1757 | if (isset($skip[$classname])) { |
||
| 1758 | if (in_array($id, $skip[$classname])) { |
||
| 1759 | // Skip the object |
||
| 1760 | return true; |
||
| 1761 | } |
||
| 1762 | } else { |
||
| 1763 | // This is the first object of this class |
||
| 1764 | $skip[$classname] = []; |
||
| 1765 | } |
||
| 1766 | |||
| 1767 | // Add the target to our skip list |
||
| 1768 | $skip[$classname][] = $id; |
||
| 1769 | |||
| 1770 | return false; |
||
| 1771 | } |
||
| 1772 | |||
| 1773 | /** |
||
| 1774 | * Delete this data object. |
||
| 1775 | * $this->onBeforeDelete() gets called. |
||
| 1776 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1777 | * @uses DataExtension::augmentSQL() |
||
| 1778 | */ |
||
| 1779 | public function delete() |
||
| 1780 | { |
||
| 1781 | $this->brokenOnDelete = true; |
||
| 1782 | $this->onBeforeDelete(); |
||
| 1783 | if ($this->brokenOnDelete) { |
||
| 1784 | throw new LogicException( |
||
| 1785 | static::class . " has a broken onBeforeDelete() function." |
||
| 1786 | . " Make sure that you call parent::onBeforeDelete()." |
||
| 1787 | ); |
||
| 1788 | } |
||
| 1789 | |||
| 1790 | // Deleting a record without an ID shouldn't do anything |
||
| 1791 | if (!$this->ID) { |
||
| 1792 | throw new LogicException("DataObject::delete() called on a DataObject without an ID"); |
||
| 1793 | } |
||
| 1794 | |||
| 1795 | // TODO: This is quite ugly. To improve: |
||
| 1796 | // - move the details of the delete code in the DataQuery system |
||
| 1797 | // - update the code to just delete the base table, and rely on cascading deletes in the DB to do the rest |
||
| 1798 | // obviously, that means getting requireTable() to configure cascading deletes ;-) |
||
| 1799 | $srcQuery = DataList::create(static::class) |
||
| 1800 | ->filter('ID', $this->ID) |
||
| 1801 | ->dataQuery() |
||
| 1802 | ->query(); |
||
| 1803 | $queriedTables = $srcQuery->queriedTables(); |
||
| 1804 | $this->extend('updateDeleteTables', $queriedTables, $srcQuery); |
||
| 1805 | foreach ($queriedTables as $table) { |
||
| 1806 | $delete = SQLDelete::create("\"$table\"", ['"ID"' => $this->ID]); |
||
| 1807 | $this->extend('updateDeleteTable', $delete, $table, $queriedTables, $srcQuery); |
||
| 1808 | $delete->execute(); |
||
| 1809 | } |
||
| 1810 | // Remove this item out of any caches |
||
| 1811 | $this->flushCache(); |
||
| 1812 | |||
| 1813 | $this->onAfterDelete(); |
||
| 1814 | |||
| 1815 | $this->OldID = $this->ID; |
||
| 1816 | $this->ID = 0; |
||
| 1817 | } |
||
| 1818 | |||
| 1819 | /** |
||
| 1820 | * Delete the record with the given ID. |
||
| 1821 | * |
||
| 1822 | * @param string $className The class name of the record to be deleted |
||
| 1823 | * @param int $id ID of record to be deleted |
||
| 1824 | */ |
||
| 1825 | public static function delete_by_id($className, $id) |
||
| 1826 | { |
||
| 1827 | $obj = DataObject::get_by_id($className, $id); |
||
| 1828 | if ($obj) { |
||
| 1829 | $obj->delete(); |
||
| 1830 | } else { |
||
| 1831 | user_error("$className object #$id wasn't found when calling DataObject::delete_by_id", E_USER_WARNING); |
||
| 1832 | } |
||
| 1833 | } |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Get the class ancestry, including the current class name. |
||
| 1837 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1838 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1839 | * will be the current class. |
||
| 1840 | * |
||
| 1841 | * @return array Class ancestry |
||
| 1842 | */ |
||
| 1843 | public function getClassAncestry() |
||
| 1844 | { |
||
| 1845 | return ClassInfo::ancestry(static::class); |
||
| 1846 | } |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * Return a unary component object from a one to one relationship, as a DataObject. |
||
| 1850 | * If no component is available, an 'empty component' will be returned for |
||
| 1851 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1852 | * |
||
| 1853 | * @param string $componentName Name of the component |
||
| 1854 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1855 | * @throws Exception |
||
| 1856 | */ |
||
| 1857 | public function getComponent($componentName) |
||
| 1858 | { |
||
| 1859 | if (isset($this->components[$componentName])) { |
||
| 1860 | return $this->components[$componentName]; |
||
| 1861 | } |
||
| 1862 | |||
| 1863 | // The join object can be returned as a component, named for its alias |
||
| 1864 | if (isset($this->record[$componentName]) && $this->record[$componentName] === $this->joinRecord) { |
||
| 1865 | return $this->record[$componentName]; |
||
| 1866 | } |
||
| 1867 | |||
| 1868 | $schema = static::getSchema(); |
||
| 1869 | if ($class = $schema->hasOneComponent(static::class, $componentName)) { |
||
| 1870 | $joinField = $componentName . 'ID'; |
||
| 1871 | $joinID = $this->getField($joinField); |
||
| 1872 | |||
| 1873 | // Extract class name for polymorphic relations |
||
| 1874 | if ($class === self::class) { |
||
| 1875 | $class = $this->getField($componentName . 'Class'); |
||
| 1876 | if (empty($class)) { |
||
| 1877 | return null; |
||
| 1878 | } |
||
| 1879 | } |
||
| 1880 | |||
| 1881 | if ($joinID) { |
||
| 1882 | // Ensure that the selected object originates from the same stage, subsite, etc |
||
| 1883 | $component = DataObject::get($class) |
||
| 1884 | ->filter('ID', $joinID) |
||
| 1885 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 1886 | ->first(); |
||
| 1887 | } |
||
| 1888 | |||
| 1889 | if (empty($component)) { |
||
| 1890 | $component = Injector::inst()->create($class); |
||
| 1891 | } |
||
| 1892 | } elseif ($class = $schema->belongsToComponent(static::class, $componentName)) { |
||
| 1893 | $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic); |
||
| 1894 | $joinID = $this->ID; |
||
| 1895 | |||
| 1896 | if ($joinID) { |
||
| 1897 | // Prepare filter for appropriate join type |
||
| 1898 | if ($polymorphic) { |
||
| 1899 | $filter = [ |
||
| 1900 | "{$joinField}ID" => $joinID, |
||
| 1901 | "{$joinField}Class" => static::class, |
||
| 1902 | ]; |
||
| 1903 | } else { |
||
| 1904 | $filter = [ |
||
| 1905 | $joinField => $joinID |
||
| 1906 | ]; |
||
| 1907 | } |
||
| 1908 | |||
| 1909 | // Ensure that the selected object originates from the same stage, subsite, etc |
||
| 1910 | $component = DataObject::get($class) |
||
| 1911 | ->filter($filter) |
||
| 1912 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 1913 | ->first(); |
||
| 1914 | } |
||
| 1915 | |||
| 1916 | if (empty($component)) { |
||
| 1917 | $component = Injector::inst()->create($class); |
||
| 1918 | if ($polymorphic) { |
||
| 1919 | $component->{$joinField . 'ID'} = $this->ID; |
||
| 1920 | $component->{$joinField . 'Class'} = static::class; |
||
| 1921 | } else { |
||
| 1922 | $component->$joinField = $this->ID; |
||
| 1923 | } |
||
| 1924 | } |
||
| 1925 | } else { |
||
| 1926 | throw new InvalidArgumentException( |
||
| 1927 | "DataObject->getComponent(): Could not find component '$componentName'." |
||
| 1928 | ); |
||
| 1929 | } |
||
| 1930 | |||
| 1931 | $this->components[$componentName] = $component; |
||
| 1932 | return $component; |
||
| 1933 | } |
||
| 1934 | |||
| 1935 | /** |
||
| 1936 | * Assign an item to the given component |
||
| 1937 | * |
||
| 1938 | * @param string $componentName |
||
| 1939 | * @param DataObject|null $item |
||
| 1940 | * @return $this |
||
| 1941 | */ |
||
| 1942 | public function setComponent($componentName, $item) |
||
| 1943 | { |
||
| 1944 | // Validate component |
||
| 1945 | $schema = static::getSchema(); |
||
| 1946 | if ($class = $schema->hasOneComponent(static::class, $componentName)) { |
||
| 1947 | // Force item to be written if not by this point |
||
| 1948 | // @todo This could be lazy-written in a beforeWrite hook, but force write here for simplicity |
||
| 1949 | // https://github.com/silverstripe/silverstripe-framework/issues/7818 |
||
| 1950 | if ($item && !$item->isInDB()) { |
||
| 1951 | $item->write(); |
||
| 1952 | } |
||
| 1953 | |||
| 1954 | // Update local ID |
||
| 1955 | $joinField = $componentName . 'ID'; |
||
| 1956 | $this->setField($joinField, $item ? $item->ID : null); |
||
| 1957 | // Update Class (Polymorphic has_one) |
||
| 1958 | // Extract class name for polymorphic relations |
||
| 1959 | if ($class === self::class) { |
||
| 1960 | $this->setField($componentName . 'Class', $item ? get_class($item) : null); |
||
| 1961 | } |
||
| 1962 | } elseif ($class = $schema->belongsToComponent(static::class, $componentName)) { |
||
| 1963 | if ($item) { |
||
| 1964 | // For belongs_to, add to has_one on other component |
||
| 1965 | $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic); |
||
| 1966 | if (!$polymorphic) { |
||
| 1967 | $joinField = substr($joinField, 0, -2); |
||
| 1968 | } |
||
| 1969 | $item->setComponent($joinField, $this); |
||
| 1970 | } |
||
| 1971 | } else { |
||
| 1972 | throw new InvalidArgumentException( |
||
| 1973 | "DataObject->setComponent(): Could not find component '$componentName'." |
||
| 1974 | ); |
||
| 1975 | } |
||
| 1976 | |||
| 1977 | $this->components[$componentName] = $item; |
||
| 1978 | return $this; |
||
| 1979 | } |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Returns a one-to-many relation as a HasManyList |
||
| 1983 | * |
||
| 1984 | * @param string $componentName Name of the component |
||
| 1985 | * @param int|array $id Optional ID(s) for parent of this relation, if not the current record |
||
| 1986 | * @return HasManyList|UnsavedRelationList The components of the one-to-many relationship. |
||
| 1987 | */ |
||
| 1988 | public function getComponents($componentName, $id = null) |
||
| 1989 | { |
||
| 1990 | if (!isset($id)) { |
||
| 1991 | $id = $this->ID; |
||
| 1992 | } |
||
| 1993 | $result = null; |
||
| 1994 | |||
| 1995 | $schema = $this->getSchema(); |
||
| 1996 | $componentClass = $schema->hasManyComponent(static::class, $componentName); |
||
| 1997 | if (!$componentClass) { |
||
| 1998 | throw new InvalidArgumentException(sprintf( |
||
| 1999 | "DataObject::getComponents(): Unknown 1-to-many component '%s' on class '%s'", |
||
| 2000 | $componentName, |
||
| 2001 | static::class |
||
| 2002 | )); |
||
| 2003 | } |
||
| 2004 | |||
| 2005 | // If we haven't been written yet, we can't save these relations, so use a list that handles this case |
||
| 2006 | if (!$id) { |
||
| 2007 | if (!isset($this->unsavedRelations[$componentName])) { |
||
| 2008 | $this->unsavedRelations[$componentName] = |
||
| 2009 | new UnsavedRelationList(static::class, $componentName, $componentClass); |
||
| 2010 | } |
||
| 2011 | return $this->unsavedRelations[$componentName]; |
||
| 2012 | } |
||
| 2013 | |||
| 2014 | // Determine type and nature of foreign relation |
||
| 2015 | $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'has_many', $polymorphic); |
||
| 2016 | /** @var HasManyList $result */ |
||
| 2017 | if ($polymorphic) { |
||
| 2018 | $result = PolymorphicHasManyList::create($componentClass, $joinField, static::class); |
||
| 2019 | } else { |
||
| 2020 | $result = HasManyList::create($componentClass, $joinField); |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | return $result |
||
| 2024 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 2025 | ->forForeignID($id); |
||
| 2026 | } |
||
| 2027 | |||
| 2028 | /** |
||
| 2029 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 2030 | * |
||
| 2031 | * @param string $relationName Relation name. |
||
| 2032 | * @return string Class name, or null if not found. |
||
| 2033 | */ |
||
| 2034 | public function getRelationClass($relationName) |
||
| 2035 | { |
||
| 2036 | // Parse many_many |
||
| 2037 | $manyManyComponent = $this->getSchema()->manyManyComponent(static::class, $relationName); |
||
| 2038 | if ($manyManyComponent) { |
||
| 2039 | return $manyManyComponent['childClass']; |
||
| 2040 | } |
||
| 2041 | |||
| 2042 | // Go through all relationship configuration fields. |
||
| 2043 | $config = $this->config(); |
||
| 2044 | $candidates = array_merge( |
||
| 2045 | ($relations = $config->get('has_one')) ? $relations : [], |
||
| 2046 | ($relations = $config->get('has_many')) ? $relations : [], |
||
| 2047 | ($relations = $config->get('belongs_to')) ? $relations : [] |
||
| 2048 | ); |
||
| 2049 | |||
| 2050 | if (isset($candidates[$relationName])) { |
||
| 2051 | $remoteClass = $candidates[$relationName]; |
||
| 2052 | |||
| 2053 | // If dot notation is present, extract just the first part that contains the class. |
||
| 2054 | if (($fieldPos = strpos($remoteClass, '.')) !== false) { |
||
| 2055 | return substr($remoteClass, 0, $fieldPos); |
||
| 2056 | } |
||
| 2057 | |||
| 2058 | // Otherwise just return the class |
||
| 2059 | return $remoteClass; |
||
| 2060 | } |
||
| 2061 | |||
| 2062 | return null; |
||
| 2063 | } |
||
| 2064 | |||
| 2065 | /** |
||
| 2066 | * Given a relation name, determine the relation type |
||
| 2067 | * |
||
| 2068 | * @param string $component Name of component |
||
| 2069 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 2070 | */ |
||
| 2071 | public function getRelationType($component) |
||
| 2072 | { |
||
| 2073 | $types = ['has_one', 'has_many', 'many_many', 'belongs_many_many', 'belongs_to']; |
||
| 2074 | $config = $this->config(); |
||
| 2075 | foreach ($types as $type) { |
||
| 2076 | $relations = $config->get($type); |
||
| 2077 | if ($relations && isset($relations[$component])) { |
||
| 2078 | return $type; |
||
| 2079 | } |
||
| 2080 | } |
||
| 2081 | return null; |
||
| 2082 | } |
||
| 2083 | |||
| 2084 | /** |
||
| 2085 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 2086 | * side of the relation. |
||
| 2087 | * |
||
| 2088 | * Notes on behaviour: |
||
| 2089 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 2090 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 2091 | * - Polymorphic relationships do not have two natural endpoints (only on one side) |
||
| 2092 | * and thus attempting to infer them will return nothing. |
||
| 2093 | * - Cannot be used on unsaved objects. |
||
| 2094 | * |
||
| 2095 | * @param string $remoteClass |
||
| 2096 | * @param string $remoteRelation |
||
| 2097 | * @return DataList|DataObject The component, either as a list or single object |
||
| 2098 | * @throws BadMethodCallException |
||
| 2099 | * @throws InvalidArgumentException |
||
| 2100 | */ |
||
| 2101 | public function inferReciprocalComponent($remoteClass, $remoteRelation) |
||
| 2102 | { |
||
| 2103 | $remote = DataObject::singleton($remoteClass); |
||
| 2104 | $class = $remote->getRelationClass($remoteRelation); |
||
| 2105 | $schema = static::getSchema(); |
||
| 2106 | |||
| 2107 | // Validate arguments |
||
| 2108 | if (!$this->isInDB()) { |
||
| 2109 | throw new BadMethodCallException(__METHOD__ . " cannot be called on unsaved objects"); |
||
| 2110 | } |
||
| 2111 | if (empty($class)) { |
||
| 2112 | throw new InvalidArgumentException(sprintf( |
||
| 2113 | "%s invoked with invalid relation %s.%s", |
||
| 2114 | __METHOD__, |
||
| 2115 | $remoteClass, |
||
| 2116 | $remoteRelation |
||
| 2117 | )); |
||
| 2118 | } |
||
| 2119 | // If relation is polymorphic, do not infer recriprocal relationship |
||
| 2120 | if ($class === self::class) { |
||
| 2121 | return null; |
||
| 2122 | } |
||
| 2123 | if (!is_a($this, $class, true)) { |
||
| 2124 | throw new InvalidArgumentException(sprintf( |
||
| 2125 | "Relation %s on %s does not refer to objects of type %s", |
||
| 2126 | $remoteRelation, |
||
| 2127 | $remoteClass, |
||
| 2128 | static::class |
||
| 2129 | )); |
||
| 2130 | } |
||
| 2131 | |||
| 2132 | // Check the relation type to mock |
||
| 2133 | $relationType = $remote->getRelationType($remoteRelation); |
||
| 2134 | switch ($relationType) { |
||
| 2135 | case 'has_one': { |
||
| 2136 | // Mock has_many |
||
| 2137 | $joinField = "{$remoteRelation}ID"; |
||
| 2138 | $componentClass = $schema->classForField($remoteClass, $joinField); |
||
| 2139 | $result = HasManyList::create($componentClass, $joinField); |
||
| 2140 | return $result |
||
| 2141 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 2142 | ->forForeignID($this->ID); |
||
| 2143 | } |
||
| 2144 | case 'belongs_to': |
||
| 2145 | case 'has_many': { |
||
| 2146 | // These relations must have a has_one on the other end, so find it |
||
| 2147 | $joinField = $schema->getRemoteJoinField( |
||
| 2148 | $remoteClass, |
||
| 2149 | $remoteRelation, |
||
| 2150 | $relationType, |
||
| 2151 | $polymorphic |
||
| 2152 | ); |
||
| 2153 | // If relation is polymorphic, do not infer recriprocal relationship automatically |
||
| 2154 | if ($polymorphic) { |
||
| 2155 | return null; |
||
| 2156 | } |
||
| 2157 | $joinID = $this->getField($joinField); |
||
| 2158 | if (empty($joinID)) { |
||
| 2159 | return null; |
||
| 2160 | } |
||
| 2161 | // Get object by joined ID |
||
| 2162 | return DataObject::get($remoteClass) |
||
| 2163 | ->filter('ID', $joinID) |
||
| 2164 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 2165 | ->first(); |
||
| 2166 | } |
||
| 2167 | case 'many_many': |
||
| 2168 | case 'belongs_many_many': { |
||
| 2169 | // Get components and extra fields from parent |
||
| 2170 | $manyMany = $remote->getSchema()->manyManyComponent($remoteClass, $remoteRelation); |
||
| 2171 | $extraFields = $schema->manyManyExtraFieldsForComponent($remoteClass, $remoteRelation) ?: []; |
||
| 2172 | |||
| 2173 | // Reverse parent and component fields and create an inverse ManyManyList |
||
| 2174 | /** @var RelationList $result */ |
||
| 2175 | $result = Injector::inst()->create( |
||
| 2176 | $manyMany['relationClass'], |
||
| 2177 | $manyMany['parentClass'], // Substitute parent class for dataClass |
||
| 2178 | $manyMany['join'], |
||
| 2179 | $manyMany['parentField'], // Reversed parent / child field |
||
| 2180 | $manyMany['childField'], // Reversed parent / child field |
||
| 2181 | $extraFields, |
||
| 2182 | $manyMany['childClass'], // substitute child class for parentClass |
||
| 2183 | $remoteClass // In case ManyManyThroughList needs to use PolymorphicHasManyList internally |
||
| 2184 | ); |
||
| 2185 | $this->extend('updateManyManyComponents', $result); |
||
| 2186 | |||
| 2187 | // If this is called on a singleton, then we return an 'orphaned relation' that can have the |
||
| 2188 | // foreignID set elsewhere. |
||
| 2189 | return $result |
||
| 2190 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 2191 | ->forForeignID($this->ID); |
||
| 2192 | } |
||
| 2193 | default: { |
||
| 2194 | return null; |
||
| 2195 | } |
||
| 2196 | } |
||
| 2197 | } |
||
| 2198 | |||
| 2199 | /** |
||
| 2200 | * Returns a many-to-many component, as a ManyManyList. |
||
| 2201 | * @param string $componentName Name of the many-many component |
||
| 2202 | * @param int|array $id Optional ID for parent of this relation, if not the current record |
||
| 2203 | * @return ManyManyList|UnsavedRelationList The set of components |
||
| 2204 | */ |
||
| 2205 | public function getManyManyComponents($componentName, $id = null) |
||
| 2206 | { |
||
| 2207 | if (!isset($id)) { |
||
| 2208 | $id = $this->ID; |
||
| 2209 | } |
||
| 2210 | $schema = static::getSchema(); |
||
| 2211 | $manyManyComponent = $schema->manyManyComponent(static::class, $componentName); |
||
| 2212 | if (!$manyManyComponent) { |
||
| 2213 | throw new InvalidArgumentException(sprintf( |
||
| 2214 | "DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'", |
||
| 2215 | $componentName, |
||
| 2216 | static::class |
||
| 2217 | )); |
||
| 2218 | } |
||
| 2219 | |||
| 2220 | // If we haven't been written yet, we can't save these relations, so use a list that handles this case |
||
| 2221 | if (!$id) { |
||
| 2222 | if (!isset($this->unsavedRelations[$componentName])) { |
||
| 2223 | $this->unsavedRelations[$componentName] = new UnsavedRelationList( |
||
| 2224 | $manyManyComponent['parentClass'], |
||
| 2225 | $componentName, |
||
| 2226 | $manyManyComponent['childClass'] |
||
| 2227 | ); |
||
| 2228 | } |
||
| 2229 | return $this->unsavedRelations[$componentName]; |
||
| 2230 | } |
||
| 2231 | |||
| 2232 | $extraFields = $schema->manyManyExtraFieldsForComponent(static::class, $componentName) ?: []; |
||
| 2233 | /** @var RelationList $result */ |
||
| 2234 | $result = Injector::inst()->create( |
||
| 2235 | $manyManyComponent['relationClass'], |
||
| 2236 | $manyManyComponent['childClass'], |
||
| 2237 | $manyManyComponent['join'], |
||
| 2238 | $manyManyComponent['childField'], |
||
| 2239 | $manyManyComponent['parentField'], |
||
| 2240 | $extraFields, |
||
| 2241 | $manyManyComponent['parentClass'], |
||
| 2242 | static::class // In case ManyManyThroughList needs to use PolymorphicHasManyList internally |
||
| 2243 | ); |
||
| 2244 | |||
| 2245 | // Store component data in query meta-data |
||
| 2246 | $result = $result->alterDataQuery(function ($query) use ($extraFields) { |
||
| 2247 | /** @var DataQuery $query */ |
||
| 2248 | $query->setQueryParam('Component.ExtraFields', $extraFields); |
||
| 2249 | }); |
||
| 2250 | |||
| 2251 | // If we have a default sort set for our "join" then we should overwrite any default already set. |
||
| 2252 | $joinSort = Config::inst()->get($manyManyComponent['join'], 'default_sort'); |
||
| 2253 | if (!empty($joinSort)) { |
||
| 2254 | $result = $result->sort($joinSort); |
||
| 2255 | } |
||
| 2256 | |||
| 2257 | $this->extend('updateManyManyComponents', $result); |
||
| 2258 | |||
| 2259 | // If this is called on a singleton, then we return an 'orphaned relation' that can have the |
||
| 2260 | // foreignID set elsewhere. |
||
| 2261 | return $result |
||
| 2262 | ->setDataQueryParam($this->getInheritableQueryParams()) |
||
| 2263 | ->forForeignID($id); |
||
| 2264 | } |
||
| 2265 | |||
| 2266 | /** |
||
| 2267 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 2268 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 2269 | * |
||
| 2270 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 2271 | * their classes. |
||
| 2272 | */ |
||
| 2273 | public function hasOne() |
||
| 2274 | { |
||
| 2275 | return (array)$this->config()->get('has_one'); |
||
| 2276 | } |
||
| 2277 | |||
| 2278 | /** |
||
| 2279 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 2280 | * their class name will be returned. |
||
| 2281 | * |
||
| 2282 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2283 | * the field data stripped off. It defaults to TRUE. |
||
| 2284 | * @return string|array |
||
| 2285 | */ |
||
| 2286 | public function belongsTo($classOnly = true) |
||
| 2287 | { |
||
| 2288 | $belongsTo = (array)$this->config()->get('belongs_to'); |
||
| 2289 | if ($belongsTo && $classOnly) { |
||
| 2290 | return preg_replace('/(.+)?\..+/', '$1', $belongsTo); |
||
| 2291 | } else { |
||
| 2292 | return $belongsTo ? $belongsTo : []; |
||
| 2293 | } |
||
| 2294 | } |
||
| 2295 | |||
| 2296 | /** |
||
| 2297 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 2298 | * relationships and their classes will be returned. |
||
| 2299 | * |
||
| 2300 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2301 | * the field data stripped off. It defaults to TRUE. |
||
| 2302 | * @return string|array|false |
||
| 2303 | */ |
||
| 2304 | public function hasMany($classOnly = true) |
||
| 2305 | { |
||
| 2306 | $hasMany = (array)$this->config()->get('has_many'); |
||
| 2307 | if ($hasMany && $classOnly) { |
||
| 2308 | return preg_replace('/(.+)?\..+/', '$1', $hasMany); |
||
| 2309 | } else { |
||
| 2310 | return $hasMany ? $hasMany : []; |
||
| 2311 | } |
||
| 2312 | } |
||
| 2313 | |||
| 2314 | /** |
||
| 2315 | * Return the many-to-many extra fields specification. |
||
| 2316 | * |
||
| 2317 | * If you don't specify a component name, it returns all |
||
| 2318 | * extra fields for all components available. |
||
| 2319 | * |
||
| 2320 | * @return array|null |
||
| 2321 | */ |
||
| 2322 | public function manyManyExtraFields() |
||
| 2323 | { |
||
| 2324 | return $this->config()->get('many_many_extraFields'); |
||
| 2325 | } |
||
| 2326 | |||
| 2327 | /** |
||
| 2328 | * Return information about a many-to-many component. |
||
| 2329 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 2330 | * components are returned. |
||
| 2331 | * |
||
| 2332 | * @see DataObjectSchema::manyManyComponent() |
||
| 2333 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 2334 | */ |
||
| 2335 | public function manyMany() |
||
| 2336 | { |
||
| 2337 | $config = $this->config(); |
||
| 2338 | $manyManys = (array)$config->get('many_many'); |
||
| 2339 | $belongsManyManys = (array)$config->get('belongs_many_many'); |
||
| 2340 | $items = array_merge($manyManys, $belongsManyManys); |
||
| 2341 | return $items; |
||
| 2342 | } |
||
| 2343 | |||
| 2344 | /** |
||
| 2345 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 2346 | * |
||
| 2347 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 2348 | * |
||
| 2349 | * @param string $class |
||
| 2350 | * @return array|false |
||
| 2351 | */ |
||
| 2352 | public function database_extensions($class) |
||
| 2353 | { |
||
| 2354 | $extensions = Config::inst()->get($class, 'database_extensions', Config::UNINHERITED); |
||
| 2355 | if ($extensions) { |
||
| 2356 | return $extensions; |
||
| 2357 | } else { |
||
| 2358 | return false; |
||
| 2359 | } |
||
| 2360 | } |
||
| 2361 | |||
| 2362 | /** |
||
| 2363 | * Generates a SearchContext to be used for building and processing |
||
| 2364 | * a generic search form for properties on this object. |
||
| 2365 | * |
||
| 2366 | * @return SearchContext |
||
| 2367 | */ |
||
| 2368 | public function getDefaultSearchContext() |
||
| 2369 | { |
||
| 2370 | return SearchContext::create( |
||
| 2371 | static::class, |
||
| 2372 | $this->scaffoldSearchFields(), |
||
| 2373 | $this->defaultSearchFilters() |
||
| 2374 | ); |
||
| 2375 | } |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * Determine which properties on the DataObject are |
||
| 2379 | * searchable, and map them to their default {@link FormField} |
||
| 2380 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 2381 | * |
||
| 2382 | * Some additional logic is included for switching field labels, based on |
||
| 2383 | * how generic or specific the field type is. |
||
| 2384 | * |
||
| 2385 | * Used by {@link SearchContext}. |
||
| 2386 | * |
||
| 2387 | * @param array $_params |
||
| 2388 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 2389 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 2390 | * @return FieldList |
||
| 2391 | */ |
||
| 2392 | public function scaffoldSearchFields($_params = null) |
||
| 2393 | { |
||
| 2394 | $params = array_merge( |
||
| 2395 | [ |
||
| 2396 | 'fieldClasses' => false, |
||
| 2397 | 'restrictFields' => false |
||
| 2398 | ], |
||
| 2399 | (array)$_params |
||
| 2400 | ); |
||
| 2401 | $fields = new FieldList(); |
||
| 2402 | foreach ($this->searchableFields() as $fieldName => $spec) { |
||
| 2403 | if ($params['restrictFields'] && !in_array($fieldName, $params['restrictFields'])) { |
||
| 2404 | continue; |
||
| 2405 | } |
||
| 2406 | |||
| 2407 | // If a custom fieldclass is provided as a string, use it |
||
| 2408 | $field = null; |
||
| 2409 | if ($params['fieldClasses'] && isset($params['fieldClasses'][$fieldName])) { |
||
| 2410 | $fieldClass = $params['fieldClasses'][$fieldName]; |
||
| 2411 | $field = new $fieldClass($fieldName); |
||
| 2412 | // If we explicitly set a field, then construct that |
||
| 2413 | } elseif (isset($spec['field'])) { |
||
| 2414 | // If it's a string, use it as a class name and construct |
||
| 2415 | if (is_string($spec['field'])) { |
||
| 2416 | $fieldClass = $spec['field']; |
||
| 2417 | $field = new $fieldClass($fieldName); |
||
| 2418 | |||
| 2419 | // If it's a FormField object, then just use that object directly. |
||
| 2420 | } elseif ($spec['field'] instanceof FormField) { |
||
| 2421 | $field = $spec['field']; |
||
| 2422 | |||
| 2423 | // Otherwise we have a bug |
||
| 2424 | } else { |
||
| 2425 | user_error("Bad value for searchable_fields, 'field' value: " |
||
| 2426 | . var_export($spec['field'], true), E_USER_WARNING); |
||
| 2427 | } |
||
| 2428 | |||
| 2429 | // Otherwise, use the database field's scaffolder |
||
| 2430 | } elseif ($object = $this->relObject($fieldName)) { |
||
| 2431 | if (is_object($object) && $object->hasMethod('scaffoldSearchField')) { |
||
| 2432 | $field = $object->scaffoldSearchField(); |
||
| 2433 | } else { |
||
| 2434 | throw new Exception(sprintf( |
||
| 2435 | "SearchField '%s' on '%s' does not return a valid DBField instance.", |
||
| 2436 | $fieldName, |
||
| 2437 | get_class($this) |
||
| 2438 | )); |
||
| 2439 | } |
||
| 2440 | } |
||
| 2441 | |||
| 2442 | // Allow fields to opt out of search |
||
| 2443 | if (!$field) { |
||
| 2444 | continue; |
||
| 2445 | } |
||
| 2446 | |||
| 2447 | if (strstr($fieldName, '.')) { |
||
| 2448 | $field->setName(str_replace('.', '__', $fieldName)); |
||
| 2449 | } |
||
| 2450 | $field->setTitle($spec['title']); |
||
| 2451 | |||
| 2452 | $fields->push($field); |
||
| 2453 | } |
||
| 2454 | return $fields; |
||
| 2455 | } |
||
| 2456 | |||
| 2457 | /** |
||
| 2458 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2459 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2460 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2461 | * |
||
| 2462 | * @uses FormScaffolder |
||
| 2463 | * |
||
| 2464 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2465 | * @return FieldList |
||
| 2466 | */ |
||
| 2467 | public function scaffoldFormFields($_params = null) |
||
| 2468 | { |
||
| 2469 | $params = array_merge( |
||
| 2470 | [ |
||
| 2471 | 'tabbed' => false, |
||
| 2472 | 'includeRelations' => false, |
||
| 2473 | 'restrictFields' => false, |
||
| 2474 | 'fieldClasses' => false, |
||
| 2475 | 'ajaxSafe' => false |
||
| 2476 | ], |
||
| 2477 | (array)$_params |
||
| 2478 | ); |
||
| 2479 | |||
| 2480 | $fs = FormScaffolder::create($this); |
||
| 2481 | $fs->tabbed = $params['tabbed']; |
||
| 2482 | $fs->includeRelations = $params['includeRelations']; |
||
| 2483 | $fs->restrictFields = $params['restrictFields']; |
||
| 2484 | $fs->fieldClasses = $params['fieldClasses']; |
||
| 2485 | $fs->ajaxSafe = $params['ajaxSafe']; |
||
| 2486 | |||
| 2487 | $this->extend('updateFormScaffolder', $fs, $this); |
||
| 2488 | |||
| 2489 | return $fs->getFieldList(); |
||
| 2490 | } |
||
| 2491 | |||
| 2492 | /** |
||
| 2493 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2494 | * being called on extensions |
||
| 2495 | * |
||
| 2496 | * @param callable $callback The callback to execute |
||
| 2497 | */ |
||
| 2498 | protected function beforeUpdateCMSFields($callback) |
||
| 2499 | { |
||
| 2500 | $this->beforeExtending('updateCMSFields', $callback); |
||
| 2501 | } |
||
| 2502 | |||
| 2503 | /** |
||
| 2504 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2505 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2506 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2507 | * generate this set. To customize, overload this method in a subclass |
||
| 2508 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2509 | * |
||
| 2510 | * <code> |
||
| 2511 | * class MyCustomClass extends DataObject { |
||
| 2512 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2513 | * |
||
| 2514 | * function getCMSFields() { |
||
| 2515 | * $fields = parent::getCMSFields(); |
||
| 2516 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2517 | * return $fields; |
||
| 2518 | * } |
||
| 2519 | * } |
||
| 2520 | * </code> |
||
| 2521 | * |
||
| 2522 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2523 | * |
||
| 2524 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2525 | */ |
||
| 2526 | public function getCMSFields() |
||
| 2527 | { |
||
| 2528 | $tabbedFields = $this->scaffoldFormFields([ |
||
| 2529 | // Don't allow has_many/many_many relationship editing before the record is first saved |
||
| 2530 | 'includeRelations' => ($this->ID > 0), |
||
| 2531 | 'tabbed' => true, |
||
| 2532 | 'ajaxSafe' => true |
||
| 2533 | ]); |
||
| 2534 | |||
| 2535 | $this->extend('updateCMSFields', $tabbedFields); |
||
| 2536 | |||
| 2537 | return $tabbedFields; |
||
| 2538 | } |
||
| 2539 | |||
| 2540 | /** |
||
| 2541 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2542 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2543 | * |
||
| 2544 | * @return FieldList an Empty FieldList(); need to be overload by solid subclass |
||
| 2545 | */ |
||
| 2546 | public function getCMSActions() |
||
| 2547 | { |
||
| 2548 | $actions = new FieldList(); |
||
| 2549 | $this->extend('updateCMSActions', $actions); |
||
| 2550 | return $actions; |
||
| 2551 | } |
||
| 2552 | |||
| 2553 | /** |
||
| 2554 | * When extending this class and overriding this method, you will need to instantiate the CompositeValidator by |
||
| 2555 | * calling parent::getCMSCompositeValidator(). This will ensure that the appropriate extension point is also |
||
| 2556 | * invoked. |
||
| 2557 | * |
||
| 2558 | * You can also update the CompositeValidator by creating an Extension and implementing the |
||
| 2559 | * updateCMSCompositeValidator(CompositeValidator $compositeValidator) method. |
||
| 2560 | * |
||
| 2561 | * @see CompositeValidator for examples of implementation |
||
| 2562 | * @return CompositeValidator |
||
| 2563 | */ |
||
| 2564 | public function getCMSCompositeValidator(): CompositeValidator |
||
| 2565 | { |
||
| 2566 | $compositeValidator = CompositeValidator::create(); |
||
| 2567 | |||
| 2568 | // Support for the old method during the deprecation period |
||
| 2569 | if ($this->hasMethod('getCMSValidator')) { |
||
| 2570 | Deprecation::notice( |
||
| 2571 | '4.6', |
||
| 2572 | 'getCMSValidator() is removed in 5.0 in favour of getCMSCompositeValidator()' |
||
| 2573 | ); |
||
| 2574 | |||
| 2575 | $compositeValidator->addValidator($this->getCMSValidator()); |
||
| 2576 | } |
||
| 2577 | |||
| 2578 | // Extend validator - forward support, will be supported beyond 5.0.0 |
||
| 2579 | $this->invokeWithExtensions('updateCMSCompositeValidator', $compositeValidator); |
||
| 2580 | |||
| 2581 | return $compositeValidator; |
||
| 2582 | } |
||
| 2583 | |||
| 2584 | /** |
||
| 2585 | * Used for simple frontend forms without relation editing |
||
| 2586 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2587 | * by default. To customize, either overload this method in your |
||
| 2588 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2589 | * |
||
| 2590 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2591 | * |
||
| 2592 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2593 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2594 | */ |
||
| 2595 | public function getFrontEndFields($params = null) |
||
| 2596 | { |
||
| 2597 | $untabbedFields = $this->scaffoldFormFields($params); |
||
| 2598 | $this->extend('updateFrontEndFields', $untabbedFields); |
||
| 2599 | |||
| 2600 | return $untabbedFields; |
||
| 2601 | } |
||
| 2602 | |||
| 2603 | public function getViewerTemplates($suffix = '') |
||
| 2604 | { |
||
| 2605 | return SSViewer::get_templates_by_class(static::class, $suffix, $this->baseClass()); |
||
| 2606 | } |
||
| 2607 | |||
| 2608 | /** |
||
| 2609 | * Gets the value of a field. |
||
| 2610 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2611 | * |
||
| 2612 | * @param string $field The name of the field |
||
| 2613 | * @return mixed The field value |
||
| 2614 | */ |
||
| 2615 | public function getField($field) |
||
| 2616 | { |
||
| 2617 | // If we already have a value in $this->record, then we should just return that |
||
| 2618 | if (isset($this->record[$field])) { |
||
| 2619 | return $this->record[$field]; |
||
| 2620 | } |
||
| 2621 | |||
| 2622 | // Do we have a field that needs to be lazy loaded? |
||
| 2623 | if (isset($this->record[$field . '_Lazy'])) { |
||
| 2624 | $tableClass = $this->record[$field . '_Lazy']; |
||
| 2625 | $this->loadLazyFields($tableClass); |
||
| 2626 | } |
||
| 2627 | $schema = static::getSchema(); |
||
| 2628 | |||
| 2629 | // Support unary relations as fields |
||
| 2630 | if ($schema->unaryComponent(static::class, $field)) { |
||
| 2631 | return $this->getComponent($field); |
||
| 2632 | } |
||
| 2633 | |||
| 2634 | // In case of complex fields, return the DBField object |
||
| 2635 | if ($schema->compositeField(static::class, $field)) { |
||
| 2636 | $this->record[$field] = $this->dbObject($field); |
||
| 2637 | } |
||
| 2638 | |||
| 2639 | return isset($this->record[$field]) ? $this->record[$field] : null; |
||
| 2640 | } |
||
| 2641 | |||
| 2642 | /** |
||
| 2643 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2644 | * |
||
| 2645 | * @param string $class Class to load the values from. Others are joined as required. |
||
| 2646 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2647 | * @return bool Flag if lazy loading succeeded |
||
| 2648 | */ |
||
| 2649 | protected function loadLazyFields($class = null) |
||
| 2724 | } |
||
| 2725 | |||
| 2726 | /** |
||
| 2727 | * Return the fields that have changed since the last write. |
||
| 2728 | * |
||
| 2729 | * The change level affects what the functions defines as "changed": |
||
| 2730 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2731 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2732 | * for example a change from 0 to null would not be included. |
||
| 2733 | * |
||
| 2734 | * Example return: |
||
| 2735 | * <code> |
||
| 2736 | * array( |
||
| 2737 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2738 | * ) |
||
| 2739 | * </code> |
||
| 2740 | * |
||
| 2741 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2742 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2743 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2744 | * @return array |
||
| 2745 | */ |
||
| 2746 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) |
||
| 2747 | { |
||
| 2748 | $changedFields = []; |
||
| 2749 | |||
| 2750 | // Update the changed array with references to changed obj-fields |
||
| 2751 | foreach ($this->record as $k => $v) { |
||
| 2752 | // Prevents DBComposite infinite looping on isChanged |
||
| 2753 | if (is_array($databaseFieldsOnly) && !in_array($k, $databaseFieldsOnly)) { |
||
| 2754 | continue; |
||
| 2755 | } |
||
| 2756 | if (is_object($v) && method_exists($v, 'isChanged') && $v->isChanged()) { |
||
| 2757 | $this->changed[$k] = self::CHANGE_VALUE; |
||
| 2758 | } |
||
| 2759 | } |
||
| 2760 | |||
| 2761 | // If change was forced, then derive change data from $this->record |
||
| 2762 | if ($this->changeForced && $changeLevel <= self::CHANGE_STRICT) { |
||
| 2763 | $changed = array_combine( |
||
| 2764 | array_keys($this->record), |
||
| 2765 | array_fill(0, count($this->record), self::CHANGE_STRICT) |
||
| 2766 | ); |
||
| 2767 | // @todo Find better way to allow versioned to write a new version after forceChange |
||
| 2768 | unset($changed['Version']); |
||
| 2769 | } else { |
||
| 2770 | $changed = $this->changed; |
||
| 2771 | } |
||
| 2772 | |||
| 2773 | if (is_array($databaseFieldsOnly)) { |
||
| 2774 | $fields = array_intersect_key($changed, array_flip($databaseFieldsOnly)); |
||
| 2775 | } elseif ($databaseFieldsOnly) { |
||
| 2776 | $fieldsSpecs = static::getSchema()->fieldSpecs(static::class); |
||
| 2777 | $fields = array_intersect_key($changed, $fieldsSpecs); |
||
| 2778 | } else { |
||
| 2779 | $fields = $changed; |
||
| 2780 | } |
||
| 2781 | |||
| 2782 | // Filter the list to those of a certain change level |
||
| 2783 | if ($changeLevel > self::CHANGE_STRICT) { |
||
| 2784 | if ($fields) { |
||
| 2785 | foreach ($fields as $name => $level) { |
||
| 2786 | if ($level < $changeLevel) { |
||
| 2787 | unset($fields[$name]); |
||
| 2788 | } |
||
| 2789 | } |
||
| 2790 | } |
||
| 2791 | } |
||
| 2792 | |||
| 2793 | if ($fields) { |
||
| 2794 | foreach ($fields as $name => $level) { |
||
| 2795 | $changedFields[$name] = [ |
||
| 2796 | 'before' => array_key_exists($name, $this->original) ? $this->original[$name] : null, |
||
| 2797 | 'after' => array_key_exists($name, $this->record) ? $this->record[$name] : null, |
||
| 2798 | 'level' => $level |
||
| 2799 | ]; |
||
| 2800 | } |
||
| 2801 | } |
||
| 2802 | |||
| 2803 | return $changedFields; |
||
| 2804 | } |
||
| 2805 | |||
| 2806 | /** |
||
| 2807 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2808 | * since loading them from the database. |
||
| 2809 | * |
||
| 2810 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2811 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2812 | * @return boolean |
||
| 2813 | */ |
||
| 2814 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) |
||
| 2815 | { |
||
| 2816 | $fields = $fieldName ? [$fieldName] : true; |
||
| 2817 | $changed = $this->getChangedFields($fields, $changeLevel); |
||
| 2818 | if (!isset($fieldName)) { |
||
| 2819 | return !empty($changed); |
||
| 2820 | } else { |
||
| 2821 | return array_key_exists($fieldName, $changed); |
||
| 2822 | } |
||
| 2823 | } |
||
| 2824 | |||
| 2825 | /** |
||
| 2826 | * Set the value of the field |
||
| 2827 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2828 | * |
||
| 2829 | * @param string $fieldName Name of the field |
||
| 2830 | * @param mixed $val New field value |
||
| 2831 | * @return $this |
||
| 2832 | */ |
||
| 2833 | public function setField($fieldName, $val) |
||
| 2834 | { |
||
| 2835 | $this->objCacheClear(); |
||
| 2836 | //if it's a has_one component, destroy the cache |
||
| 2837 | if (substr($fieldName, -2) == 'ID') { |
||
| 2838 | unset($this->components[substr($fieldName, 0, -2)]); |
||
| 2839 | } |
||
| 2840 | |||
| 2841 | // If we've just lazy-loaded the column, then we need to populate the $original array |
||
| 2842 | if (isset($this->record[$fieldName . '_Lazy'])) { |
||
| 2843 | $tableClass = $this->record[$fieldName . '_Lazy']; |
||
| 2844 | $this->loadLazyFields($tableClass); |
||
| 2845 | } |
||
| 2846 | |||
| 2847 | // Support component assignent via field setter |
||
| 2848 | $schema = static::getSchema(); |
||
| 2849 | if ($schema->unaryComponent(static::class, $fieldName)) { |
||
| 2850 | unset($this->components[$fieldName]); |
||
| 2851 | // Assign component directly |
||
| 2852 | if (is_null($val) || $val instanceof DataObject) { |
||
| 2853 | return $this->setComponent($fieldName, $val); |
||
| 2854 | } |
||
| 2855 | // Assign by ID instead of object |
||
| 2856 | if (is_numeric($val)) { |
||
| 2857 | $fieldName .= 'ID'; |
||
| 2858 | } |
||
| 2859 | } |
||
| 2860 | |||
| 2861 | // Situation 1: Passing an DBField |
||
| 2862 | if ($val instanceof DBField) { |
||
| 2863 | $val->setName($fieldName); |
||
| 2864 | $val->saveInto($this); |
||
| 2865 | |||
| 2866 | // Situation 1a: Composite fields should remain bound in case they are |
||
| 2867 | // later referenced to update the parent dataobject |
||
| 2868 | if ($val instanceof DBComposite) { |
||
| 2869 | $val->bindTo($this); |
||
| 2870 | $this->record[$fieldName] = $val; |
||
| 2871 | } |
||
| 2872 | // Situation 2: Passing a literal or non-DBField object |
||
| 2873 | } else { |
||
| 2874 | // If this is a proper database field, we shouldn't be getting non-DBField objects |
||
| 2875 | if (is_object($val) && $schema->fieldSpec(static::class, $fieldName)) { |
||
| 2876 | throw new InvalidArgumentException('DataObject::setField: passed an object that is not a DBField'); |
||
| 2877 | } |
||
| 2878 | |||
| 2879 | if (!empty($val) && !is_scalar($val)) { |
||
| 2880 | $dbField = $this->dbObject($fieldName); |
||
| 2881 | if ($dbField && $dbField->scalarValueOnly()) { |
||
| 2882 | throw new InvalidArgumentException( |
||
| 2883 | sprintf( |
||
| 2884 | 'DataObject::setField: %s only accepts scalars', |
||
| 2885 | $fieldName |
||
| 2886 | ) |
||
| 2887 | ); |
||
| 2888 | } |
||
| 2889 | } |
||
| 2890 | |||
| 2891 | // if a field is not existing or has strictly changed |
||
| 2892 | if (!array_key_exists($fieldName, $this->original) || $this->original[$fieldName] !== $val) { |
||
| 2893 | // TODO Add check for php-level defaults which are not set in the db |
||
| 2894 | // TODO Add check for hidden input-fields (readonly) which are not set in the db |
||
| 2895 | // At the very least, the type has changed |
||
| 2896 | $this->changed[$fieldName] = self::CHANGE_STRICT; |
||
| 2897 | |||
| 2898 | if ((!array_key_exists($fieldName, $this->original) && $val) |
||
| 2899 | || (array_key_exists($fieldName, $this->original) && $this->original[$fieldName] != $val) |
||
| 2900 | ) { |
||
| 2901 | // Value has changed as well, not just the type |
||
| 2902 | $this->changed[$fieldName] = self::CHANGE_VALUE; |
||
| 2903 | } |
||
| 2904 | // Value has been restored to its original, remove any record of the change |
||
| 2905 | } elseif (isset($this->changed[$fieldName])) { |
||
| 2906 | unset($this->changed[$fieldName]); |
||
| 2907 | } |
||
| 2908 | |||
| 2909 | // Value is saved regardless, since the change detection relates to the last write |
||
| 2910 | $this->record[$fieldName] = $val; |
||
| 2911 | } |
||
| 2912 | return $this; |
||
| 2913 | } |
||
| 2914 | |||
| 2915 | /** |
||
| 2916 | * Set the value of the field, using a casting object. |
||
| 2917 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2918 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2919 | * can be saved into the Image table. |
||
| 2920 | * |
||
| 2921 | * @param string $fieldName Name of the field |
||
| 2922 | * @param mixed $value New field value |
||
| 2923 | * @return $this |
||
| 2924 | */ |
||
| 2925 | public function setCastedField($fieldName, $value) |
||
| 2926 | { |
||
| 2927 | if (!$fieldName) { |
||
| 2928 | throw new InvalidArgumentException("DataObject::setCastedField: Called without a fieldName"); |
||
| 2929 | } |
||
| 2930 | $fieldObj = $this->dbObject($fieldName); |
||
| 2931 | if ($fieldObj) { |
||
| 2932 | $fieldObj->setValue($value); |
||
| 2933 | $fieldObj->saveInto($this); |
||
| 2934 | } else { |
||
| 2935 | $this->$fieldName = $value; |
||
| 2936 | } |
||
| 2937 | return $this; |
||
| 2938 | } |
||
| 2939 | |||
| 2940 | /** |
||
| 2941 | * {@inheritdoc} |
||
| 2942 | */ |
||
| 2943 | public function castingHelper($field) |
||
| 2944 | { |
||
| 2945 | $fieldSpec = static::getSchema()->fieldSpec(static::class, $field); |
||
| 2946 | if ($fieldSpec) { |
||
| 2947 | return $fieldSpec; |
||
| 2948 | } |
||
| 2949 | |||
| 2950 | // many_many_extraFields aren't presented by db(), so we check if the source query params |
||
| 2951 | // provide us with meta-data for a many_many relation we can inspect for extra fields. |
||
| 2952 | $queryParams = $this->getSourceQueryParams(); |
||
| 2953 | if (!empty($queryParams['Component.ExtraFields'])) { |
||
| 2954 | $extraFields = $queryParams['Component.ExtraFields']; |
||
| 2955 | |||
| 2956 | if (isset($extraFields[$field])) { |
||
| 2957 | return $extraFields[$field]; |
||
| 2958 | } |
||
| 2959 | } |
||
| 2960 | |||
| 2961 | return parent::castingHelper($field); |
||
| 2962 | } |
||
| 2963 | |||
| 2964 | /** |
||
| 2965 | * Returns true if the given field exists in a database column on any of |
||
| 2966 | * the objects tables and optionally look up a dynamic getter with |
||
| 2967 | * get<fieldName>(). |
||
| 2968 | * |
||
| 2969 | * @param string $field Name of the field |
||
| 2970 | * @return boolean True if the given field exists |
||
| 2971 | */ |
||
| 2972 | public function hasField($field) |
||
| 2973 | { |
||
| 2974 | $schema = static::getSchema(); |
||
| 2975 | return ( |
||
| 2976 | array_key_exists($field, $this->record) |
||
| 2977 | || array_key_exists($field, $this->components) |
||
| 2978 | || $schema->fieldSpec(static::class, $field) |
||
| 2979 | || $schema->unaryComponent(static::class, $field) |
||
| 2980 | || $this->hasMethod("get{$field}") |
||
| 2981 | ); |
||
| 2982 | } |
||
| 2983 | |||
| 2984 | /** |
||
| 2985 | * Returns true if the given field exists as a database column |
||
| 2986 | * |
||
| 2987 | * @param string $field Name of the field |
||
| 2988 | * |
||
| 2989 | * @return boolean |
||
| 2990 | */ |
||
| 2991 | public function hasDatabaseField($field) |
||
| 2992 | { |
||
| 2993 | $spec = static::getSchema()->fieldSpec(static::class, $field, DataObjectSchema::DB_ONLY); |
||
| 2994 | return !empty($spec); |
||
| 2995 | } |
||
| 2996 | |||
| 2997 | /** |
||
| 2998 | * Returns true if the member is allowed to do the given action. |
||
| 2999 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 3000 | * |
||
| 3001 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 3002 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 3003 | * in user. |
||
| 3004 | * @param array $context Additional $context to pass to extendedCan() |
||
| 3005 | * |
||
| 3006 | * @return boolean True if the the member is allowed to do the given action |
||
| 3007 | */ |
||
| 3008 | public function can($perm, $member = null, $context = []) |
||
| 3009 | { |
||
| 3010 | if (!$member) { |
||
| 3011 | $member = Security::getCurrentUser(); |
||
| 3012 | } |
||
| 3013 | |||
| 3014 | if ($member && Permission::checkMember($member, "ADMIN")) { |
||
| 3015 | return true; |
||
| 3016 | } |
||
| 3017 | |||
| 3018 | if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) { |
||
| 3019 | $method = 'can' . ucfirst($perm); |
||
| 3020 | return $this->$method($member); |
||
| 3021 | } |
||
| 3022 | |||
| 3023 | $results = $this->extendedCan('can', $member); |
||
| 3024 | if (isset($results)) { |
||
| 3025 | return $results; |
||
| 3026 | } |
||
| 3027 | |||
| 3028 | return ($member && Permission::checkMember($member, $perm)); |
||
| 3029 | } |
||
| 3030 | |||
| 3031 | /** |
||
| 3032 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 3033 | * expected to return one of three values: |
||
| 3034 | * |
||
| 3035 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 3036 | * - true: Allow this permission, as long as no other extensions return false |
||
| 3037 | * - NULL: Don't affect the outcome |
||
| 3038 | * |
||
| 3039 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 3040 | * |
||
| 3041 | * <code> |
||
| 3042 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 3043 | * if($extended !== null) return $extended; |
||
| 3044 | * else return $normalValue; |
||
| 3045 | * </code> |
||
| 3046 | * |
||
| 3047 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 3048 | * @param Member|int $member |
||
| 3049 | * @param array $context Optional context |
||
| 3050 | * @return boolean|null |
||
| 3051 | */ |
||
| 3052 | public function extendedCan($methodName, $member, $context = []) |
||
| 3053 | { |
||
| 3054 | $results = $this->extend($methodName, $member, $context); |
||
| 3055 | if ($results && is_array($results)) { |
||
| 3056 | // Remove NULLs |
||
| 3057 | $results = array_filter($results, function ($v) { |
||
| 3058 | return !is_null($v); |
||
| 3059 | }); |
||
| 3060 | // If there are any non-NULL responses, then return the lowest one of them. |
||
| 3061 | // If any explicitly deny the permission, then we don't get access |
||
| 3062 | if ($results) { |
||
| 3063 | return min($results); |
||
| 3064 | } |
||
| 3065 | } |
||
| 3066 | return null; |
||
| 3067 | } |
||
| 3068 | |||
| 3069 | /** |
||
| 3070 | * @param Member $member |
||
| 3071 | * @return boolean |
||
| 3072 | */ |
||
| 3073 | public function canView($member = null) |
||
| 3074 | { |
||
| 3075 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 3076 | if ($extended !== null) { |
||
| 3077 | return $extended; |
||
| 3078 | } |
||
| 3079 | return Permission::check('ADMIN', 'any', $member); |
||
| 3080 | } |
||
| 3081 | |||
| 3082 | /** |
||
| 3083 | * @param Member $member |
||
| 3084 | * @return boolean |
||
| 3085 | */ |
||
| 3086 | public function canEdit($member = null) |
||
| 3087 | { |
||
| 3088 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 3089 | if ($extended !== null) { |
||
| 3090 | return $extended; |
||
| 3091 | } |
||
| 3092 | return Permission::check('ADMIN', 'any', $member); |
||
| 3093 | } |
||
| 3094 | |||
| 3095 | /** |
||
| 3096 | * @param Member $member |
||
| 3097 | * @return boolean |
||
| 3098 | */ |
||
| 3099 | public function canDelete($member = null) |
||
| 3100 | { |
||
| 3101 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 3102 | if ($extended !== null) { |
||
| 3103 | return $extended; |
||
| 3104 | } |
||
| 3105 | return Permission::check('ADMIN', 'any', $member); |
||
| 3106 | } |
||
| 3107 | |||
| 3108 | /** |
||
| 3109 | * @param Member $member |
||
| 3110 | * @param array $context Additional context-specific data which might |
||
| 3111 | * affect whether (or where) this object could be created. |
||
| 3112 | * @return boolean |
||
| 3113 | */ |
||
| 3114 | public function canCreate($member = null, $context = []) |
||
| 3115 | { |
||
| 3116 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
| 3117 | if ($extended !== null) { |
||
| 3118 | return $extended; |
||
| 3119 | } |
||
| 3120 | return Permission::check('ADMIN', 'any', $member); |
||
| 3121 | } |
||
| 3122 | |||
| 3123 | /** |
||
| 3124 | * Debugging used by Debug::show() |
||
| 3125 | * |
||
| 3126 | * @return string HTML data representing this object |
||
| 3127 | */ |
||
| 3128 | public function debug() |
||
| 3129 | { |
||
| 3130 | $class = static::class; |
||
| 3131 | $val = "<h3>Database record: {$class}</h3>\n<ul>\n"; |
||
| 3132 | if ($this->record) { |
||
| 3133 | foreach ($this->record as $fieldName => $fieldVal) { |
||
| 3134 | $val .= "\t<li>$fieldName: " . Debug::text($fieldVal) . "</li>\n"; |
||
| 3135 | } |
||
| 3136 | } |
||
| 3137 | $val .= "</ul>\n"; |
||
| 3138 | return $val; |
||
| 3139 | } |
||
| 3140 | |||
| 3141 | /** |
||
| 3142 | * Return the DBField object that represents the given field. |
||
| 3143 | * This works similarly to obj() with 2 key differences: |
||
| 3144 | * - it still returns an object even when the field has no value. |
||
| 3145 | * - it only matches fields and not methods |
||
| 3146 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 3147 | * |
||
| 3148 | * @param string $fieldName Name of the field |
||
| 3149 | * @return DBField The field as a DBField object |
||
| 3150 | */ |
||
| 3151 | public function dbObject($fieldName) |
||
| 3152 | { |
||
| 3153 | // Check for field in DB |
||
| 3154 | $schema = static::getSchema(); |
||
| 3155 | $helper = $schema->fieldSpec(static::class, $fieldName, DataObjectSchema::INCLUDE_CLASS); |
||
| 3156 | if (!$helper) { |
||
| 3157 | return null; |
||
| 3158 | } |
||
| 3159 | |||
| 3160 | if (!isset($this->record[$fieldName]) && isset($this->record[$fieldName . '_Lazy'])) { |
||
| 3161 | $tableClass = $this->record[$fieldName . '_Lazy']; |
||
| 3162 | $this->loadLazyFields($tableClass); |
||
| 3163 | } |
||
| 3164 | |||
| 3165 | $value = isset($this->record[$fieldName]) |
||
| 3166 | ? $this->record[$fieldName] |
||
| 3167 | : null; |
||
| 3168 | |||
| 3169 | // If we have a DBField object in $this->record, then return that |
||
| 3170 | if ($value instanceof DBField) { |
||
| 3171 | return $value; |
||
| 3172 | } |
||
| 3173 | |||
| 3174 | $pos = strpos($helper, '.'); |
||
| 3175 | $class = substr($helper, 0, $pos); |
||
| 3176 | $spec = substr($helper, $pos + 1); |
||
| 3177 | |||
| 3178 | /** @var DBField $obj */ |
||
| 3179 | $table = $schema->tableName($class); |
||
| 3180 | $obj = Injector::inst()->create($spec, $fieldName); |
||
| 3181 | $obj->setTable($table); |
||
| 3182 | $obj->setValue($value, $this, false); |
||
| 3183 | return $obj; |
||
| 3184 | } |
||
| 3185 | |||
| 3186 | /** |
||
| 3187 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 3188 | * |
||
| 3189 | * The path to the related field is specified with dot separated syntax |
||
| 3190 | * (eg: Parent.Child.Child.FieldName). |
||
| 3191 | * |
||
| 3192 | * If a relation is blank, this will return null instead. |
||
| 3193 | * If a relation name is invalid (e.g. non-relation on a parent) this |
||
| 3194 | * can throw a LogicException. |
||
| 3195 | * |
||
| 3196 | * @param string $fieldPath List of paths on this object. All items in this path |
||
| 3197 | * must be ViewableData implementors |
||
| 3198 | * |
||
| 3199 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 3200 | * @throws LogicException If accessing invalid relations |
||
| 3201 | */ |
||
| 3202 | public function relObject($fieldPath) |
||
| 3203 | { |
||
| 3204 | $object = null; |
||
| 3205 | $component = $this; |
||
| 3206 | |||
| 3207 | // Parse all relations |
||
| 3208 | foreach (explode('.', $fieldPath) as $relation) { |
||
| 3209 | if (!$component) { |
||
| 3210 | return null; |
||
| 3211 | } |
||
| 3212 | |||
| 3213 | // Inspect relation type |
||
| 3214 | if (ClassInfo::hasMethod($component, $relation)) { |
||
| 3215 | $component = $component->$relation(); |
||
| 3216 | } elseif ($component instanceof Relation || $component instanceof DataList) { |
||
| 3217 | // $relation could either be a field (aggregate), or another relation |
||
| 3218 | $singleton = DataObject::singleton($component->dataClass()); |
||
| 3219 | $component = $singleton->dbObject($relation) ?: $component->relation($relation); |
||
| 3220 | } elseif ($component instanceof DataObject && ($dbObject = $component->dbObject($relation))) { |
||
| 3221 | $component = $dbObject; |
||
| 3222 | } elseif ($component instanceof ViewableData && $component->hasField($relation)) { |
||
| 3223 | $component = $component->obj($relation); |
||
| 3224 | } else { |
||
| 3225 | throw new LogicException( |
||
| 3226 | "$relation is not a relation/field on " . get_class($component) |
||
| 3227 | ); |
||
| 3228 | } |
||
| 3229 | } |
||
| 3230 | return $component; |
||
| 3231 | } |
||
| 3232 | |||
| 3233 | /** |
||
| 3234 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 3235 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 3236 | * |
||
| 3237 | * @param string $fieldName string |
||
| 3238 | * @return mixed Will return null on a missing value |
||
| 3239 | */ |
||
| 3240 | public function relField($fieldName) |
||
| 3241 | { |
||
| 3242 | // Navigate to relative parent using relObject() if needed |
||
| 3243 | $component = $this; |
||
| 3244 | if (($pos = strrpos($fieldName, '.')) !== false) { |
||
| 3245 | $relation = substr($fieldName, 0, $pos); |
||
| 3246 | $fieldName = substr($fieldName, $pos + 1); |
||
| 3247 | $component = $this->relObject($relation); |
||
| 3248 | } |
||
| 3249 | |||
| 3250 | // Bail if the component is null |
||
| 3251 | if (!$component) { |
||
| 3252 | return null; |
||
| 3253 | } |
||
| 3254 | if (ClassInfo::hasMethod($component, $fieldName)) { |
||
| 3255 | return $component->$fieldName(); |
||
| 3256 | } |
||
| 3257 | return $component->$fieldName; |
||
| 3258 | } |
||
| 3259 | |||
| 3260 | /** |
||
| 3261 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 3262 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 3263 | * |
||
| 3264 | * @param string $className |
||
| 3265 | * @return string |
||
| 3266 | */ |
||
| 3267 | public function getReverseAssociation($className) |
||
| 3268 | { |
||
| 3269 | if (is_array($this->manyMany())) { |
||
| 3270 | $many_many = array_flip($this->manyMany()); |
||
| 3271 | if (array_key_exists($className, $many_many)) { |
||
| 3272 | return $many_many[$className]; |
||
| 3273 | } |
||
| 3274 | } |
||
| 3275 | if (is_array($this->hasMany())) { |
||
| 3276 | $has_many = array_flip($this->hasMany()); |
||
| 3277 | if (array_key_exists($className, $has_many)) { |
||
| 3278 | return $has_many[$className]; |
||
| 3279 | } |
||
| 3280 | } |
||
| 3281 | if (is_array($this->hasOne())) { |
||
| 3282 | $has_one = array_flip($this->hasOne()); |
||
| 3283 | if (array_key_exists($className, $has_one)) { |
||
| 3284 | return $has_one[$className]; |
||
| 3285 | } |
||
| 3286 | } |
||
| 3287 | |||
| 3288 | return false; |
||
| 3289 | } |
||
| 3290 | |||
| 3291 | /** |
||
| 3292 | * Return all objects matching the filter |
||
| 3293 | * sub-classes are automatically selected and included |
||
| 3294 | * |
||
| 3295 | * @param string $callerClass The class of objects to be returned |
||
| 3296 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3297 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3298 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 3299 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 3300 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 3301 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 3302 | * @param string $containerClass The container class to return the results in. |
||
| 3303 | * |
||
| 3304 | * @todo $containerClass is Ignored, why? |
||
| 3305 | * |
||
| 3306 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 3307 | */ |
||
| 3308 | public static function get( |
||
| 3309 | $callerClass = null, |
||
| 3310 | $filter = "", |
||
| 3311 | $sort = "", |
||
| 3312 | $join = "", |
||
| 3313 | $limit = null, |
||
| 3314 | $containerClass = DataList::class |
||
| 3315 | ) { |
||
| 3316 | // Validate arguments |
||
| 3317 | if ($callerClass == null) { |
||
| 3318 | $callerClass = get_called_class(); |
||
| 3319 | if ($callerClass === self::class) { |
||
| 3320 | throw new InvalidArgumentException('Call <classname>::get() instead of DataObject::get()'); |
||
| 3321 | } |
||
| 3322 | if ($filter || $sort || $join || $limit || ($containerClass !== DataList::class)) { |
||
| 3323 | throw new InvalidArgumentException('If calling <classname>::get() then you shouldn\'t pass any other' |
||
| 3324 | . ' arguments'); |
||
| 3325 | } |
||
| 3326 | } elseif ($callerClass === self::class) { |
||
| 3327 | throw new InvalidArgumentException('DataObject::get() cannot query non-subclass DataObject directly'); |
||
| 3328 | } |
||
| 3329 | if ($join) { |
||
| 3330 | throw new InvalidArgumentException( |
||
| 3331 | 'The $join argument has been removed. Use leftJoin($table, $joinClause) instead.' |
||
| 3332 | ); |
||
| 3333 | } |
||
| 3334 | |||
| 3335 | // Build and decorate with args |
||
| 3336 | $result = DataList::create($callerClass); |
||
| 3337 | if ($filter) { |
||
| 3338 | $result = $result->where($filter); |
||
| 3339 | } |
||
| 3340 | if ($sort) { |
||
| 3341 | $result = $result->sort($sort); |
||
| 3342 | } |
||
| 3343 | if ($limit && strpos($limit, ',') !== false) { |
||
| 3344 | $limitArguments = explode(',', $limit); |
||
| 3345 | $result = $result->limit($limitArguments[1], $limitArguments[0]); |
||
| 3346 | } elseif ($limit) { |
||
| 3347 | $result = $result->limit($limit); |
||
| 3348 | } |
||
| 3349 | |||
| 3350 | return $result; |
||
| 3351 | } |
||
| 3352 | |||
| 3353 | |||
| 3354 | /** |
||
| 3355 | * Return the first item matching the given query. |
||
| 3356 | * |
||
| 3357 | * The object returned is cached, unlike DataObject::get()->first() {@link DataList::first()} |
||
| 3358 | * and DataObject::get()->last() {@link DataList::last()} |
||
| 3359 | * |
||
| 3360 | * The filter argument supports parameterised queries (see SQLSelect::addWhere() for syntax examples). Because |
||
| 3361 | * of that (and differently from e.g. DataList::filter()) you need to manually escape the field names: |
||
| 3362 | * <code> |
||
| 3363 | * $member = DataObject::get_one('Member', [ '"FirstName"' => 'John' ]); |
||
| 3364 | * </code> |
||
| 3365 | * |
||
| 3366 | * @param string $callerClass The class of objects to be returned |
||
| 3367 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3368 | * @param boolean $cache Use caching |
||
| 3369 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 3370 | * |
||
| 3371 | * @return DataObject|null The first item matching the query |
||
| 3372 | */ |
||
| 3373 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") |
||
| 3374 | { |
||
| 3375 | /** @var DataObject $singleton */ |
||
| 3376 | $singleton = singleton($callerClass); |
||
| 3377 | |||
| 3378 | $cacheComponents = [$filter, $orderby, $singleton->getUniqueKeyComponents()]; |
||
| 3379 | $cacheKey = md5(serialize($cacheComponents)); |
||
| 3380 | |||
| 3381 | $item = null; |
||
| 3382 | if (!$cache || !isset(self::$_cache_get_one[$callerClass][$cacheKey])) { |
||
| 3383 | $dl = DataObject::get($callerClass)->where($filter)->sort($orderby); |
||
| 3384 | $item = $dl->first(); |
||
| 3385 | |||
| 3386 | if ($cache) { |
||
| 3387 | self::$_cache_get_one[$callerClass][$cacheKey] = $item; |
||
| 3388 | if (!self::$_cache_get_one[$callerClass][$cacheKey]) { |
||
| 3389 | self::$_cache_get_one[$callerClass][$cacheKey] = false; |
||
| 3390 | } |
||
| 3391 | } |
||
| 3392 | } |
||
| 3393 | |||
| 3394 | if ($cache) { |
||
| 3395 | return self::$_cache_get_one[$callerClass][$cacheKey] ?: null; |
||
| 3396 | } |
||
| 3397 | |||
| 3398 | return $item; |
||
| 3399 | } |
||
| 3400 | |||
| 3401 | /** |
||
| 3402 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 3403 | * Also clears any cached aggregate data. |
||
| 3404 | * |
||
| 3405 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 3406 | * When false will just clear session-local cached data |
||
| 3407 | * @return DataObject $this |
||
| 3408 | */ |
||
| 3409 | public function flushCache($persistent = true) |
||
| 3410 | { |
||
| 3411 | if (static::class == self::class) { |
||
| 3412 | self::$_cache_get_one = []; |
||
| 3413 | return $this; |
||
| 3414 | } |
||
| 3415 | |||
| 3416 | $classes = ClassInfo::ancestry(static::class); |
||
| 3417 | foreach ($classes as $class) { |
||
| 3418 | if (isset(self::$_cache_get_one[$class])) { |
||
| 3419 | unset(self::$_cache_get_one[$class]); |
||
| 3420 | } |
||
| 3421 | } |
||
| 3422 | |||
| 3423 | $this->extend('flushCache'); |
||
| 3424 | |||
| 3425 | $this->components = []; |
||
| 3426 | return $this; |
||
| 3427 | } |
||
| 3428 | |||
| 3429 | /** |
||
| 3430 | * Flush the get_one global cache and destroy associated objects. |
||
| 3431 | */ |
||
| 3432 | public static function flush_and_destroy_cache() |
||
| 3433 | { |
||
| 3434 | if (self::$_cache_get_one) { |
||
| 3435 | foreach (self::$_cache_get_one as $class => $items) { |
||
| 3436 | if (is_array($items)) { |
||
| 3437 | foreach ($items as $item) { |
||
| 3438 | if ($item) { |
||
| 3439 | $item->destroy(); |
||
| 3440 | } |
||
| 3441 | } |
||
| 3442 | } |
||
| 3443 | } |
||
| 3444 | } |
||
| 3445 | self::$_cache_get_one = []; |
||
| 3446 | } |
||
| 3447 | |||
| 3448 | /** |
||
| 3449 | * Reset all global caches associated with DataObject. |
||
| 3450 | */ |
||
| 3451 | public static function reset() |
||
| 3452 | { |
||
| 3453 | // @todo Decouple these |
||
| 3454 | DBEnum::flushCache(); |
||
| 3455 | ClassInfo::reset_db_cache(); |
||
| 3456 | static::getSchema()->reset(); |
||
| 3457 | self::$_cache_get_one = []; |
||
| 3458 | self::$_cache_field_labels = []; |
||
| 3459 | } |
||
| 3460 | |||
| 3461 | /** |
||
| 3462 | * Return the given element, searching by ID. |
||
| 3463 | * |
||
| 3464 | * This can be called either via `DataObject::get_by_id(MyClass::class, $id)` |
||
| 3465 | * or `MyClass::get_by_id($id)` |
||
| 3466 | * |
||
| 3467 | * The object returned is cached, unlike DataObject::get()->byID() {@link DataList::byID()} |
||
| 3468 | * |
||
| 3469 | * @param string|int $classOrID The class of the object to be returned, or id if called on target class |
||
| 3470 | * @param int|bool $idOrCache The id of the element, or cache if called on target class |
||
| 3471 | * @param boolean $cache See {@link get_one()} |
||
| 3472 | * |
||
| 3473 | * @return static The element |
||
| 3474 | */ |
||
| 3475 | public static function get_by_id($classOrID, $idOrCache = null, $cache = true) |
||
| 3476 | { |
||
| 3477 | // Shift arguments if passing id in first or second argument |
||
| 3478 | list ($class, $id, $cached) = is_numeric($classOrID) |
||
| 3479 | ? [get_called_class(), $classOrID, isset($idOrCache) ? $idOrCache : $cache] |
||
| 3480 | : [$classOrID, $idOrCache, $cache]; |
||
| 3481 | |||
| 3482 | // Validate class |
||
| 3483 | if ($class === self::class) { |
||
| 3484 | throw new InvalidArgumentException('DataObject::get_by_id() cannot query non-subclass DataObject directly'); |
||
| 3485 | } |
||
| 3486 | |||
| 3487 | // Pass to get_one |
||
| 3488 | $column = static::getSchema()->sqlColumnForField($class, 'ID'); |
||
| 3489 | return DataObject::get_one($class, [$column => $id], $cached); |
||
| 3490 | } |
||
| 3491 | |||
| 3492 | /** |
||
| 3493 | * Get the name of the base table for this object |
||
| 3494 | * |
||
| 3495 | * @return string |
||
| 3496 | */ |
||
| 3497 | public function baseTable() |
||
| 3498 | { |
||
| 3499 | return static::getSchema()->baseDataTable($this); |
||
| 3500 | } |
||
| 3501 | |||
| 3502 | /** |
||
| 3503 | * Get the base class for this object |
||
| 3504 | * |
||
| 3505 | * @return string |
||
| 3506 | */ |
||
| 3507 | public function baseClass() |
||
| 3508 | { |
||
| 3509 | return static::getSchema()->baseDataClass($this); |
||
| 3510 | } |
||
| 3511 | |||
| 3512 | /** |
||
| 3513 | * @var array Parameters used in the query that built this object. |
||
| 3514 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3515 | * run additional queries using the same context. |
||
| 3516 | */ |
||
| 3517 | protected $sourceQueryParams; |
||
| 3518 | |||
| 3519 | /** |
||
| 3520 | * @see $sourceQueryParams |
||
| 3521 | * @return array |
||
| 3522 | */ |
||
| 3523 | public function getSourceQueryParams() |
||
| 3524 | { |
||
| 3525 | return $this->sourceQueryParams; |
||
| 3526 | } |
||
| 3527 | |||
| 3528 | /** |
||
| 3529 | * Get list of parameters that should be inherited to relations on this object |
||
| 3530 | * |
||
| 3531 | * @return array |
||
| 3532 | */ |
||
| 3533 | public function getInheritableQueryParams() |
||
| 3534 | { |
||
| 3535 | $params = $this->getSourceQueryParams(); |
||
| 3536 | $this->extend('updateInheritableQueryParams', $params); |
||
| 3537 | return $params; |
||
| 3538 | } |
||
| 3539 | |||
| 3540 | /** |
||
| 3541 | * @see $sourceQueryParams |
||
| 3542 | * @param array $array |
||
| 3543 | */ |
||
| 3544 | public function setSourceQueryParams($array) |
||
| 3545 | { |
||
| 3546 | $this->sourceQueryParams = $array; |
||
| 3547 | } |
||
| 3548 | |||
| 3549 | /** |
||
| 3550 | * @see $sourceQueryParams |
||
| 3551 | * @param string $key |
||
| 3552 | * @param string $value |
||
| 3553 | */ |
||
| 3554 | public function setSourceQueryParam($key, $value) |
||
| 3555 | { |
||
| 3556 | $this->sourceQueryParams[$key] = $value; |
||
| 3557 | } |
||
| 3558 | |||
| 3559 | /** |
||
| 3560 | * @see $sourceQueryParams |
||
| 3561 | * @param string $key |
||
| 3562 | * @return string |
||
| 3563 | */ |
||
| 3564 | public function getSourceQueryParam($key) |
||
| 3565 | { |
||
| 3566 | if (isset($this->sourceQueryParams[$key])) { |
||
| 3567 | return $this->sourceQueryParams[$key]; |
||
| 3568 | } |
||
| 3569 | return null; |
||
| 3570 | } |
||
| 3571 | |||
| 3572 | //-------------------------------------------------------------------------------------------// |
||
| 3573 | |||
| 3574 | /** |
||
| 3575 | * Check the database schema and update it as necessary. |
||
| 3576 | * |
||
| 3577 | * @uses DataExtension::augmentDatabase() |
||
| 3578 | */ |
||
| 3579 | public function requireTable() |
||
| 3580 | { |
||
| 3581 | // Only build the table if we've actually got fields |
||
| 3582 | $schema = static::getSchema(); |
||
| 3583 | $table = $schema->tableName(static::class); |
||
| 3584 | $fields = $schema->databaseFields(static::class, false); |
||
| 3585 | $indexes = $schema->databaseIndexes(static::class, false); |
||
| 3586 | $extensions = self::database_extensions(static::class); |
||
| 3587 | |||
| 3588 | if (empty($table)) { |
||
| 3589 | throw new LogicException( |
||
| 3590 | "Class " . static::class . " not loaded by manifest, or no database table configured" |
||
| 3591 | ); |
||
| 3592 | } |
||
| 3593 | |||
| 3594 | if ($fields) { |
||
| 3595 | $hasAutoIncPK = get_parent_class($this) === self::class; |
||
| 3596 | DB::require_table( |
||
| 3597 | $table, |
||
| 3598 | $fields, |
||
| 3599 | $indexes, |
||
| 3600 | $hasAutoIncPK, |
||
| 3601 | $this->config()->get('create_table_options'), |
||
| 3602 | $extensions |
||
| 3603 | ); |
||
| 3604 | } else { |
||
| 3605 | DB::dont_require_table($table); |
||
| 3606 | } |
||
| 3607 | |||
| 3608 | // Build any child tables for many_many items |
||
| 3609 | if ($manyMany = $this->uninherited('many_many')) { |
||
| 3610 | $extras = $this->uninherited('many_many_extraFields'); |
||
| 3611 | foreach ($manyMany as $component => $spec) { |
||
| 3612 | // Get many_many spec |
||
| 3613 | $manyManyComponent = $schema->manyManyComponent(static::class, $component); |
||
| 3614 | $parentField = $manyManyComponent['parentField']; |
||
| 3615 | $childField = $manyManyComponent['childField']; |
||
| 3616 | $tableOrClass = $manyManyComponent['join']; |
||
| 3617 | |||
| 3618 | // Skip if backed by actual class |
||
| 3619 | if (class_exists($tableOrClass)) { |
||
| 3620 | continue; |
||
| 3621 | } |
||
| 3622 | |||
| 3623 | // Build fields |
||
| 3624 | $manymanyFields = [ |
||
| 3625 | $parentField => "Int", |
||
| 3626 | $childField => "Int", |
||
| 3627 | ]; |
||
| 3628 | if (isset($extras[$component])) { |
||
| 3629 | $manymanyFields = array_merge($manymanyFields, $extras[$component]); |
||
| 3630 | } |
||
| 3631 | |||
| 3632 | // Build index list |
||
| 3633 | $manymanyIndexes = [ |
||
| 3634 | $parentField => [ |
||
| 3635 | 'type' => 'index', |
||
| 3636 | 'name' => $parentField, |
||
| 3637 | 'columns' => [$parentField], |
||
| 3638 | ], |
||
| 3639 | $childField => [ |
||
| 3640 | 'type' => 'index', |
||
| 3641 | 'name' => $childField, |
||
| 3642 | 'columns' => [$childField], |
||
| 3643 | ], |
||
| 3644 | ]; |
||
| 3645 | DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, $extensions); |
||
| 3646 | } |
||
| 3647 | } |
||
| 3648 | |||
| 3649 | // Let any extentions make their own database fields |
||
| 3650 | $this->extend('augmentDatabase', $dummy); |
||
| 3651 | } |
||
| 3652 | |||
| 3653 | /** |
||
| 3654 | * Add default records to database. This function is called whenever the |
||
| 3655 | * database is built, after the database tables have all been created. Overload |
||
| 3656 | * this to add default records when the database is built, but make sure you |
||
| 3657 | * call parent::requireDefaultRecords(). |
||
| 3658 | * |
||
| 3659 | * @uses DataExtension::requireDefaultRecords() |
||
| 3660 | */ |
||
| 3661 | public function requireDefaultRecords() |
||
| 3662 | { |
||
| 3663 | $defaultRecords = $this->config()->uninherited('default_records'); |
||
| 3664 | |||
| 3665 | if (!empty($defaultRecords)) { |
||
| 3666 | $hasData = DataObject::get_one(static::class); |
||
| 3667 | if (!$hasData) { |
||
| 3668 | $className = static::class; |
||
| 3669 | foreach ($defaultRecords as $record) { |
||
| 3670 | $obj = Injector::inst()->create($className, $record); |
||
| 3671 | $obj->write(); |
||
| 3672 | } |
||
| 3673 | DB::alteration_message("Added default records to $className table", "created"); |
||
| 3674 | } |
||
| 3675 | } |
||
| 3676 | |||
| 3677 | // Let any extentions make their own database default data |
||
| 3678 | $this->extend('requireDefaultRecords', $dummy); |
||
| 3679 | } |
||
| 3680 | |||
| 3681 | /** |
||
| 3682 | * Invoked after every database build is complete (including after table creation and |
||
| 3683 | * default record population). |
||
| 3684 | * |
||
| 3685 | * See {@link DatabaseAdmin::doBuild()} for context. |
||
| 3686 | */ |
||
| 3687 | public function onAfterBuild() |
||
| 3688 | { |
||
| 3689 | $this->extend('onAfterBuild'); |
||
| 3690 | } |
||
| 3691 | |||
| 3692 | /** |
||
| 3693 | * Get the default searchable fields for this object, as defined in the |
||
| 3694 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3695 | * data object, uses a default selection of summary fields. |
||
| 3696 | * |
||
| 3697 | * @return array |
||
| 3698 | */ |
||
| 3699 | public function searchableFields() |
||
| 3700 | { |
||
| 3701 | // can have mixed format, need to make consistent in most verbose form |
||
| 3702 | $fields = $this->config()->get('searchable_fields'); |
||
| 3703 | $labels = $this->fieldLabels(); |
||
| 3704 | |||
| 3705 | // fallback to summary fields (unless empty array is explicitly specified) |
||
| 3706 | if (!$fields && !is_array($fields)) { |
||
| 3707 | $summaryFields = array_keys($this->summaryFields()); |
||
| 3708 | $fields = []; |
||
| 3709 | |||
| 3710 | // remove the custom getters as the search should not include them |
||
| 3711 | $schema = static::getSchema(); |
||
| 3712 | if ($summaryFields) { |
||
| 3713 | foreach ($summaryFields as $key => $name) { |
||
| 3714 | $spec = $name; |
||
| 3715 | |||
| 3716 | // Extract field name in case this is a method called on a field (e.g. "Date.Nice") |
||
| 3717 | if (($fieldPos = strpos($name, '.')) !== false) { |
||
| 3718 | $name = substr($name, 0, $fieldPos); |
||
| 3719 | } |
||
| 3720 | |||
| 3721 | if ($schema->fieldSpec($this, $name)) { |
||
| 3722 | $fields[] = $name; |
||
| 3723 | } elseif ($this->relObject($spec)) { |
||
| 3724 | $fields[] = $spec; |
||
| 3725 | } |
||
| 3726 | } |
||
| 3727 | } |
||
| 3728 | } |
||
| 3729 | |||
| 3730 | // we need to make sure the format is unified before |
||
| 3731 | // augmenting fields, so extensions can apply consistent checks |
||
| 3732 | // but also after augmenting fields, because the extension |
||
| 3733 | // might use the shorthand notation as well |
||
| 3734 | |||
| 3735 | // rewrite array, if it is using shorthand syntax |
||
| 3736 | $rewrite = []; |
||
| 3737 | foreach ($fields as $name => $specOrName) { |
||
| 3738 | $identifer = (is_int($name)) ? $specOrName : $name; |
||
| 3739 | |||
| 3740 | if (is_int($name)) { |
||
| 3741 | // Format: array('MyFieldName') |
||
| 3742 | $rewrite[$identifer] = []; |
||
| 3743 | } elseif (is_array($specOrName) && ($relObject = $this->relObject($identifer))) { |
||
| 3744 | // Format: array('MyFieldName' => array( |
||
| 3745 | // 'filter => 'ExactMatchFilter', |
||
| 3746 | // 'field' => 'NumericField', // optional |
||
| 3747 | // 'title' => 'My Title', // optional |
||
| 3748 | // )) |
||
| 3749 | $rewrite[$identifer] = array_merge( |
||
| 3750 | ['filter' => $relObject->config()->get('default_search_filter_class')], |
||
| 3751 | (array)$specOrName |
||
| 3752 | ); |
||
| 3753 | } else { |
||
| 3754 | // Format: array('MyFieldName' => 'ExactMatchFilter') |
||
| 3755 | $rewrite[$identifer] = [ |
||
| 3756 | 'filter' => $specOrName, |
||
| 3757 | ]; |
||
| 3758 | } |
||
| 3759 | if (!isset($rewrite[$identifer]['title'])) { |
||
| 3760 | $rewrite[$identifer]['title'] = (isset($labels[$identifer])) |
||
| 3761 | ? $labels[$identifer] : FormField::name_to_label($identifer); |
||
| 3762 | } |
||
| 3763 | if (!isset($rewrite[$identifer]['filter'])) { |
||
| 3764 | /** @skipUpgrade */ |
||
| 3765 | $rewrite[$identifer]['filter'] = 'PartialMatchFilter'; |
||
| 3766 | } |
||
| 3767 | } |
||
| 3768 | |||
| 3769 | $fields = $rewrite; |
||
| 3770 | |||
| 3771 | // apply DataExtensions if present |
||
| 3772 | $this->extend('updateSearchableFields', $fields); |
||
| 3773 | |||
| 3774 | return $fields; |
||
| 3775 | } |
||
| 3776 | |||
| 3777 | /** |
||
| 3778 | * Get any user defined searchable fields labels that |
||
| 3779 | * exist. Allows overriding of default field names in the form |
||
| 3780 | * interface actually presented to the user. |
||
| 3781 | * |
||
| 3782 | * The reason for keeping this separate from searchable_fields, |
||
| 3783 | * which would be a logical place for this functionality, is to |
||
| 3784 | * avoid bloating and complicating the configuration array. Currently |
||
| 3785 | * much of this system is based on sensible defaults, and this property |
||
| 3786 | * would generally only be set in the case of more complex relationships |
||
| 3787 | * between data object being required in the search interface. |
||
| 3788 | * |
||
| 3789 | * Generates labels based on name of the field itself, if no static property |
||
| 3790 | * {@link self::field_labels} exists. |
||
| 3791 | * |
||
| 3792 | * @uses $field_labels |
||
| 3793 | * @uses FormField::name_to_label() |
||
| 3794 | * |
||
| 3795 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3796 | * |
||
| 3797 | * @return array Array of all element labels |
||
| 3798 | */ |
||
| 3799 | public function fieldLabels($includerelations = true) |
||
| 3800 | { |
||
| 3801 | $cacheKey = static::class . '_' . $includerelations; |
||
| 3802 | |||
| 3803 | if (!isset(self::$_cache_field_labels[$cacheKey])) { |
||
| 3804 | $customLabels = $this->config()->get('field_labels'); |
||
| 3805 | $autoLabels = []; |
||
| 3806 | |||
| 3807 | // get all translated static properties as defined in i18nCollectStatics() |
||
| 3808 | $ancestry = ClassInfo::ancestry(static::class); |
||
| 3809 | $ancestry = array_reverse($ancestry); |
||
| 3810 | if ($ancestry) { |
||
| 3811 | foreach ($ancestry as $ancestorClass) { |
||
| 3812 | if ($ancestorClass === ViewableData::class) { |
||
| 3813 | break; |
||
| 3814 | } |
||
| 3815 | $types = [ |
||
| 3816 | 'db' => (array)Config::inst()->get($ancestorClass, 'db', Config::UNINHERITED) |
||
| 3817 | ]; |
||
| 3818 | if ($includerelations) { |
||
| 3819 | $types['has_one'] = (array)Config::inst()->get($ancestorClass, 'has_one', Config::UNINHERITED); |
||
| 3820 | $types['has_many'] = (array)Config::inst()->get( |
||
| 3821 | $ancestorClass, |
||
| 3822 | 'has_many', |
||
| 3823 | Config::UNINHERITED |
||
| 3824 | ); |
||
| 3825 | $types['many_many'] = (array)Config::inst()->get( |
||
| 3826 | $ancestorClass, |
||
| 3827 | 'many_many', |
||
| 3828 | Config::UNINHERITED |
||
| 3829 | ); |
||
| 3830 | $types['belongs_many_many'] = (array)Config::inst()->get( |
||
| 3831 | $ancestorClass, |
||
| 3832 | 'belongs_many_many', |
||
| 3833 | Config::UNINHERITED |
||
| 3834 | ); |
||
| 3835 | } |
||
| 3836 | foreach ($types as $type => $attrs) { |
||
| 3837 | foreach ($attrs as $name => $spec) { |
||
| 3838 | $autoLabels[$name] = _t( |
||
| 3839 | "{$ancestorClass}.{$type}_{$name}", |
||
| 3840 | FormField::name_to_label($name) |
||
| 3841 | ); |
||
| 3842 | } |
||
| 3843 | } |
||
| 3844 | } |
||
| 3845 | } |
||
| 3846 | |||
| 3847 | $labels = array_merge((array)$autoLabels, (array)$customLabels); |
||
| 3848 | $this->extend('updateFieldLabels', $labels); |
||
| 3849 | self::$_cache_field_labels[$cacheKey] = $labels; |
||
| 3850 | } |
||
| 3851 | |||
| 3852 | return self::$_cache_field_labels[$cacheKey]; |
||
| 3853 | } |
||
| 3854 | |||
| 3855 | /** |
||
| 3856 | * Get a human-readable label for a single field, |
||
| 3857 | * see {@link fieldLabels()} for more details. |
||
| 3858 | * |
||
| 3859 | * @uses fieldLabels() |
||
| 3860 | * @uses FormField::name_to_label() |
||
| 3861 | * |
||
| 3862 | * @param string $name Name of the field |
||
| 3863 | * @return string Label of the field |
||
| 3864 | */ |
||
| 3865 | public function fieldLabel($name) |
||
| 3866 | { |
||
| 3867 | $labels = $this->fieldLabels(); |
||
| 3868 | return (isset($labels[$name])) ? $labels[$name] : FormField::name_to_label($name); |
||
| 3869 | } |
||
| 3870 | |||
| 3871 | /** |
||
| 3872 | * Get the default summary fields for this object. |
||
| 3873 | * |
||
| 3874 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3875 | * |
||
| 3876 | * @return array |
||
| 3877 | */ |
||
| 3878 | public function summaryFields() |
||
| 3879 | { |
||
| 3880 | $rawFields = $this->config()->get('summary_fields'); |
||
| 3881 | |||
| 3882 | // Merge associative / numeric keys |
||
| 3883 | $fields = []; |
||
| 3884 | foreach ($rawFields as $key => $value) { |
||
| 3885 | if (is_int($key)) { |
||
| 3886 | $key = $value; |
||
| 3887 | } |
||
| 3888 | $fields[$key] = $value; |
||
| 3889 | } |
||
| 3890 | |||
| 3891 | if (!$fields) { |
||
| 3892 | $fields = []; |
||
| 3893 | // try to scaffold a couple of usual suspects |
||
| 3894 | if ($this->hasField('Name')) { |
||
| 3895 | $fields['Name'] = 'Name'; |
||
| 3896 | } |
||
| 3897 | if (static::getSchema()->fieldSpec($this, 'Title')) { |
||
| 3898 | $fields['Title'] = 'Title'; |
||
| 3899 | } |
||
| 3900 | if ($this->hasField('Description')) { |
||
| 3901 | $fields['Description'] = 'Description'; |
||
| 3902 | } |
||
| 3903 | if ($this->hasField('FirstName')) { |
||
| 3904 | $fields['FirstName'] = 'First Name'; |
||
| 3905 | } |
||
| 3906 | } |
||
| 3907 | $this->extend("updateSummaryFields", $fields); |
||
| 3908 | |||
| 3909 | // Final fail-over, just list ID field |
||
| 3910 | if (!$fields) { |
||
| 3911 | $fields['ID'] = 'ID'; |
||
| 3912 | } |
||
| 3913 | |||
| 3914 | // Localize fields (if possible) |
||
| 3915 | foreach ($this->fieldLabels(false) as $name => $label) { |
||
| 3916 | // only attempt to localize if the label definition is the same as the field name. |
||
| 3917 | // this will preserve any custom labels set in the summary_fields configuration |
||
| 3918 | if (isset($fields[$name]) && $name === $fields[$name]) { |
||
| 3919 | $fields[$name] = $label; |
||
| 3920 | } |
||
| 3921 | } |
||
| 3922 | |||
| 3923 | return $fields; |
||
| 3924 | } |
||
| 3925 | |||
| 3926 | /** |
||
| 3927 | * Defines a default list of filters for the search context. |
||
| 3928 | * |
||
| 3929 | * If a filter class mapping is defined on the data object, |
||
| 3930 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3931 | * {@link DBField} is used. |
||
| 3932 | * |
||
| 3933 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3934 | * |
||
| 3935 | * @return array |
||
| 3936 | */ |
||
| 3937 | public function defaultSearchFilters() |
||
| 3938 | { |
||
| 3939 | $filters = []; |
||
| 3940 | |||
| 3941 | foreach ($this->searchableFields() as $name => $spec) { |
||
| 3942 | if (empty($spec['filter'])) { |
||
| 3943 | /** @skipUpgrade */ |
||
| 3944 | $filters[$name] = 'PartialMatchFilter'; |
||
| 3945 | } elseif ($spec['filter'] instanceof SearchFilter) { |
||
| 3946 | $filters[$name] = $spec['filter']; |
||
| 3947 | } else { |
||
| 3948 | $filters[$name] = Injector::inst()->create($spec['filter'], $name); |
||
| 3949 | } |
||
| 3950 | } |
||
| 3951 | |||
| 3952 | return $filters; |
||
| 3953 | } |
||
| 3954 | |||
| 3955 | /** |
||
| 3956 | * @return boolean True if the object is in the database |
||
| 3957 | */ |
||
| 3958 | public function isInDB() |
||
| 3959 | { |
||
| 3960 | return is_numeric($this->ID) && $this->ID > 0; |
||
| 3961 | } |
||
| 3962 | |||
| 3963 | /* |
||
| 3964 | * @ignore |
||
| 3965 | */ |
||
| 3966 | private static $subclass_access = true; |
||
| 3967 | |||
| 3968 | /** |
||
| 3969 | * Temporarily disable subclass access in data object qeur |
||
| 3970 | */ |
||
| 3971 | public static function disable_subclass_access() |
||
| 3972 | { |
||
| 3973 | self::$subclass_access = false; |
||
| 3974 | } |
||
| 3975 | |||
| 3976 | public static function enable_subclass_access() |
||
| 3977 | { |
||
| 3978 | self::$subclass_access = true; |
||
| 3979 | } |
||
| 3980 | |||
| 3981 | //-------------------------------------------------------------------------------------------// |
||
| 3982 | |||
| 3983 | /** |
||
| 3984 | * Database field definitions. |
||
| 3985 | * This is a map from field names to field type. The field |
||
| 3986 | * type should be a class that extends . |
||
| 3987 | * @var array |
||
| 3988 | * @config |
||
| 3989 | */ |
||
| 3990 | private static $db = []; |
||
| 3991 | |||
| 3992 | /** |
||
| 3993 | * Use a casting object for a field. This is a map from |
||
| 3994 | * field name to class name of the casting object. |
||
| 3995 | * |
||
| 3996 | * @var array |
||
| 3997 | */ |
||
| 3998 | private static $casting = [ |
||
| 3999 | "Title" => 'Text', |
||
| 4000 | ]; |
||
| 4001 | |||
| 4002 | /** |
||
| 4003 | * Specify custom options for a CREATE TABLE call. |
||
| 4004 | * Can be used to specify a custom storage engine for specific database table. |
||
| 4005 | * All options have to be keyed for a specific database implementation, |
||
| 4006 | * identified by their class name (extending from {@link SS_Database}). |
||
| 4007 | * |
||
| 4008 | * <code> |
||
| 4009 | * array( |
||
| 4010 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 4011 | * ) |
||
| 4012 | * </code> |
||
| 4013 | * |
||
| 4014 | * Caution: This API is experimental, and might not be |
||
| 4015 | * included in the next major release. Please use with care. |
||
| 4016 | * |
||
| 4017 | * @var array |
||
| 4018 | * @config |
||
| 4019 | */ |
||
| 4020 | private static $create_table_options = [ |
||
| 4021 | MySQLSchemaManager::ID => 'ENGINE=InnoDB' |
||
| 4022 | ]; |
||
| 4023 | |||
| 4024 | /** |
||
| 4025 | * If a field is in this array, then create a database index |
||
| 4026 | * on that field. This is a map from fieldname to index type. |
||
| 4027 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 4028 | * |
||
| 4029 | * @var array |
||
| 4030 | * @config |
||
| 4031 | */ |
||
| 4032 | private static $indexes = null; |
||
| 4033 | |||
| 4034 | /** |
||
| 4035 | * Inserts standard column-values when a DataObject |
||
| 4036 | * is instantiated. Does not insert default records {@see $default_records}. |
||
| 4037 | * This is a map from fieldname to default value. |
||
| 4038 | * |
||
| 4039 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 4040 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 4041 | * or false in your subclass. Setting it to null won't work. |
||
| 4042 | * |
||
| 4043 | * @var array |
||
| 4044 | * @config |
||
| 4045 | */ |
||
| 4046 | private static $defaults = []; |
||
| 4047 | |||
| 4048 | /** |
||
| 4049 | * Multidimensional array which inserts default data into the database |
||
| 4050 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 4051 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 4052 | * behaviour such as publishing and ParentNodes. |
||
| 4053 | * |
||
| 4054 | * Example: |
||
| 4055 | * array( |
||
| 4056 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 4057 | * array('Title' => "DefaultPage2") |
||
| 4058 | * ). |
||
| 4059 | * |
||
| 4060 | * @var array |
||
| 4061 | * @config |
||
| 4062 | */ |
||
| 4063 | private static $default_records = null; |
||
| 4064 | |||
| 4065 | /** |
||
| 4066 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 4067 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 4068 | * |
||
| 4069 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 4070 | * |
||
| 4071 | * @var array |
||
| 4072 | * @config |
||
| 4073 | */ |
||
| 4074 | private static $has_one = []; |
||
| 4075 | |||
| 4076 | /** |
||
| 4077 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 4078 | * |
||
| 4079 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 4080 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 4081 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 4082 | * |
||
| 4083 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 4084 | * |
||
| 4085 | * @var array |
||
| 4086 | * @config |
||
| 4087 | */ |
||
| 4088 | private static $belongs_to = []; |
||
| 4089 | |||
| 4090 | /** |
||
| 4091 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 4092 | * |
||
| 4093 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 4094 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 4095 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 4096 | * which foreign key to use. |
||
| 4097 | * |
||
| 4098 | * @var array |
||
| 4099 | * @config |
||
| 4100 | */ |
||
| 4101 | private static $has_many = []; |
||
| 4102 | |||
| 4103 | /** |
||
| 4104 | * many-many relationship definitions. |
||
| 4105 | * This is a map from component name to data type. |
||
| 4106 | * @var array |
||
| 4107 | * @config |
||
| 4108 | */ |
||
| 4109 | private static $many_many = []; |
||
| 4110 | |||
| 4111 | /** |
||
| 4112 | * Extra fields to include on the connecting many-many table. |
||
| 4113 | * This is a map from field name to field type. |
||
| 4114 | * |
||
| 4115 | * Example code: |
||
| 4116 | * <code> |
||
| 4117 | * public static $many_many_extraFields = array( |
||
| 4118 | * 'Members' => array( |
||
| 4119 | * 'Role' => 'Varchar(100)' |
||
| 4120 | * ) |
||
| 4121 | * ); |
||
| 4122 | * </code> |
||
| 4123 | * |
||
| 4124 | * @var array |
||
| 4125 | * @config |
||
| 4126 | */ |
||
| 4127 | private static $many_many_extraFields = []; |
||
| 4128 | |||
| 4129 | /** |
||
| 4130 | * The inverse side of a many-many relationship. |
||
| 4131 | * This is a map from component name to data type. |
||
| 4132 | * @var array |
||
| 4133 | * @config |
||
| 4134 | */ |
||
| 4135 | private static $belongs_many_many = []; |
||
| 4136 | |||
| 4137 | /** |
||
| 4138 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 4139 | * clause of a SQL query if no other sort expression is provided. |
||
| 4140 | * @var string |
||
| 4141 | * @config |
||
| 4142 | */ |
||
| 4143 | private static $default_sort = null; |
||
| 4144 | |||
| 4145 | /** |
||
| 4146 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 4147 | * search interface. |
||
| 4148 | * |
||
| 4149 | * Overriding the default filter, with a custom defined filter: |
||
| 4150 | * <code> |
||
| 4151 | * static $searchable_fields = array( |
||
| 4152 | * "Name" => "PartialMatchFilter" |
||
| 4153 | * ); |
||
| 4154 | * </code> |
||
| 4155 | * |
||
| 4156 | * Overriding the default form fields, with a custom defined field. |
||
| 4157 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 4158 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 4159 | * <code> |
||
| 4160 | * static $searchable_fields = array( |
||
| 4161 | * "Name" => array( |
||
| 4162 | * "field" => "TextField" |
||
| 4163 | * ) |
||
| 4164 | * ); |
||
| 4165 | * </code> |
||
| 4166 | * |
||
| 4167 | * Overriding the default form field, filter and title: |
||
| 4168 | * <code> |
||
| 4169 | * static $searchable_fields = array( |
||
| 4170 | * "Organisation.ZipCode" => array( |
||
| 4171 | * "field" => "TextField", |
||
| 4172 | * "filter" => "PartialMatchFilter", |
||
| 4173 | * "title" => 'Organisation ZIP' |
||
| 4174 | * ) |
||
| 4175 | * ); |
||
| 4176 | * </code> |
||
| 4177 | * @config |
||
| 4178 | * @var array |
||
| 4179 | */ |
||
| 4180 | private static $searchable_fields = null; |
||
| 4181 | |||
| 4182 | /** |
||
| 4183 | * User defined labels for searchable_fields, used to override |
||
| 4184 | * default display in the search form. |
||
| 4185 | * @config |
||
| 4186 | * @var array |
||
| 4187 | */ |
||
| 4188 | private static $field_labels = []; |
||
| 4189 | |||
| 4190 | /** |
||
| 4191 | * Provides a default list of fields to be used by a 'summary' |
||
| 4192 | * view of this object. |
||
| 4193 | * @config |
||
| 4194 | * @var array |
||
| 4195 | */ |
||
| 4196 | private static $summary_fields = []; |
||
| 4197 | |||
| 4198 | public function provideI18nEntities() |
||
| 4199 | { |
||
| 4200 | // Note: see http://guides.rubyonrails.org/i18n.html#pluralization for rules |
||
| 4201 | // Best guess for a/an rule. Better guesses require overriding in subclasses |
||
| 4202 | $pluralName = $this->plural_name(); |
||
| 4203 | $singularName = $this->singular_name(); |
||
| 4204 | $conjunction = preg_match('/^[aeiou]/i', $singularName) ? 'An ' : 'A '; |
||
| 4205 | return [ |
||
| 4206 | static::class . '.SINGULARNAME' => $this->singular_name(), |
||
| 4207 | static::class . '.PLURALNAME' => $pluralName, |
||
| 4208 | static::class . '.PLURALS' => [ |
||
| 4209 | 'one' => $conjunction . $singularName, |
||
| 4210 | 'other' => '{count} ' . $pluralName |
||
| 4211 | ] |
||
| 4212 | ]; |
||
| 4213 | } |
||
| 4214 | |||
| 4215 | /** |
||
| 4216 | * Returns true if the given method/parameter has a value |
||
| 4217 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 4218 | * |
||
| 4219 | * @param string $field The field name |
||
| 4220 | * @param array $arguments |
||
| 4221 | * @param bool $cache |
||
| 4222 | * @return boolean |
||
| 4223 | */ |
||
| 4224 | public function hasValue($field, $arguments = null, $cache = true) |
||
| 4225 | { |
||
| 4226 | // has_one fields should not use dbObject to check if a value is given |
||
| 4227 | $hasOne = static::getSchema()->hasOneComponent(static::class, $field); |
||
| 4228 | if (!$hasOne && ($obj = $this->dbObject($field))) { |
||
| 4229 | return $obj->exists(); |
||
| 4230 | } else { |
||
| 4231 | return parent::hasValue($field, $arguments, $cache); |
||
| 4232 | } |
||
| 4233 | } |
||
| 4234 | |||
| 4235 | /** |
||
| 4236 | * If selected through a many_many through relation, this is the instance of the joined record |
||
| 4237 | * |
||
| 4238 | * @return DataObject |
||
| 4239 | */ |
||
| 4240 | public function getJoin() |
||
| 4241 | { |
||
| 4242 | return $this->joinRecord; |
||
| 4243 | } |
||
| 4244 | |||
| 4245 | /** |
||
| 4246 | * Set joining object |
||
| 4247 | * |
||
| 4248 | * @param DataObject $object |
||
| 4249 | * @param string $alias Alias |
||
| 4250 | * @return $this |
||
| 4251 | */ |
||
| 4252 | public function setJoin(DataObject $object, $alias = null) |
||
| 4253 | { |
||
| 4254 | $this->joinRecord = $object; |
||
| 4255 | if ($alias) { |
||
| 4256 | if (static::getSchema()->fieldSpec(static::class, $alias)) { |
||
| 4257 | throw new InvalidArgumentException( |
||
| 4258 | "Joined record $alias cannot also be a db field" |
||
| 4259 | ); |
||
| 4260 | } |
||
| 4261 | $this->record[$alias] = $object; |
||
| 4262 | } |
||
| 4263 | return $this; |
||
| 4264 | } |
||
| 4265 | |||
| 4266 | /** |
||
| 4267 | * Find objects in the given relationships, merging them into the given list |
||
| 4268 | * |
||
| 4269 | * @param string $source Config property to extract relationships from |
||
| 4270 | * @param bool $recursive True if recursive |
||
| 4271 | * @param ArrayList $list If specified, items will be added to this list. If not, a new |
||
| 4272 | * instance of ArrayList will be constructed and returned |
||
| 4273 | * @return ArrayList The list of related objects |
||
| 4274 | */ |
||
| 4275 | public function findRelatedObjects($source, $recursive = true, $list = null) |
||
| 4314 | } |
||
| 4315 | |||
| 4316 | /** |
||
| 4317 | * Helper method to merge owned/owning items into a list. |
||
| 4318 | * Items already present in the list will be skipped. |
||
| 4319 | * |
||
| 4320 | * @param ArrayList $list Items to merge into |
||
| 4321 | * @param mixed $items List of new items to merge |
||
| 4322 | * @return ArrayList List of all newly added items that did not already exist in $list |
||
| 4323 | */ |
||
| 4324 | public function mergeRelatedObjects($list, $items) |
||
| 4325 | { |
||
| 4326 | $added = new ArrayList(); |
||
| 4327 | if (!$items) { |
||
| 4328 | return $added; |
||
| 4329 | } |
||
| 4330 | if ($items instanceof DataObject) { |
||
| 4331 | $items = [$items]; |
||
| 4332 | } |
||
| 4333 | |||
| 4334 | /** @var DataObject $item */ |
||
| 4335 | foreach ($items as $item) { |
||
| 4336 | $this->mergeRelatedObject($list, $added, $item); |
||
| 4337 | } |
||
| 4338 | return $added; |
||
| 4339 | } |
||
| 4340 | |||
| 4341 | /** |
||
| 4342 | * Generate a unique key for data object |
||
| 4343 | * the unique key uses the @see DataObject::getUniqueKeyComponents() extension point so unique key modifiers |
||
| 4344 | * such as versioned or fluent are covered |
||
| 4345 | * i.e. same data object in different stages or different locales will produce different unique key |
||
| 4346 | * |
||
| 4347 | * recommended use: |
||
| 4348 | * - when you need unique key for caching purposes |
||
| 4349 | * - when you need unique id on the front end (for example JavaScript needs to target specific element) |
||
| 4350 | * |
||
| 4351 | * @return string |
||
| 4352 | * @throws Exception |
||
| 4353 | */ |
||
| 4354 | public function getUniqueKey(): string |
||
| 4361 | } |
||
| 4362 | |||
| 4363 | /** |
||
| 4364 | * Merge single object into a list, but ensures that existing objects are not |
||
| 4365 | * re-added. |
||
| 4366 | * |
||
| 4367 | * @param ArrayList $list Global list |
||
| 4368 | * @param ArrayList $added Additional list to insert into |
||
| 4369 | * @param DataObject $item Item to add |
||
| 4370 | */ |
||
| 4371 | protected function mergeRelatedObject($list, $added, $item) |
||
| 4372 | { |
||
| 4373 | // Identify item |
||
| 4374 | $itemKey = get_class($item) . '/' . $item->ID; |
||
| 4375 | |||
| 4376 | // Write if saved, versioned, and not already added |
||
| 4377 | if ($item->isInDB() && !isset($list[$itemKey])) { |
||
| 4378 | $list[$itemKey] = $item; |
||
| 4379 | $added[$itemKey] = $item; |
||
| 4380 | } |
||
| 4381 | |||
| 4382 | // Add joined record (from many_many through) automatically |
||
| 4383 | $joined = $item->getJoin(); |
||
| 4384 | if ($joined) { |
||
| 4385 | $this->mergeRelatedObject($list, $added, $joined); |
||
| 4386 | } |
||
| 4387 | } |
||
| 4388 | |||
| 4389 | /** |
||
| 4390 | * Extension point to add more cache key components. |
||
| 4391 | * The framework extend method will return combined values from DataExtension method(s) as an array |
||
| 4392 | * The method on your DataExtension class should return a single scalar value. For example: |
||
| 4393 | * |
||
| 4394 | * public function cacheKeyComponent() |
||
| 4395 | * { |
||
| 4396 | * return (string) $this->owner->MyColumn; |
||
| 4397 | * } |
||
| 4398 | * |
||
| 4399 | * @return array |
||
| 4400 | */ |
||
| 4401 | private function getUniqueKeyComponents(): array |
||
| 4404 | } |
||
| 4405 | |||
| 4406 | /** |
||
| 4407 | * Find all other DataObject instances that are related to this DataObject in the database |
||
| 4408 | * through has_one and many_many relationships. For example: |
||
| 4409 | * This method is called on a File. The MyPage model $has_one File. There is a Page record that has |
||
| 4410 | * a FileID = $this->ID. This SS_List returned by this method will include that Page instance. |
||
| 4411 | * |
||
| 4412 | * @param string[] $excludedClasses |
||
| 4413 | * @return SS_List |
||
| 4414 | * @internal |
||
| 4415 | */ |
||
| 4416 | public function findAllRelatedData(array $excludedClasses = []): SS_List |
||
| 4417 | { |
||
| 4418 | $service = Injector::inst()->get(RelatedDataService::class); |
||
| 4419 | return $service->findAll($this, $excludedClasses); |
||
| 4420 | } |
||
| 4421 | } |
||
| 4422 |