Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DataObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 82 | class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Human-readable singular name. |
||
| 86 | * @var string |
||
| 87 | * @config |
||
| 88 | */ |
||
| 89 | private static $singular_name = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Human-readable plural name |
||
| 93 | * @var string |
||
| 94 | * @config |
||
| 95 | */ |
||
| 96 | private static $plural_name = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Allow API access to this object? |
||
| 100 | * @todo Define the options that can be set here |
||
| 101 | * @config |
||
| 102 | */ |
||
| 103 | private static $api_access = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Allows specification of a default value for the ClassName field. |
||
| 107 | * Configure this value only in subclasses of DataObject. |
||
| 108 | * |
||
| 109 | * @config |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private static $default_classname = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * True if this DataObject has been destroyed. |
||
| 116 | * @var boolean |
||
| 117 | */ |
||
| 118 | public $destroyed = false; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The DataModel from this this object comes |
||
| 122 | */ |
||
| 123 | protected $model; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Data stored in this objects database record. An array indexed by fieldname. |
||
| 127 | * |
||
| 128 | * Use {@link toMap()} if you want an array representation |
||
| 129 | * of this object, as the $record array might contain lazy loaded field aliases. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $record; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Represents a field that hasn't changed (before === after, thus before == after) |
||
| 137 | */ |
||
| 138 | const CHANGE_NONE = 0; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Represents a field that has changed type, although not the loosely defined value. |
||
| 142 | * (before !== after && before == after) |
||
| 143 | * E.g. change 1 to true or "true" to true, but not true to 0. |
||
| 144 | * Value changes are by nature also considered strict changes. |
||
| 145 | */ |
||
| 146 | const CHANGE_STRICT = 1; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Represents a field that has changed the loosely defined value |
||
| 150 | * (before != after, thus, before !== after)) |
||
| 151 | * E.g. change false to true, but not false to 0 |
||
| 152 | */ |
||
| 153 | const CHANGE_VALUE = 2; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * An array indexed by fieldname, true if the field has been changed. |
||
| 157 | * Use {@link getChangedFields()} and {@link isChanged()} to inspect |
||
| 158 | * the changed state. |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private $changed; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The database record (in the same format as $record), before |
||
| 166 | * any changes. |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | protected $original; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete() |
||
| 173 | * @var boolean |
||
| 174 | */ |
||
| 175 | protected $brokenOnDelete = false; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite() |
||
| 179 | * @var boolean |
||
| 180 | */ |
||
| 181 | protected $brokenOnWrite = false; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @config |
||
| 185 | * @var boolean Should dataobjects be validated before they are written? |
||
| 186 | * Caution: Validation can contain safeguards against invalid/malicious data, |
||
| 187 | * and check permission levels (e.g. on {@link Group}). Therefore it is recommended |
||
| 188 | * to only disable validation for very specific use cases. |
||
| 189 | */ |
||
| 190 | private static $validation_enabled = true; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Static caches used by relevant functions. |
||
| 194 | */ |
||
| 195 | protected static $_cache_has_own_table = array(); |
||
| 196 | protected static $_cache_get_one; |
||
| 197 | protected static $_cache_get_class_ancestry; |
||
| 198 | protected static $_cache_composite_fields = array(); |
||
| 199 | protected static $_cache_database_fields = array(); |
||
| 200 | protected static $_cache_field_labels = array(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Base fields which are not defined in static $db |
||
| 204 | * |
||
| 205 | * @config |
||
| 206 | * @var array |
||
| 207 | */ |
||
| 208 | private static $fixed_fields = array( |
||
| 209 | 'ID' => 'PrimaryKey', |
||
| 210 | 'ClassName' => 'DBClassName', |
||
| 211 | 'LastEdited' => 'SS_Datetime', |
||
| 212 | 'Created' => 'SS_Datetime', |
||
| 213 | ); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Core dataobject extensions |
||
| 217 | * |
||
| 218 | * @config |
||
| 219 | * @var array |
||
| 220 | */ |
||
| 221 | private static $extensions = array( |
||
|
|
|||
| 222 | 'AssetControl' => '\\SilverStripe\\Filesystem\\AssetControlExtension' |
||
| 223 | ); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Non-static relationship cache, indexed by component name. |
||
| 227 | */ |
||
| 228 | protected $components; |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Non-static cache of has_many and many_many relations that can't be written until this object is saved. |
||
| 232 | */ |
||
| 233 | protected $unsavedRelations; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
| 237 | * "ID" will be included on every table. |
||
| 238 | * |
||
| 239 | * Composite DB field specifications are returned by reference if necessary, but not in the return |
||
| 240 | * array. |
||
| 241 | * |
||
| 242 | * Can be called directly on an object. E.g. Member::database_fields() |
||
| 243 | * |
||
| 244 | * @param string $class Class name to query from |
||
| 245 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 246 | */ |
||
| 247 | public static function database_fields($class = null) { |
||
| 248 | if(empty($class)) { |
||
| 249 | $class = get_called_class(); |
||
| 250 | } |
||
| 251 | |||
| 252 | // Refresh cache |
||
| 253 | self::cache_database_fields($class); |
||
| 254 | |||
| 255 | // Return cached values |
||
| 256 | return self::$_cache_database_fields[$class]; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Cache all database and composite fields for the given class. |
||
| 261 | * Will do nothing if already cached |
||
| 262 | * |
||
| 263 | * @param string $class Class name to cache |
||
| 264 | */ |
||
| 265 | protected static function cache_database_fields($class) { |
||
| 266 | // Skip if already cached |
||
| 267 | if( isset(self::$_cache_database_fields[$class]) |
||
| 268 | && isset(self::$_cache_composite_fields[$class]) |
||
| 269 | ) { |
||
| 270 | return; |
||
| 271 | } |
||
| 272 | |||
| 273 | $compositeFields = array(); |
||
| 274 | $dbFields = array(); |
||
| 275 | |||
| 276 | // Ensure fixed fields appear at the start |
||
| 277 | $fixedFields = self::config()->fixed_fields; |
||
| 278 | if(get_parent_class($class) === 'DataObject') { |
||
| 279 | // Merge fixed with ClassName spec and custom db fields |
||
| 280 | $dbFields = $fixedFields; |
||
| 281 | } else { |
||
| 282 | $dbFields['ID'] = $fixedFields['ID']; |
||
| 283 | } |
||
| 284 | |||
| 285 | // Check each DB value as either a field or composite field |
||
| 286 | $db = Config::inst()->get($class, 'db', Config::UNINHERITED) ?: array(); |
||
| 287 | foreach($db as $fieldName => $fieldSpec) { |
||
| 288 | $fieldClass = strtok($fieldSpec, '('); |
||
| 289 | if(singleton($fieldClass) instanceof DBComposite) { |
||
| 290 | $compositeFields[$fieldName] = $fieldSpec; |
||
| 291 | } else { |
||
| 292 | $dbFields[$fieldName] = $fieldSpec; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | // Add in all has_ones |
||
| 297 | $hasOne = Config::inst()->get($class, 'has_one', Config::UNINHERITED) ?: array(); |
||
| 298 | foreach($hasOne as $fieldName => $hasOneClass) { |
||
| 299 | if($hasOneClass === 'DataObject') { |
||
| 300 | $compositeFields[$fieldName] = 'PolymorphicForeignKey'; |
||
| 301 | } else { |
||
| 302 | $dbFields["{$fieldName}ID"] = 'ForeignKey'; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | // Merge composite fields into DB |
||
| 307 | foreach($compositeFields as $fieldName => $fieldSpec) { |
||
| 308 | $fieldObj = Object::create_from_string($fieldSpec, $fieldName); |
||
| 309 | $fieldObj->setTable($class); |
||
| 310 | $nestedFields = $fieldObj->compositeDatabaseFields(); |
||
| 311 | foreach($nestedFields as $nestedName => $nestedSpec) { |
||
| 312 | $dbFields["{$fieldName}{$nestedName}"] = $nestedSpec; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | // Return cached results |
||
| 317 | self::$_cache_database_fields[$class] = $dbFields; |
||
| 318 | self::$_cache_composite_fields[$class] = $compositeFields; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get all database columns explicitly defined on a class in {@link DataObject::$db} |
||
| 323 | * and {@link DataObject::$has_one}. Resolves instances of {@link DBComposite} |
||
| 324 | * into the actual database fields, rather than the name of the field which |
||
| 325 | * might not equate a database column. |
||
| 326 | * |
||
| 327 | * Does not include "base fields" like "ID", "ClassName", "Created", "LastEdited", |
||
| 328 | * see {@link database_fields()}. |
||
| 329 | * |
||
| 330 | * Can be called directly on an object. E.g. Member::custom_database_fields() |
||
| 331 | * |
||
| 332 | * @uses DBComposite->compositeDatabaseFields() |
||
| 333 | * |
||
| 334 | * @param string $class Class name to query from |
||
| 335 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 336 | */ |
||
| 337 | public static function custom_database_fields($class = null) { |
||
| 338 | if(empty($class)) { |
||
| 339 | $class = get_called_class(); |
||
| 340 | } |
||
| 341 | |||
| 342 | // Get all fields |
||
| 343 | $fields = self::database_fields($class); |
||
| 344 | |||
| 345 | // Remove fixed fields. This assumes that NO fixed_fields are composite |
||
| 346 | $fields = array_diff_key($fields, self::config()->fixed_fields); |
||
| 347 | return $fields; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns the field class if the given db field on the class is a composite field. |
||
| 352 | * Will check all applicable ancestor classes and aggregate results. |
||
| 353 | * |
||
| 354 | * @param string $class Class to check |
||
| 355 | * @param string $name Field to check |
||
| 356 | * @param boolean $aggregated True if parent classes should be checked, or false to limit to this class |
||
| 357 | * @return string|false Class spec name of composite field if it exists, or false if not |
||
| 358 | */ |
||
| 359 | public static function is_composite_field($class, $name, $aggregated = true) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 366 | * Will check all applicable ancestor classes and aggregate results. |
||
| 367 | * |
||
| 368 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
| 369 | * to aggregate. |
||
| 370 | * |
||
| 371 | * Includes composite has_one (Polymorphic) fields |
||
| 372 | * |
||
| 373 | * @param string $class Name of class to check |
||
| 374 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 375 | * @return array List of composite fields and their class spec |
||
| 376 | */ |
||
| 377 | public static function composite_fields($class = null, $aggregated = true) { |
||
| 378 | // Check $class |
||
| 379 | if(empty($class)) { |
||
| 380 | $class = get_called_class(); |
||
| 381 | } |
||
| 382 | if($class === 'DataObject') { |
||
| 383 | return array(); |
||
| 384 | } |
||
| 385 | |||
| 386 | // Refresh cache |
||
| 387 | self::cache_database_fields($class); |
||
| 388 | |||
| 389 | // Get fields for this class |
||
| 390 | $compositeFields = self::$_cache_composite_fields[$class]; |
||
| 391 | if(!$aggregated) { |
||
| 392 | return $compositeFields; |
||
| 393 | } |
||
| 394 | |||
| 395 | // Recursively merge |
||
| 396 | return array_merge( |
||
| 397 | $compositeFields, |
||
| 398 | self::composite_fields(get_parent_class($class)) |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Construct a new DataObject. |
||
| 404 | * |
||
| 405 | * @param array|null $record This will be null for a new database record. Alternatively, you can pass an array of |
||
| 406 | * field values. Normally this contructor is only used by the internal systems that get objects from the database. |
||
| 407 | * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods. |
||
| 408 | * Singletons don't have their defaults set. |
||
| 409 | * @param DataModel $model |
||
| 410 | * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects. |
||
| 411 | */ |
||
| 412 | public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) { |
||
| 413 | parent::__construct(); |
||
| 414 | |||
| 415 | // Set query params on the DataObject to tell the lazy loading mechanism the context the object creation context |
||
| 416 | $this->setSourceQueryParams($queryParams); |
||
| 417 | |||
| 418 | // Set the fields data. |
||
| 419 | if(!$record) { |
||
| 420 | $record = array( |
||
| 421 | 'ID' => 0, |
||
| 422 | 'ClassName' => get_class($this), |
||
| 423 | 'RecordClassName' => get_class($this) |
||
| 424 | ); |
||
| 425 | } |
||
| 426 | |||
| 427 | if(!is_array($record) && !is_a($record, "stdClass")) { |
||
| 428 | if(is_object($record)) $passed = "an object of type '$record->class'"; |
||
| 429 | else $passed = "The value '$record'"; |
||
| 430 | |||
| 431 | user_error("DataObject::__construct passed $passed. It's supposed to be passed an array," |
||
| 432 | . " taken straight from the database. Perhaps you should use DataList::create()->First(); instead?", |
||
| 433 | E_USER_WARNING); |
||
| 434 | $record = null; |
||
| 435 | } |
||
| 436 | |||
| 437 | if(is_a($record, "stdClass")) { |
||
| 438 | $record = (array)$record; |
||
| 439 | } |
||
| 440 | |||
| 441 | // Set $this->record to $record, but ignore NULLs |
||
| 442 | $this->record = array(); |
||
| 443 | foreach($record as $k => $v) { |
||
| 444 | // Ensure that ID is stored as a number and not a string |
||
| 445 | // To do: this kind of clean-up should be done on all numeric fields, in some relatively |
||
| 446 | // performant manner |
||
| 447 | if($v !== null) { |
||
| 448 | if($k == 'ID' && is_numeric($v)) $this->record[$k] = (int)$v; |
||
| 449 | else $this->record[$k] = $v; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | // Identify fields that should be lazy loaded, but only on existing records |
||
| 454 | if(!empty($record['ID'])) { |
||
| 455 | $currentObj = get_class($this); |
||
| 456 | while($currentObj != 'DataObject') { |
||
| 457 | $fields = self::custom_database_fields($currentObj); |
||
| 458 | foreach($fields as $field => $type) { |
||
| 459 | if(!array_key_exists($field, $record)) $this->record[$field.'_Lazy'] = $currentObj; |
||
| 460 | } |
||
| 461 | $currentObj = get_parent_class($currentObj); |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | $this->original = $this->record; |
||
| 466 | |||
| 467 | // Keep track of the modification date of all the data sourced to make this page |
||
| 468 | // From this we create a Last-Modified HTTP header |
||
| 469 | if(isset($record['LastEdited'])) { |
||
| 470 | HTTP::register_modification_date($record['LastEdited']); |
||
| 471 | } |
||
| 472 | |||
| 473 | // this must be called before populateDefaults(), as field getters on a DataObject |
||
| 474 | // may call getComponent() and others, which rely on $this->model being set. |
||
| 475 | $this->model = $model ? $model : DataModel::inst(); |
||
| 476 | |||
| 477 | // Must be called after parent constructor |
||
| 478 | if(!$isSingleton && (!isset($this->record['ID']) || !$this->record['ID'])) { |
||
| 479 | $this->populateDefaults(); |
||
| 480 | } |
||
| 481 | |||
| 482 | // prevent populateDefaults() and setField() from marking overwritten defaults as changed |
||
| 483 | $this->changed = array(); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set the DataModel |
||
| 488 | * @param DataModel $model |
||
| 489 | * @return DataObject $this |
||
| 490 | */ |
||
| 491 | public function setDataModel(DataModel $model) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Destroy all of this objects dependant objects and local caches. |
||
| 498 | * You'll need to call this to get the memory of an object that has components or extensions freed. |
||
| 499 | */ |
||
| 500 | public function destroy() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create a duplicate of this node. |
||
| 508 | * Note: now also duplicates relations. |
||
| 509 | * |
||
| 510 | * @param bool $doWrite Perform a write() operation before returning the object. |
||
| 511 | * If this is true, it will create the duplicate in the database. |
||
| 512 | * @return DataObject A duplicate of this node. The exact type will be the type of this node. |
||
| 513 | */ |
||
| 514 | public function duplicate($doWrite = true) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object |
||
| 531 | * The destinationObject must be written to the database already and have an ID. Writing is performed |
||
| 532 | * automatically when adding the new relations. |
||
| 533 | * |
||
| 534 | * @param DataObject $sourceObject the source object to duplicate from |
||
| 535 | * @param DataObject $destinationObject the destination object to populate with the duplicated relations |
||
| 536 | * @return DataObject with the new many_many relations copied in |
||
| 537 | */ |
||
| 538 | protected function duplicateManyManyRelations($sourceObject, $destinationObject) { |
||
| 539 | if (!$destinationObject || $destinationObject->ID < 1) { |
||
| 540 | user_error("Can't duplicate relations for an object that has not been written to the database", |
||
| 541 | E_USER_ERROR); |
||
| 542 | } |
||
| 543 | |||
| 544 | //duplicate complex relations |
||
| 545 | // DO NOT copy has_many relations, because copying the relation would result in us changing the has_one |
||
| 546 | // relation on the other side of this relation to point at the copy and no longer the original (being a |
||
| 547 | // has_one, it can only point at one thing at a time). So, all relations except has_many can and are copied |
||
| 548 | if ($sourceObject->hasOne()) foreach($sourceObject->hasOne() as $name => $type) { |
||
| 549 | $this->duplicateRelations($sourceObject, $destinationObject, $name); |
||
| 550 | } |
||
| 551 | if ($sourceObject->manyMany()) foreach($sourceObject->manyMany() as $name => $type) { |
||
| 552 | //many_many include belongs_many_many |
||
| 553 | $this->duplicateRelations($sourceObject, $destinationObject, $name); |
||
| 554 | } |
||
| 555 | |||
| 556 | return $destinationObject; |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Helper function to duplicate relations from one object to another |
||
| 561 | * @param $sourceObject the source object to duplicate from |
||
| 562 | * @param $destinationObject the destination object to populate with the duplicated relations |
||
| 563 | * @param $name the name of the relation to duplicate (e.g. members) |
||
| 564 | */ |
||
| 565 | private function duplicateRelations($sourceObject, $destinationObject, $name) { |
||
| 566 | $relations = $sourceObject->$name(); |
||
| 567 | if ($relations) { |
||
| 568 | if ($relations instanceOf RelationList) { //many-to-something relation |
||
| 569 | if ($relations->Count() > 0) { //with more than one thing it is related to |
||
| 570 | foreach($relations as $relation) { |
||
| 571 | $destinationObject->$name()->add($relation); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | } else { //one-to-one relation |
||
| 575 | $destinationObject->{"{$name}ID"} = $relations->ID; |
||
| 576 | } |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | public function getObsoleteClassName() { |
||
| 584 | |||
| 585 | public function getClassName() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set the ClassName attribute. {@link $class} is also updated. |
||
| 593 | * Warning: This will produce an inconsistent record, as the object |
||
| 594 | * instance will not automatically switch to the new subclass. |
||
| 595 | * Please use {@link newClassInstance()} for this purpose, |
||
| 596 | * or destroy and reinstanciate the record. |
||
| 597 | * |
||
| 598 | * @param string $className The new ClassName attribute (a subclass of {@link DataObject}) |
||
| 599 | * @return DataObject $this |
||
| 600 | */ |
||
| 601 | public function setClassName($className) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Create a new instance of a different class from this object's record. |
||
| 612 | * This is useful when dynamically changing the type of an instance. Specifically, |
||
| 613 | * it ensures that the instance of the class is a match for the className of the |
||
| 614 | * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName} |
||
| 615 | * property manually before calling this method, as it will confuse change detection. |
||
| 616 | * |
||
| 617 | * If the new class is different to the original class, defaults are populated again |
||
| 618 | * because this will only occur automatically on instantiation of a DataObject if |
||
| 619 | * there is no record, or the record has no ID. In this case, we do have an ID but |
||
| 620 | * we still need to repopulate the defaults. |
||
| 621 | * |
||
| 622 | * @param string $newClassName The name of the new class |
||
| 623 | * |
||
| 624 | * @return DataObject The new instance of the new class, The exact type will be of the class name provided. |
||
| 625 | */ |
||
| 626 | public function newClassInstance($newClassName) { |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Adds methods from the extensions. |
||
| 647 | * Called by Object::__construct() once per class. |
||
| 648 | */ |
||
| 649 | public function defineMethods() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Returns true if this object "exists", i.e., has a sensible value. |
||
| 691 | * The default behaviour for a DataObject is to return true if |
||
| 692 | * the object exists in the database, you can override this in subclasses. |
||
| 693 | * |
||
| 694 | * @return boolean true if this object exists |
||
| 695 | */ |
||
| 696 | public function exists() { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns TRUE if all values (other than "ID") are |
||
| 702 | * considered empty (by weak boolean comparison). |
||
| 703 | * |
||
| 704 | * @return boolean |
||
| 705 | */ |
||
| 706 | public function isEmpty() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Pluralise this item given a specific count. |
||
| 727 | * |
||
| 728 | * E.g. "0 Pages", "1 File", "3 Images" |
||
| 729 | * |
||
| 730 | * @param string $count |
||
| 731 | * @param bool $prependNumber Include number in result. Defaults to true. |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | public function i18n_pluralise($count, $prependNumber = true) { |
||
| 735 | return i18n::pluralise( |
||
| 736 | $this->i18n_singular_name(), |
||
| 737 | $this->i18n_plural_name(), |
||
| 738 | $count, |
||
| 739 | $prependNumber |
||
| 740 | ); |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Get the user friendly singular name of this DataObject. |
||
| 745 | * If the name is not defined (by redefining $singular_name in the subclass), |
||
| 746 | * this returns the class name. |
||
| 747 | * |
||
| 748 | * @return string User friendly singular name of this DataObject |
||
| 749 | */ |
||
| 750 | public function singular_name() { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Get the translated user friendly singular name of this DataObject |
||
| 760 | * same as singular_name() but runs it through the translating function |
||
| 761 | * |
||
| 762 | * Translating string is in the form: |
||
| 763 | * $this->class.SINGULARNAME |
||
| 764 | * Example: |
||
| 765 | * Page.SINGULARNAME |
||
| 766 | * |
||
| 767 | * @return string User friendly translated singular name of this DataObject |
||
| 768 | */ |
||
| 769 | public function i18n_singular_name() { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Get the user friendly plural name of this DataObject |
||
| 775 | * If the name is not defined (by renaming $plural_name in the subclass), |
||
| 776 | * this returns a pluralised version of the class name. |
||
| 777 | * |
||
| 778 | * @return string User friendly plural name of this DataObject |
||
| 779 | */ |
||
| 780 | public function plural_name() { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Get the translated user friendly plural name of this DataObject |
||
| 795 | * Same as plural_name but runs it through the translation function |
||
| 796 | * Translation string is in the form: |
||
| 797 | * $this->class.PLURALNAME |
||
| 798 | * Example: |
||
| 799 | * Page.PLURALNAME |
||
| 800 | * |
||
| 801 | * @return string User friendly translated plural name of this DataObject |
||
| 802 | */ |
||
| 803 | public function i18n_plural_name() |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Standard implementation of a title/label for a specific |
||
| 811 | * record. Tries to find properties 'Title' or 'Name', |
||
| 812 | * and falls back to the 'ID'. Useful to provide |
||
| 813 | * user-friendly identification of a record, e.g. in errormessages |
||
| 814 | * or UI-selections. |
||
| 815 | * |
||
| 816 | * Overload this method to have a more specialized implementation, |
||
| 817 | * e.g. for an Address record this could be: |
||
| 818 | * <code> |
||
| 819 | * function getTitle() { |
||
| 820 | * return "{$this->StreetNumber} {$this->StreetName} {$this->City}"; |
||
| 821 | * } |
||
| 822 | * </code> |
||
| 823 | * |
||
| 824 | * @return string |
||
| 825 | */ |
||
| 826 | public function getTitle() { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Returns the associated database record - in this case, the object itself. |
||
| 835 | * This is included so that you can call $dataOrController->data() and get a DataObject all the time. |
||
| 836 | * |
||
| 837 | * @return DataObject Associated database record |
||
| 838 | */ |
||
| 839 | public function data() { |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Convert this object to a map. |
||
| 845 | * |
||
| 846 | * @return array The data as a map. |
||
| 847 | */ |
||
| 848 | public function toMap() { |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Return all currently fetched database fields. |
||
| 855 | * |
||
| 856 | * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields. |
||
| 857 | * Obviously, this makes it a lot faster. |
||
| 858 | * |
||
| 859 | * @return array The data as a map. |
||
| 860 | */ |
||
| 861 | public function getQueriedDatabaseFields() { |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Update a number of fields on this object, given a map of the desired changes. |
||
| 867 | * |
||
| 868 | * The field names can be simple names, or you can use a dot syntax to access $has_one relations. |
||
| 869 | * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim". |
||
| 870 | * |
||
| 871 | * update() doesn't write the main object, but if you use the dot syntax, it will write() |
||
| 872 | * the related objects that it alters. |
||
| 873 | * |
||
| 874 | * @param array $data A map of field name to data values to update. |
||
| 875 | * @return DataObject $this |
||
| 876 | */ |
||
| 877 | public function update($data) { |
||
| 924 | |||
| 925 | /** |
||
| 926 | * Pass changes as a map, and try to |
||
| 927 | * get automatic casting for these fields. |
||
| 928 | * Doesn't write to the database. To write the data, |
||
| 929 | * use the write() method. |
||
| 930 | * |
||
| 931 | * @param array $data A map of field name to data values to update. |
||
| 932 | * @return DataObject $this |
||
| 933 | */ |
||
| 934 | public function castedUpdate($data) { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Merges data and relations from another object of same class, |
||
| 943 | * without conflict resolution. Allows to specify which |
||
| 944 | * dataset takes priority in case its not empty. |
||
| 945 | * has_one-relations are just transferred with priority 'right'. |
||
| 946 | * has_many and many_many-relations are added regardless of priority. |
||
| 947 | * |
||
| 948 | * Caution: has_many/many_many relations are moved rather than duplicated, |
||
| 949 | * meaning they are not connected to the merged object any longer. |
||
| 950 | * Caution: Just saves updated has_many/many_many relations to the database, |
||
| 951 | * doesn't write the updated object itself (just writes the object-properties). |
||
| 952 | * Caution: Does not delete the merged object. |
||
| 953 | * Caution: Does now overwrite Created date on the original object. |
||
| 954 | * |
||
| 955 | * @param $obj DataObject |
||
| 956 | * @param $priority String left|right Determines who wins in case of a conflict (optional) |
||
| 957 | * @param $includeRelations Boolean Merge any existing relations (optional) |
||
| 958 | * @param $overwriteWithEmpty Boolean Overwrite existing left values with empty right values. |
||
| 959 | * Only applicable with $priority='right'. (optional) |
||
| 960 | * @return Boolean |
||
| 961 | */ |
||
| 962 | public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false) { |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * Forces the record to think that all its data has changed. |
||
| 1038 | * Doesn't write to the database. Only sets fields as changed |
||
| 1039 | * if they are not already marked as changed. |
||
| 1040 | * |
||
| 1041 | * @return $this |
||
| 1042 | */ |
||
| 1043 | public function forceChange() { |
||
| 1044 | // Ensure lazy fields loaded |
||
| 1045 | $this->loadLazyFields(); |
||
| 1046 | |||
| 1047 | // $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well |
||
| 1048 | $fieldNames = array_unique(array_merge( |
||
| 1049 | array_keys($this->record), |
||
| 1050 | array_keys($this->db()) |
||
| 1051 | )); |
||
| 1052 | |||
| 1053 | foreach($fieldNames as $fieldName) { |
||
| 1054 | if(!isset($this->changed[$fieldName])) $this->changed[$fieldName] = self::CHANGE_STRICT; |
||
| 1055 | // Populate the null values in record so that they actually get written |
||
| 1056 | if(!isset($this->record[$fieldName])) $this->record[$fieldName] = null; |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | // @todo Find better way to allow versioned to write a new version after forceChange |
||
| 1060 | if($this->isChanged('Version')) unset($this->changed['Version']); |
||
| 1061 | return $this; |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Validate the current object. |
||
| 1066 | * |
||
| 1067 | * By default, there is no validation - objects are always valid! However, you can overload this method in your |
||
| 1068 | * DataObject sub-classes to specify custom validation, or use the hook through DataExtension. |
||
| 1069 | * |
||
| 1070 | * Invalid objects won't be able to be written - a warning will be thrown and no write will occur. onBeforeWrite() |
||
| 1071 | * and onAfterWrite() won't get called either. |
||
| 1072 | * |
||
| 1073 | * It is expected that you call validate() in your own application to test that an object is valid before |
||
| 1074 | * attempting a write, and respond appropriately if it isn't. |
||
| 1075 | * |
||
| 1076 | * @see {@link ValidationResult} |
||
| 1077 | * @return ValidationResult |
||
| 1078 | */ |
||
| 1079 | public function validate() { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Public accessor for {@see DataObject::validate()} |
||
| 1087 | * |
||
| 1088 | * @return ValidationResult |
||
| 1089 | */ |
||
| 1090 | public function doValidate() { |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Event handler called before writing to the database. |
||
| 1097 | * You can overload this to clean up or otherwise process data before writing it to the |
||
| 1098 | * database. Don't forget to call parent::onBeforeWrite(), though! |
||
| 1099 | * |
||
| 1100 | * This called after {@link $this->validate()}, so you can be sure that your data is valid. |
||
| 1101 | * |
||
| 1102 | * @uses DataExtension->onBeforeWrite() |
||
| 1103 | */ |
||
| 1104 | protected function onBeforeWrite() { |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Event handler called after writing to the database. |
||
| 1113 | * You can overload this to act upon changes made to the data after it is written. |
||
| 1114 | * $this->changed will have a record |
||
| 1115 | * database. Don't forget to call parent::onAfterWrite(), though! |
||
| 1116 | * |
||
| 1117 | * @uses DataExtension->onAfterWrite() |
||
| 1118 | */ |
||
| 1119 | protected function onAfterWrite() { |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Event handler called before deleting from the database. |
||
| 1126 | * You can overload this to clean up or otherwise process data before delete this |
||
| 1127 | * record. Don't forget to call parent::onBeforeDelete(), though! |
||
| 1128 | * |
||
| 1129 | * @uses DataExtension->onBeforeDelete() |
||
| 1130 | */ |
||
| 1131 | protected function onBeforeDelete() { |
||
| 1137 | |||
| 1138 | protected function onAfterDelete() { |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Load the default values in from the self::$defaults array. |
||
| 1144 | * Will traverse the defaults of the current class and all its parent classes. |
||
| 1145 | * Called by the constructor when creating new records. |
||
| 1146 | * |
||
| 1147 | * @uses DataExtension->populateDefaults() |
||
| 1148 | * @return DataObject $this |
||
| 1149 | */ |
||
| 1150 | public function populateDefaults() { |
||
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Determine validation of this object prior to write |
||
| 1184 | * |
||
| 1185 | * @return ValidationException Exception generated by this write, or null if valid |
||
| 1186 | */ |
||
| 1187 | protected function validateWrite() { |
||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Prepare an object prior to write |
||
| 1210 | * |
||
| 1211 | * @throws ValidationException |
||
| 1212 | */ |
||
| 1213 | protected function preWrite() { |
||
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Detects and updates all changes made to this object |
||
| 1232 | * |
||
| 1233 | * @param bool $forceChanges If set to true, force all fields to be treated as changed |
||
| 1234 | * @return bool True if any changes are detected |
||
| 1235 | */ |
||
| 1236 | protected function updateChanges($forceChanges = false) |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Writes a subset of changes for a specific table to the given manipulation |
||
| 1250 | * |
||
| 1251 | * @param string $baseTable Base table |
||
| 1252 | * @param string $now Timestamp to use for the current time |
||
| 1253 | * @param bool $isNewRecord Whether this should be treated as a new record write |
||
| 1254 | * @param array $manipulation Manipulation to write to |
||
| 1255 | * @param string $class Table and Class to select and write to |
||
| 1256 | */ |
||
| 1257 | protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class) { |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Ensures that a blank base record exists with the basic fixed fields for this dataobject |
||
| 1304 | * |
||
| 1305 | * Does nothing if an ID is already assigned for this record |
||
| 1306 | * |
||
| 1307 | * @param string $baseTable Base table |
||
| 1308 | * @param string $now Timestamp to use for the current time |
||
| 1309 | */ |
||
| 1310 | protected function writeBaseRecord($baseTable, $now) { |
||
| 1322 | |||
| 1323 | /** |
||
| 1324 | * Generate and write the database manipulation for all changed fields |
||
| 1325 | * |
||
| 1326 | * @param string $baseTable Base table |
||
| 1327 | * @param string $now Timestamp to use for the current time |
||
| 1328 | * @param bool $isNewRecord If this is a new record |
||
| 1329 | */ |
||
| 1330 | protected function writeManipulation($baseTable, $now, $isNewRecord) { |
||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Writes all changes to this object to the database. |
||
| 1354 | * - It will insert a record whenever ID isn't set, otherwise update. |
||
| 1355 | * - All relevant tables will be updated. |
||
| 1356 | * - $this->onBeforeWrite() gets called beforehand. |
||
| 1357 | * - Extensions such as Versioned will ammend the database-write to ensure that a version is saved. |
||
| 1358 | * |
||
| 1359 | * @uses DataExtension->augmentWrite() |
||
| 1360 | * |
||
| 1361 | * @param boolean $showDebug Show debugging information |
||
| 1362 | * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists |
||
| 1363 | * @param boolean $forceWrite Write to database even if there are no changes |
||
| 1364 | * @param boolean $writeComponents Call write() on all associated component instances which were previously |
||
| 1365 | * retrieved through {@link getComponent()}, {@link getComponents()} or |
||
| 1366 | * {@link getManyManyComponents()} (Default: false) |
||
| 1367 | * @return int The ID of the record |
||
| 1368 | * @throws ValidationException Exception that can be caught and handled by the calling function |
||
| 1369 | */ |
||
| 1370 | public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false) { |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * Writes cached relation lists to the database, if possible |
||
| 1418 | */ |
||
| 1419 | public function writeRelations() { |
||
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Write the cached components to the database. Cached components could refer to two different instances of the |
||
| 1433 | * same record. |
||
| 1434 | * |
||
| 1435 | * @param $recursive Recursively write components |
||
| 1436 | * @return DataObject $this |
||
| 1437 | */ |
||
| 1438 | public function writeComponents($recursive = false) { |
||
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Delete this data object. |
||
| 1449 | * $this->onBeforeDelete() gets called. |
||
| 1450 | * Note that in Versioned objects, both Stage and Live will be deleted. |
||
| 1451 | * @uses DataExtension->augmentSQL() |
||
| 1452 | */ |
||
| 1453 | public function delete() { |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Delete the record with the given ID. |
||
| 1484 | * |
||
| 1485 | * @param string $className The class name of the record to be deleted |
||
| 1486 | * @param int $id ID of record to be deleted |
||
| 1487 | */ |
||
| 1488 | public static function delete_by_id($className, $id) { |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Get the class ancestry, including the current class name. |
||
| 1499 | * The ancestry will be returned as an array of class names, where the 0th element |
||
| 1500 | * will be the class that inherits directly from DataObject, and the last element |
||
| 1501 | * will be the current class. |
||
| 1502 | * |
||
| 1503 | * @return array Class ancestry |
||
| 1504 | */ |
||
| 1505 | public function getClassAncestry() { |
||
| 1514 | |||
| 1515 | /** |
||
| 1516 | * Return a component object from a one to one relationship, as a DataObject. |
||
| 1517 | * If no component is available, an 'empty component' will be returned for |
||
| 1518 | * non-polymorphic relations, or for polymorphic relations with a class set. |
||
| 1519 | * |
||
| 1520 | * @param string $componentName Name of the component |
||
| 1521 | * @return DataObject The component object. It's exact type will be that of the component. |
||
| 1522 | * @throws Exception |
||
| 1523 | */ |
||
| 1524 | public function getComponent($componentName) { |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Returns a one-to-many relation as a HasManyList |
||
| 1595 | * |
||
| 1596 | * @param string $componentName Name of the component |
||
| 1597 | * @return HasManyList The components of the one-to-many relationship. |
||
| 1598 | */ |
||
| 1599 | public function getComponents($componentName) { |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Find the foreign class of a relation on this DataObject, regardless of the relation type. |
||
| 1640 | * |
||
| 1641 | * @param string $relationName Relation name. |
||
| 1642 | * @return string Class name, or null if not found. |
||
| 1643 | */ |
||
| 1644 | public function getRelationClass($relationName) { |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Given a relation name, determine the relation type |
||
| 1671 | * |
||
| 1672 | * @param string $component Name of component |
||
| 1673 | * @return string has_one, has_many, many_many, belongs_many_many or belongs_to |
||
| 1674 | */ |
||
| 1675 | public function getRelationType($component) { |
||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * Given a relation declared on a remote class, generate a substitute component for the opposite |
||
| 1688 | * side of the relation. |
||
| 1689 | * |
||
| 1690 | * Notes on behaviour: |
||
| 1691 | * - This can still be used on components that are defined on both sides, but do not need to be. |
||
| 1692 | * - All has_ones on remote class will be treated as local has_many, even if they are belongs_to |
||
| 1693 | * - Cannot be used on polymorphic relationships |
||
| 1694 | * - Cannot be used on unsaved objects. |
||
| 1695 | * |
||
| 1696 | * @param string $remoteClass |
||
| 1697 | * @param string $remoteRelation |
||
| 1698 | * @return DataList|DataObject The component, either as a list or single object |
||
| 1699 | * @throws BadMethodCallException |
||
| 1700 | * @throws InvalidArgumentException |
||
| 1701 | */ |
||
| 1702 | public function inferReciprocalComponent($remoteClass, $remoteRelation) { |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Tries to find the database key on another object that is used to store a |
||
| 1802 | * relationship to this class. If no join field can be found it defaults to 'ParentID'. |
||
| 1803 | * |
||
| 1804 | * If the remote field is polymorphic then $polymorphic is set to true, and the return value |
||
| 1805 | * is in the form 'Relation' instead of 'RelationID', referencing the composite DBField. |
||
| 1806 | * |
||
| 1807 | * @param string $component Name of the relation on the current object pointing to the |
||
| 1808 | * remote object. |
||
| 1809 | * @param string $type the join type - either 'has_many' or 'belongs_to' |
||
| 1810 | * @param boolean $polymorphic Flag set to true if the remote join field is polymorphic. |
||
| 1811 | * @return string |
||
| 1812 | * @throws Exception |
||
| 1813 | */ |
||
| 1814 | public function getRemoteJoinField($component, $type = 'has_many', &$polymorphic = false) { |
||
| 1882 | |||
| 1883 | /** |
||
| 1884 | * Returns a many-to-many component, as a ManyManyList. |
||
| 1885 | * @param string $componentName Name of the many-many component |
||
| 1886 | * @return ManyManyList The set of components |
||
| 1887 | */ |
||
| 1888 | public function getManyManyComponents($componentName) { |
||
| 1931 | |||
| 1932 | /** |
||
| 1933 | * Return the class of a one-to-one component. If $component is null, return all of the one-to-one components and |
||
| 1934 | * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type. |
||
| 1935 | * |
||
| 1936 | * @return string|array The class of the one-to-one component, or an array of all one-to-one components and |
||
| 1937 | * their classes. |
||
| 1938 | */ |
||
| 1939 | public function hasOne() { |
||
| 1942 | |||
| 1943 | /** |
||
| 1944 | * Return data for a specific has_one component. |
||
| 1945 | * @param string $component |
||
| 1946 | * @return string|null |
||
| 1947 | */ |
||
| 1948 | public function hasOneComponent($component) { |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and |
||
| 1961 | * their class name will be returned. |
||
| 1962 | * |
||
| 1963 | * @param string $component - Name of component |
||
| 1964 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1965 | * the field data stripped off. It defaults to TRUE. |
||
| 1966 | * @return string|array |
||
| 1967 | */ |
||
| 1968 | View Code Duplication | public function belongsTo($component = null, $classOnly = true) { |
|
| 1985 | |||
| 1986 | /** |
||
| 1987 | * Return data for a specific belongs_to component. |
||
| 1988 | * @param string $component |
||
| 1989 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 1990 | * the field data stripped off. It defaults to TRUE. |
||
| 1991 | * @return string|null |
||
| 1992 | */ |
||
| 1993 | View Code Duplication | public function belongsToComponent($component, $classOnly = true) { |
|
| 2004 | |||
| 2005 | /** |
||
| 2006 | * Return all of the database fields in this object |
||
| 2007 | * |
||
| 2008 | * @param string $fieldName Limit the output to a specific field name |
||
| 2009 | * @param string $includeTable If returning a single column, prefix the column with the table name |
||
| 2010 | * in Table.Column(spec) format |
||
| 2011 | * @return array|string|null The database fields, or if searching a single field, just this one field if found |
||
| 2012 | * Field will be a string in ClassName(args) format, or Table.ClassName(args) format if $includeTable is true |
||
| 2013 | */ |
||
| 2014 | public function db($fieldName = null, $includeTable = false) { |
||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many |
||
| 2056 | * relationships and their classes will be returned. |
||
| 2057 | * |
||
| 2058 | * @param string $component Deprecated - Name of component |
||
| 2059 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2060 | * the field data stripped off. It defaults to TRUE. |
||
| 2061 | * @return string|array|false |
||
| 2062 | */ |
||
| 2063 | View Code Duplication | public function hasMany($component = null, $classOnly = true) { |
|
| 2080 | |||
| 2081 | /** |
||
| 2082 | * Return data for a specific has_many component. |
||
| 2083 | * @param string $component |
||
| 2084 | * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have |
||
| 2085 | * the field data stripped off. It defaults to TRUE. |
||
| 2086 | * @return string|null |
||
| 2087 | */ |
||
| 2088 | View Code Duplication | public function hasManyComponent($component, $classOnly = true) { |
|
| 2099 | |||
| 2100 | /** |
||
| 2101 | * Return the many-to-many extra fields specification. |
||
| 2102 | * |
||
| 2103 | * If you don't specify a component name, it returns all |
||
| 2104 | * extra fields for all components available. |
||
| 2105 | * |
||
| 2106 | * @return array|null |
||
| 2107 | */ |
||
| 2108 | public function manyManyExtraFields() { |
||
| 2111 | |||
| 2112 | /** |
||
| 2113 | * Return the many-to-many extra fields specification for a specific component. |
||
| 2114 | * @param string $component |
||
| 2115 | * @return array|null |
||
| 2116 | */ |
||
| 2117 | public function manyManyExtraFieldsForComponent($component) { |
||
| 2157 | |||
| 2158 | /** |
||
| 2159 | * Return information about a many-to-many component. |
||
| 2160 | * The return value is an array of (parentclass, childclass). If $component is null, then all many-many |
||
| 2161 | * components are returned. |
||
| 2162 | * |
||
| 2163 | * @see DataObject::manyManyComponent() |
||
| 2164 | * @return array|null An array of (parentclass, childclass), or an array of all many-many components |
||
| 2165 | */ |
||
| 2166 | public function manyMany() { |
||
| 2172 | |||
| 2173 | /** |
||
| 2174 | * Return information about a specific many_many component. Returns a numeric array of: |
||
| 2175 | * array( |
||
| 2176 | * <classname>, The class that relation is defined in e.g. "Product" |
||
| 2177 | * <candidateName>, The target class of the relation e.g. "Category" |
||
| 2178 | * <parentField>, The field name pointing to <classname>'s table e.g. "ProductID" |
||
| 2179 | * <childField>, The field name pointing to <candidatename>'s table e.g. "CategoryID" |
||
| 2180 | * <joinTable> The join table between the two classes e.g. "Product_Categories" |
||
| 2181 | * ) |
||
| 2182 | * @param string $component The component name |
||
| 2183 | * @return array|null |
||
| 2184 | */ |
||
| 2185 | public function manyManyComponent($component) { |
||
| 2240 | |||
| 2241 | /** |
||
| 2242 | * This returns an array (if it exists) describing the database extensions that are required, or false if none |
||
| 2243 | * |
||
| 2244 | * This is experimental, and is currently only a Postgres-specific enhancement. |
||
| 2245 | * |
||
| 2246 | * @return array or false |
||
| 2247 | */ |
||
| 2248 | public function database_extensions($class){ |
||
| 2256 | |||
| 2257 | /** |
||
| 2258 | * Generates a SearchContext to be used for building and processing |
||
| 2259 | * a generic search form for properties on this object. |
||
| 2260 | * |
||
| 2261 | * @return SearchContext |
||
| 2262 | */ |
||
| 2263 | public function getDefaultSearchContext() { |
||
| 2270 | |||
| 2271 | /** |
||
| 2272 | * Determine which properties on the DataObject are |
||
| 2273 | * searchable, and map them to their default {@link FormField} |
||
| 2274 | * representations. Used for scaffolding a searchform for {@link ModelAdmin}. |
||
| 2275 | * |
||
| 2276 | * Some additional logic is included for switching field labels, based on |
||
| 2277 | * how generic or specific the field type is. |
||
| 2278 | * |
||
| 2279 | * Used by {@link SearchContext}. |
||
| 2280 | * |
||
| 2281 | * @param array $_params |
||
| 2282 | * 'fieldClasses': Associative array of field names as keys and FormField classes as values |
||
| 2283 | * 'restrictFields': Numeric array of a field name whitelist |
||
| 2284 | * @return FieldList |
||
| 2285 | */ |
||
| 2286 | public function scaffoldSearchFields($_params = null) { |
||
| 2338 | |||
| 2339 | /** |
||
| 2340 | * Scaffold a simple edit form for all properties on this dataobject, |
||
| 2341 | * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}. |
||
| 2342 | * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}. |
||
| 2343 | * |
||
| 2344 | * @uses FormScaffolder |
||
| 2345 | * |
||
| 2346 | * @param array $_params Associative array passing through properties to {@link FormScaffolder}. |
||
| 2347 | * @return FieldList |
||
| 2348 | */ |
||
| 2349 | public function scaffoldFormFields($_params = null) { |
||
| 2370 | |||
| 2371 | /** |
||
| 2372 | * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields |
||
| 2373 | * being called on extensions |
||
| 2374 | * |
||
| 2375 | * @param callable $callback The callback to execute |
||
| 2376 | */ |
||
| 2377 | protected function beforeUpdateCMSFields($callback) { |
||
| 2380 | |||
| 2381 | /** |
||
| 2382 | * Centerpiece of every data administration interface in Silverstripe, |
||
| 2383 | * which returns a {@link FieldList} suitable for a {@link Form} object. |
||
| 2384 | * If not overloaded, we're using {@link scaffoldFormFields()} to automatically |
||
| 2385 | * generate this set. To customize, overload this method in a subclass |
||
| 2386 | * or extended onto it by using {@link DataExtension->updateCMSFields()}. |
||
| 2387 | * |
||
| 2388 | * <code> |
||
| 2389 | * class MyCustomClass extends DataObject { |
||
| 2390 | * static $db = array('CustomProperty'=>'Boolean'); |
||
| 2391 | * |
||
| 2392 | * function getCMSFields() { |
||
| 2393 | * $fields = parent::getCMSFields(); |
||
| 2394 | * $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty')); |
||
| 2395 | * return $fields; |
||
| 2396 | * } |
||
| 2397 | * } |
||
| 2398 | * </code> |
||
| 2399 | * |
||
| 2400 | * @see Good example of complex FormField building: SiteTree::getCMSFields() |
||
| 2401 | * |
||
| 2402 | * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms. |
||
| 2403 | */ |
||
| 2404 | public function getCMSFields() { |
||
| 2416 | |||
| 2417 | /** |
||
| 2418 | * need to be overload by solid dataobject, so that the customised actions of that dataobject, |
||
| 2419 | * including that dataobject's extensions customised actions could be added to the EditForm. |
||
| 2420 | * |
||
| 2421 | * @return an Empty FieldList(); need to be overload by solid subclass |
||
| 2422 | */ |
||
| 2423 | public function getCMSActions() { |
||
| 2428 | |||
| 2429 | |||
| 2430 | /** |
||
| 2431 | * Used for simple frontend forms without relation editing |
||
| 2432 | * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()} |
||
| 2433 | * by default. To customize, either overload this method in your |
||
| 2434 | * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}. |
||
| 2435 | * |
||
| 2436 | * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API |
||
| 2437 | * |
||
| 2438 | * @param array $params See {@link scaffoldFormFields()} |
||
| 2439 | * @return FieldList Always returns a simple field collection without TabSet. |
||
| 2440 | */ |
||
| 2441 | public function getFrontEndFields($params = null) { |
||
| 2447 | |||
| 2448 | /** |
||
| 2449 | * Gets the value of a field. |
||
| 2450 | * Called by {@link __get()} and any getFieldName() methods you might create. |
||
| 2451 | * |
||
| 2452 | * @param string $field The name of the field |
||
| 2453 | * |
||
| 2454 | * @return mixed The field value |
||
| 2455 | */ |
||
| 2456 | public function getField($field) { |
||
| 2475 | |||
| 2476 | /** |
||
| 2477 | * Loads all the stub fields that an initial lazy load didn't load fully. |
||
| 2478 | * |
||
| 2479 | * @param string $tableClass Base table to load the values from. Others are joined as required. |
||
| 2480 | * Not specifying a tableClass will load all lazy fields from all tables. |
||
| 2481 | * @return bool Flag if lazy loading succeeded |
||
| 2482 | */ |
||
| 2483 | protected function loadLazyFields($tableClass = null) { |
||
| 2557 | |||
| 2558 | /** |
||
| 2559 | * Return the fields that have changed. |
||
| 2560 | * |
||
| 2561 | * The change level affects what the functions defines as "changed": |
||
| 2562 | * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones. |
||
| 2563 | * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes, |
||
| 2564 | * for example a change from 0 to null would not be included. |
||
| 2565 | * |
||
| 2566 | * Example return: |
||
| 2567 | * <code> |
||
| 2568 | * array( |
||
| 2569 | * 'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE) |
||
| 2570 | * ) |
||
| 2571 | * </code> |
||
| 2572 | * |
||
| 2573 | * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true |
||
| 2574 | * to return all database fields, or an array for an explicit filter. false returns all fields. |
||
| 2575 | * @param int $changeLevel The strictness of what is defined as change. Defaults to strict |
||
| 2576 | * @return array |
||
| 2577 | */ |
||
| 2578 | public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT) { |
||
| 2619 | |||
| 2620 | /** |
||
| 2621 | * Uses {@link getChangedFields()} to determine if fields have been changed |
||
| 2622 | * since loading them from the database. |
||
| 2623 | * |
||
| 2624 | * @param string $fieldName Name of the database field to check, will check for any if not given |
||
| 2625 | * @param int $changeLevel See {@link getChangedFields()} |
||
| 2626 | * @return boolean |
||
| 2627 | */ |
||
| 2628 | public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT) { |
||
| 2638 | |||
| 2639 | /** |
||
| 2640 | * Set the value of the field |
||
| 2641 | * Called by {@link __set()} and any setFieldName() methods you might create. |
||
| 2642 | * |
||
| 2643 | * @param string $fieldName Name of the field |
||
| 2644 | * @param mixed $val New field value |
||
| 2645 | * @return DataObject $this |
||
| 2646 | */ |
||
| 2647 | public function setField($fieldName, $val) { |
||
| 2697 | |||
| 2698 | /** |
||
| 2699 | * Set the value of the field, using a casting object. |
||
| 2700 | * This is useful when you aren't sure that a date is in SQL format, for example. |
||
| 2701 | * setCastedField() can also be used, by forms, to set related data. For example, uploaded images |
||
| 2702 | * can be saved into the Image table. |
||
| 2703 | * |
||
| 2704 | * @param string $fieldName Name of the field |
||
| 2705 | * @param mixed $value New field value |
||
| 2706 | * @return $this |
||
| 2707 | */ |
||
| 2708 | public function setCastedField($fieldName, $value) { |
||
| 2721 | |||
| 2722 | /** |
||
| 2723 | * {@inheritdoc} |
||
| 2724 | */ |
||
| 2725 | public function castingHelper($field) { |
||
| 2743 | |||
| 2744 | /** |
||
| 2745 | * Returns true if the given field exists in a database column on any of |
||
| 2746 | * the objects tables and optionally look up a dynamic getter with |
||
| 2747 | * get<fieldName>(). |
||
| 2748 | * |
||
| 2749 | * @param string $field Name of the field |
||
| 2750 | * @return boolean True if the given field exists |
||
| 2751 | */ |
||
| 2752 | public function hasField($field) { |
||
| 2760 | |||
| 2761 | /** |
||
| 2762 | * Returns true if the given field exists as a database column |
||
| 2763 | * |
||
| 2764 | * @param string $field Name of the field |
||
| 2765 | * |
||
| 2766 | * @return boolean |
||
| 2767 | */ |
||
| 2768 | public function hasDatabaseField($field) { |
||
| 2772 | |||
| 2773 | /** |
||
| 2774 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2775 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2776 | * |
||
| 2777 | * @param string $field Name of the field |
||
| 2778 | * @return string The field type of the given field |
||
| 2779 | */ |
||
| 2780 | public function hasOwnTableDatabaseField($field) { |
||
| 2783 | |||
| 2784 | /** |
||
| 2785 | * Returns the field type of the given field, if it belongs to this class, and not a parent. |
||
| 2786 | * Note that the field type will not include constructor arguments in round brackets, only the classname. |
||
| 2787 | * |
||
| 2788 | * @param string $class Class name to check |
||
| 2789 | * @param string $field Name of the field |
||
| 2790 | * @return string The field type of the given field |
||
| 2791 | */ |
||
| 2792 | public static function has_own_table_database_field($class, $field) { |
||
| 2802 | |||
| 2803 | /** |
||
| 2804 | * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than |
||
| 2805 | * actually looking in the database. |
||
| 2806 | * |
||
| 2807 | * @param string $dataClass |
||
| 2808 | * @return bool |
||
| 2809 | */ |
||
| 2810 | public static function has_own_table($dataClass) { |
||
| 2825 | |||
| 2826 | /** |
||
| 2827 | * Returns true if the member is allowed to do the given action. |
||
| 2828 | * See {@link extendedCan()} for a more versatile tri-state permission control. |
||
| 2829 | * |
||
| 2830 | * @param string $perm The permission to be checked, such as 'View'. |
||
| 2831 | * @param Member $member The member whose permissions need checking. Defaults to the currently logged |
||
| 2832 | * in user. |
||
| 2833 | * @param array $context Additional $context to pass to extendedCan() |
||
| 2834 | * |
||
| 2835 | * @return boolean True if the the member is allowed to do the given action |
||
| 2836 | */ |
||
| 2837 | public function can($perm, $member = null, $context = array()) { |
||
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Process tri-state responses from permission-alterting extensions. The extensions are |
||
| 2899 | * expected to return one of three values: |
||
| 2900 | * |
||
| 2901 | * - false: Disallow this permission, regardless of what other extensions say |
||
| 2902 | * - true: Allow this permission, as long as no other extensions return false |
||
| 2903 | * - NULL: Don't affect the outcome |
||
| 2904 | * |
||
| 2905 | * This method itself returns a tri-state value, and is designed to be used like this: |
||
| 2906 | * |
||
| 2907 | * <code> |
||
| 2908 | * $extended = $this->extendedCan('canDoSomething', $member); |
||
| 2909 | * if($extended !== null) return $extended; |
||
| 2910 | * else return $normalValue; |
||
| 2911 | * </code> |
||
| 2912 | * |
||
| 2913 | * @param string $methodName Method on the same object, e.g. {@link canEdit()} |
||
| 2914 | * @param Member|int $member |
||
| 2915 | * @param array $context Optional context |
||
| 2916 | * @return boolean|null |
||
| 2917 | */ |
||
| 2918 | public function extendedCan($methodName, $member, $context = array()) { |
||
| 2929 | |||
| 2930 | /** |
||
| 2931 | * @param Member $member |
||
| 2932 | * @return boolean |
||
| 2933 | */ |
||
| 2934 | View Code Duplication | public function canView($member = null) { |
|
| 2941 | |||
| 2942 | /** |
||
| 2943 | * @param Member $member |
||
| 2944 | * @return boolean |
||
| 2945 | */ |
||
| 2946 | View Code Duplication | public function canEdit($member = null) { |
|
| 2953 | |||
| 2954 | /** |
||
| 2955 | * @param Member $member |
||
| 2956 | * @return boolean |
||
| 2957 | */ |
||
| 2958 | View Code Duplication | public function canDelete($member = null) { |
|
| 2965 | |||
| 2966 | /** |
||
| 2967 | * @param Member $member |
||
| 2968 | * @param array $context Additional context-specific data which might |
||
| 2969 | * affect whether (or where) this object could be created. |
||
| 2970 | * @return boolean |
||
| 2971 | */ |
||
| 2972 | View Code Duplication | public function canCreate($member = null, $context = array()) { |
|
| 2979 | |||
| 2980 | /** |
||
| 2981 | * Debugging used by Debug::show() |
||
| 2982 | * |
||
| 2983 | * @return string HTML data representing this object |
||
| 2984 | */ |
||
| 2985 | public function debug() { |
||
| 2993 | |||
| 2994 | /** |
||
| 2995 | * Return the DBField object that represents the given field. |
||
| 2996 | * This works similarly to obj() with 2 key differences: |
||
| 2997 | * - it still returns an object even when the field has no value. |
||
| 2998 | * - it only matches fields and not methods |
||
| 2999 | * - it matches foreign keys generated by has_one relationships, eg, "ParentID" |
||
| 3000 | * |
||
| 3001 | * @param string $fieldName Name of the field |
||
| 3002 | * @return DBField The field as a DBField object |
||
| 3003 | */ |
||
| 3004 | public function dbObject($fieldName) { |
||
| 3024 | |||
| 3025 | /** |
||
| 3026 | * Traverses to a DBField referenced by relationships between data objects. |
||
| 3027 | * |
||
| 3028 | * The path to the related field is specified with dot separated syntax |
||
| 3029 | * (eg: Parent.Child.Child.FieldName). |
||
| 3030 | * |
||
| 3031 | * @param string $fieldPath |
||
| 3032 | * |
||
| 3033 | * @return mixed DBField of the field on the object or a DataList instance. |
||
| 3034 | */ |
||
| 3035 | public function relObject($fieldPath) { |
||
| 3065 | |||
| 3066 | /** |
||
| 3067 | * Traverses to a field referenced by relationships between data objects, returning the value |
||
| 3068 | * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName) |
||
| 3069 | * |
||
| 3070 | * @param $fieldName string |
||
| 3071 | * @return string | null - will return null on a missing value |
||
| 3072 | */ |
||
| 3073 | View Code Duplication | public function relField($fieldName) { |
|
| 3108 | |||
| 3109 | /** |
||
| 3110 | * Temporary hack to return an association name, based on class, to get around the mangle |
||
| 3111 | * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys. |
||
| 3112 | * |
||
| 3113 | * @return String |
||
| 3114 | */ |
||
| 3115 | public function getReverseAssociation($className) { |
||
| 3131 | |||
| 3132 | /** |
||
| 3133 | * Return all objects matching the filter |
||
| 3134 | * sub-classes are automatically selected and included |
||
| 3135 | * |
||
| 3136 | * @param string $callerClass The class of objects to be returned |
||
| 3137 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3138 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3139 | * @param string|array $sort A sort expression to be inserted into the ORDER |
||
| 3140 | * BY clause. If omitted, self::$default_sort will be used. |
||
| 3141 | * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead. |
||
| 3142 | * @param string|array $limit A limit expression to be inserted into the LIMIT clause. |
||
| 3143 | * @param string $containerClass The container class to return the results in. |
||
| 3144 | * |
||
| 3145 | * @todo $containerClass is Ignored, why? |
||
| 3146 | * |
||
| 3147 | * @return DataList The objects matching the filter, in the class specified by $containerClass |
||
| 3148 | */ |
||
| 3149 | public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, |
||
| 3186 | |||
| 3187 | |||
| 3188 | /** |
||
| 3189 | * Return the first item matching the given query. |
||
| 3190 | * All calls to get_one() are cached. |
||
| 3191 | * |
||
| 3192 | * @param string $callerClass The class of objects to be returned |
||
| 3193 | * @param string|array $filter A filter to be inserted into the WHERE clause. |
||
| 3194 | * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples. |
||
| 3195 | * @param boolean $cache Use caching |
||
| 3196 | * @param string $orderby A sort expression to be inserted into the ORDER BY clause. |
||
| 3197 | * |
||
| 3198 | * @return DataObject The first item matching the query |
||
| 3199 | */ |
||
| 3200 | public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { |
||
| 3226 | |||
| 3227 | /** |
||
| 3228 | * Flush the cached results for all relations (has_one, has_many, many_many) |
||
| 3229 | * Also clears any cached aggregate data. |
||
| 3230 | * |
||
| 3231 | * @param boolean $persistent When true will also clear persistent data stored in the Cache system. |
||
| 3232 | * When false will just clear session-local cached data |
||
| 3233 | * @return DataObject $this |
||
| 3234 | */ |
||
| 3235 | public function flushCache($persistent = true) { |
||
| 3251 | |||
| 3252 | /** |
||
| 3253 | * Flush the get_one global cache and destroy associated objects. |
||
| 3254 | */ |
||
| 3255 | public static function flush_and_destroy_cache() { |
||
| 3263 | |||
| 3264 | /** |
||
| 3265 | * Reset all global caches associated with DataObject. |
||
| 3266 | */ |
||
| 3267 | public static function reset() { |
||
| 3276 | |||
| 3277 | /** |
||
| 3278 | * Return the given element, searching by ID |
||
| 3279 | * |
||
| 3280 | * @param string $callerClass The class of the object to be returned |
||
| 3281 | * @param int $id The id of the element |
||
| 3282 | * @param boolean $cache See {@link get_one()} |
||
| 3283 | * |
||
| 3284 | * @return DataObject The element |
||
| 3285 | */ |
||
| 3286 | public static function get_by_id($callerClass, $id, $cache = true) { |
||
| 3303 | |||
| 3304 | /** |
||
| 3305 | * Get the name of the base table for this object |
||
| 3306 | */ |
||
| 3307 | public function baseTable() { |
||
| 3311 | |||
| 3312 | /** |
||
| 3313 | * @var Array Parameters used in the query that built this object. |
||
| 3314 | * This can be used by decorators (e.g. lazy loading) to |
||
| 3315 | * run additional queries using the same context. |
||
| 3316 | */ |
||
| 3317 | protected $sourceQueryParams; |
||
| 3318 | |||
| 3319 | /** |
||
| 3320 | * @see $sourceQueryParams |
||
| 3321 | * @return array |
||
| 3322 | */ |
||
| 3323 | public function getSourceQueryParams() { |
||
| 3326 | |||
| 3327 | /** |
||
| 3328 | * Get list of parameters that should be inherited to relations on this object |
||
| 3329 | * |
||
| 3330 | * @return array |
||
| 3331 | */ |
||
| 3332 | public function getInheritableQueryParams() { |
||
| 3337 | |||
| 3338 | /** |
||
| 3339 | * @see $sourceQueryParams |
||
| 3340 | * @param array |
||
| 3341 | */ |
||
| 3342 | public function setSourceQueryParams($array) { |
||
| 3345 | |||
| 3346 | /** |
||
| 3347 | * @see $sourceQueryParams |
||
| 3348 | * @param array |
||
| 3349 | */ |
||
| 3350 | public function setSourceQueryParam($key, $value) { |
||
| 3353 | |||
| 3354 | /** |
||
| 3355 | * @see $sourceQueryParams |
||
| 3356 | * @return Mixed |
||
| 3357 | */ |
||
| 3358 | public function getSourceQueryParam($key) { |
||
| 3362 | |||
| 3363 | //-------------------------------------------------------------------------------------------// |
||
| 3364 | |||
| 3365 | /** |
||
| 3366 | * Return the database indexes on this table. |
||
| 3367 | * This array is indexed by the name of the field with the index, and |
||
| 3368 | * the value is the type of index. |
||
| 3369 | */ |
||
| 3370 | public function databaseIndexes() { |
||
| 3395 | |||
| 3396 | /** |
||
| 3397 | * Check the database schema and update it as necessary. |
||
| 3398 | * |
||
| 3399 | * @uses DataExtension->augmentDatabase() |
||
| 3400 | */ |
||
| 3401 | public function requireTable() { |
||
| 3446 | |||
| 3447 | /** |
||
| 3448 | * Validate that the configured relations for this class use the correct syntaxes |
||
| 3449 | * @throws LogicException |
||
| 3450 | */ |
||
| 3451 | protected function validateModelDefinitions() { |
||
| 3482 | |||
| 3483 | /** |
||
| 3484 | * Add default records to database. This function is called whenever the |
||
| 3485 | * database is built, after the database tables have all been created. Overload |
||
| 3486 | * this to add default records when the database is built, but make sure you |
||
| 3487 | * call parent::requireDefaultRecords(). |
||
| 3488 | * |
||
| 3489 | * @uses DataExtension->requireDefaultRecords() |
||
| 3490 | */ |
||
| 3491 | public function requireDefaultRecords() { |
||
| 3509 | |||
| 3510 | /** |
||
| 3511 | * Get the default searchable fields for this object, as defined in the |
||
| 3512 | * $searchable_fields list. If searchable fields are not defined on the |
||
| 3513 | * data object, uses a default selection of summary fields. |
||
| 3514 | * |
||
| 3515 | * @return array |
||
| 3516 | */ |
||
| 3517 | public function searchableFields() { |
||
| 3591 | |||
| 3592 | /** |
||
| 3593 | * Get any user defined searchable fields labels that |
||
| 3594 | * exist. Allows overriding of default field names in the form |
||
| 3595 | * interface actually presented to the user. |
||
| 3596 | * |
||
| 3597 | * The reason for keeping this separate from searchable_fields, |
||
| 3598 | * which would be a logical place for this functionality, is to |
||
| 3599 | * avoid bloating and complicating the configuration array. Currently |
||
| 3600 | * much of this system is based on sensible defaults, and this property |
||
| 3601 | * would generally only be set in the case of more complex relationships |
||
| 3602 | * between data object being required in the search interface. |
||
| 3603 | * |
||
| 3604 | * Generates labels based on name of the field itself, if no static property |
||
| 3605 | * {@link self::field_labels} exists. |
||
| 3606 | * |
||
| 3607 | * @uses $field_labels |
||
| 3608 | * @uses FormField::name_to_label() |
||
| 3609 | * |
||
| 3610 | * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields |
||
| 3611 | * |
||
| 3612 | * @return array|string Array of all element labels if no argument given, otherwise the label of the field |
||
| 3613 | */ |
||
| 3614 | public function fieldLabels($includerelations = true) { |
||
| 3649 | |||
| 3650 | /** |
||
| 3651 | * Get a human-readable label for a single field, |
||
| 3652 | * see {@link fieldLabels()} for more details. |
||
| 3653 | * |
||
| 3654 | * @uses fieldLabels() |
||
| 3655 | * @uses FormField::name_to_label() |
||
| 3656 | * |
||
| 3657 | * @param string $name Name of the field |
||
| 3658 | * @return string Label of the field |
||
| 3659 | */ |
||
| 3660 | public function fieldLabel($name) { |
||
| 3664 | |||
| 3665 | /** |
||
| 3666 | * Get the default summary fields for this object. |
||
| 3667 | * |
||
| 3668 | * @todo use the translation apparatus to return a default field selection for the language |
||
| 3669 | * |
||
| 3670 | * @return array |
||
| 3671 | */ |
||
| 3672 | public function summaryFields() { |
||
| 3705 | |||
| 3706 | /** |
||
| 3707 | * Defines a default list of filters for the search context. |
||
| 3708 | * |
||
| 3709 | * If a filter class mapping is defined on the data object, |
||
| 3710 | * it is constructed here. Otherwise, the default filter specified in |
||
| 3711 | * {@link DBField} is used. |
||
| 3712 | * |
||
| 3713 | * @todo error handling/type checking for valid FormField and SearchFilter subclasses? |
||
| 3714 | * |
||
| 3715 | * @return array |
||
| 3716 | */ |
||
| 3717 | public function defaultSearchFilters() { |
||
| 3738 | |||
| 3739 | /** |
||
| 3740 | * @return boolean True if the object is in the database |
||
| 3741 | */ |
||
| 3742 | public function isInDB() { |
||
| 3745 | |||
| 3746 | /* |
||
| 3747 | * @ignore |
||
| 3748 | */ |
||
| 3749 | private static $subclass_access = true; |
||
| 3750 | |||
| 3751 | /** |
||
| 3752 | * Temporarily disable subclass access in data object qeur |
||
| 3753 | */ |
||
| 3754 | public static function disable_subclass_access() { |
||
| 3760 | |||
| 3761 | //-------------------------------------------------------------------------------------------// |
||
| 3762 | |||
| 3763 | /** |
||
| 3764 | * Database field definitions. |
||
| 3765 | * This is a map from field names to field type. The field |
||
| 3766 | * type should be a class that extends . |
||
| 3767 | * @var array |
||
| 3768 | * @config |
||
| 3769 | */ |
||
| 3770 | private static $db = null; |
||
| 3771 | |||
| 3772 | /** |
||
| 3773 | * Use a casting object for a field. This is a map from |
||
| 3774 | * field name to class name of the casting object. |
||
| 3775 | * |
||
| 3776 | * @var array |
||
| 3777 | */ |
||
| 3778 | private static $casting = array( |
||
| 3779 | "Title" => 'Text', |
||
| 3780 | ); |
||
| 3781 | |||
| 3782 | /** |
||
| 3783 | * Specify custom options for a CREATE TABLE call. |
||
| 3784 | * Can be used to specify a custom storage engine for specific database table. |
||
| 3785 | * All options have to be keyed for a specific database implementation, |
||
| 3786 | * identified by their class name (extending from {@link SS_Database}). |
||
| 3787 | * |
||
| 3788 | * <code> |
||
| 3789 | * array( |
||
| 3790 | * 'MySQLDatabase' => 'ENGINE=MyISAM' |
||
| 3791 | * ) |
||
| 3792 | * </code> |
||
| 3793 | * |
||
| 3794 | * Caution: This API is experimental, and might not be |
||
| 3795 | * included in the next major release. Please use with care. |
||
| 3796 | * |
||
| 3797 | * @var array |
||
| 3798 | * @config |
||
| 3799 | */ |
||
| 3800 | private static $create_table_options = array( |
||
| 3801 | 'MySQLDatabase' => 'ENGINE=InnoDB' |
||
| 3802 | ); |
||
| 3803 | |||
| 3804 | /** |
||
| 3805 | * If a field is in this array, then create a database index |
||
| 3806 | * on that field. This is a map from fieldname to index type. |
||
| 3807 | * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation. |
||
| 3808 | * |
||
| 3809 | * @var array |
||
| 3810 | * @config |
||
| 3811 | */ |
||
| 3812 | private static $indexes = null; |
||
| 3813 | |||
| 3814 | /** |
||
| 3815 | * Inserts standard column-values when a DataObject |
||
| 3816 | * is instanciated. Does not insert default records {@see $default_records}. |
||
| 3817 | * This is a map from fieldname to default value. |
||
| 3818 | * |
||
| 3819 | * - If you would like to change a default value in a sub-class, just specify it. |
||
| 3820 | * - If you would like to disable the default value given by a parent class, set the default value to 0,'', |
||
| 3821 | * or false in your subclass. Setting it to null won't work. |
||
| 3822 | * |
||
| 3823 | * @var array |
||
| 3824 | * @config |
||
| 3825 | */ |
||
| 3826 | private static $defaults = null; |
||
| 3827 | |||
| 3828 | /** |
||
| 3829 | * Multidimensional array which inserts default data into the database |
||
| 3830 | * on a db/build-call as long as the database-table is empty. Please use this only |
||
| 3831 | * for simple constructs, not for SiteTree-Objects etc. which need special |
||
| 3832 | * behaviour such as publishing and ParentNodes. |
||
| 3833 | * |
||
| 3834 | * Example: |
||
| 3835 | * array( |
||
| 3836 | * array('Title' => "DefaultPage1", 'PageTitle' => 'page1'), |
||
| 3837 | * array('Title' => "DefaultPage2") |
||
| 3838 | * ). |
||
| 3839 | * |
||
| 3840 | * @var array |
||
| 3841 | * @config |
||
| 3842 | */ |
||
| 3843 | private static $default_records = null; |
||
| 3844 | |||
| 3845 | /** |
||
| 3846 | * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a |
||
| 3847 | * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class. |
||
| 3848 | * |
||
| 3849 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3850 | * |
||
| 3851 | * @var array |
||
| 3852 | * @config |
||
| 3853 | */ |
||
| 3854 | private static $has_one = null; |
||
| 3855 | |||
| 3856 | /** |
||
| 3857 | * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}. |
||
| 3858 | * |
||
| 3859 | * This does not actually create any data structures, but allows you to query the other object in a one-to-one |
||
| 3860 | * relationship from the child object. If you have multiple belongs_to links to another object you can use the |
||
| 3861 | * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use. |
||
| 3862 | * |
||
| 3863 | * Note that you cannot have a has_one and belongs_to relationship with the same name. |
||
| 3864 | * |
||
| 3865 | * @var array |
||
| 3866 | * @config |
||
| 3867 | */ |
||
| 3868 | private static $belongs_to; |
||
| 3869 | |||
| 3870 | /** |
||
| 3871 | * This defines a one-to-many relationship. It is a map of component name to the remote data class. |
||
| 3872 | * |
||
| 3873 | * This relationship type does not actually create a data structure itself - you need to define a matching $has_one |
||
| 3874 | * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this |
||
| 3875 | * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show |
||
| 3876 | * which foreign key to use. |
||
| 3877 | * |
||
| 3878 | * @var array |
||
| 3879 | * @config |
||
| 3880 | */ |
||
| 3881 | private static $has_many = null; |
||
| 3882 | |||
| 3883 | /** |
||
| 3884 | * many-many relationship definitions. |
||
| 3885 | * This is a map from component name to data type. |
||
| 3886 | * @var array |
||
| 3887 | * @config |
||
| 3888 | */ |
||
| 3889 | private static $many_many = null; |
||
| 3890 | |||
| 3891 | /** |
||
| 3892 | * Extra fields to include on the connecting many-many table. |
||
| 3893 | * This is a map from field name to field type. |
||
| 3894 | * |
||
| 3895 | * Example code: |
||
| 3896 | * <code> |
||
| 3897 | * public static $many_many_extraFields = array( |
||
| 3898 | * 'Members' => array( |
||
| 3899 | * 'Role' => 'Varchar(100)' |
||
| 3900 | * ) |
||
| 3901 | * ); |
||
| 3902 | * </code> |
||
| 3903 | * |
||
| 3904 | * @var array |
||
| 3905 | * @config |
||
| 3906 | */ |
||
| 3907 | private static $many_many_extraFields = null; |
||
| 3908 | |||
| 3909 | /** |
||
| 3910 | * The inverse side of a many-many relationship. |
||
| 3911 | * This is a map from component name to data type. |
||
| 3912 | * @var array |
||
| 3913 | * @config |
||
| 3914 | */ |
||
| 3915 | private static $belongs_many_many = null; |
||
| 3916 | |||
| 3917 | /** |
||
| 3918 | * The default sort expression. This will be inserted in the ORDER BY |
||
| 3919 | * clause of a SQL query if no other sort expression is provided. |
||
| 3920 | * @var string |
||
| 3921 | * @config |
||
| 3922 | */ |
||
| 3923 | private static $default_sort = null; |
||
| 3924 | |||
| 3925 | /** |
||
| 3926 | * Default list of fields that can be scaffolded by the ModelAdmin |
||
| 3927 | * search interface. |
||
| 3928 | * |
||
| 3929 | * Overriding the default filter, with a custom defined filter: |
||
| 3930 | * <code> |
||
| 3931 | * static $searchable_fields = array( |
||
| 3932 | * "Name" => "PartialMatchFilter" |
||
| 3933 | * ); |
||
| 3934 | * </code> |
||
| 3935 | * |
||
| 3936 | * Overriding the default form fields, with a custom defined field. |
||
| 3937 | * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}. |
||
| 3938 | * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}. |
||
| 3939 | * <code> |
||
| 3940 | * static $searchable_fields = array( |
||
| 3941 | * "Name" => array( |
||
| 3942 | * "field" => "TextField" |
||
| 3943 | * ) |
||
| 3944 | * ); |
||
| 3945 | * </code> |
||
| 3946 | * |
||
| 3947 | * Overriding the default form field, filter and title: |
||
| 3948 | * <code> |
||
| 3949 | * static $searchable_fields = array( |
||
| 3950 | * "Organisation.ZipCode" => array( |
||
| 3951 | * "field" => "TextField", |
||
| 3952 | * "filter" => "PartialMatchFilter", |
||
| 3953 | * "title" => 'Organisation ZIP' |
||
| 3954 | * ) |
||
| 3955 | * ); |
||
| 3956 | * </code> |
||
| 3957 | * @config |
||
| 3958 | */ |
||
| 3959 | private static $searchable_fields = null; |
||
| 3960 | |||
| 3961 | /** |
||
| 3962 | * User defined labels for searchable_fields, used to override |
||
| 3963 | * default display in the search form. |
||
| 3964 | * @config |
||
| 3965 | */ |
||
| 3966 | private static $field_labels = null; |
||
| 3967 | |||
| 3968 | /** |
||
| 3969 | * Provides a default list of fields to be used by a 'summary' |
||
| 3970 | * view of this object. |
||
| 3971 | * @config |
||
| 3972 | */ |
||
| 3973 | private static $summary_fields = null; |
||
| 3974 | |||
| 3975 | /** |
||
| 3976 | * Collect all static properties on the object |
||
| 3977 | * which contain natural language, and need to be translated. |
||
| 3978 | * The full entity name is composed from the class name and a custom identifier. |
||
| 3979 | * |
||
| 3980 | * @return array A numerical array which contains one or more entities in array-form. |
||
| 3981 | * Each numeric entity array contains the "arguments" for a _t() call as array values: |
||
| 3982 | * $entity, $string, $priority, $context. |
||
| 3983 | */ |
||
| 3984 | public function provideI18nEntities() { |
||
| 4002 | |||
| 4003 | /** |
||
| 4004 | * Returns true if the given method/parameter has a value |
||
| 4005 | * (Uses the DBField::hasValue if the parameter is a database field) |
||
| 4006 | * |
||
| 4007 | * @param string $field The field name |
||
| 4008 | * @param array $arguments |
||
| 4009 | * @param bool $cache |
||
| 4010 | * @return boolean |
||
| 4011 | */ |
||
| 4012 | public function hasValue($field, $arguments = null, $cache = true) { |
||
| 4020 | |||
| 4021 | } |
||
| 4022 |