Passed
Push — 4.0 ( 01b48e...d290ee )
by Damian
07:58
created

DataObject::getViewerTemplates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM;
4
5
use BadMethodCallException;
6
use Exception;
7
use InvalidArgumentException;
8
use LogicException;
9
use SilverStripe\Control\HTTP;
10
use SilverStripe\Core\ClassInfo;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Core\Resettable;
14
use SilverStripe\Dev\Debug;
15
use SilverStripe\Dev\Deprecation;
16
use SilverStripe\Forms\FieldList;
17
use SilverStripe\Forms\FormField;
18
use SilverStripe\Forms\FormScaffolder;
19
use SilverStripe\i18n\i18n;
20
use SilverStripe\i18n\i18nEntityProvider;
21
use SilverStripe\ORM\Connect\MySQLSchemaManager;
22
use SilverStripe\ORM\FieldType\DBClassName;
23
use SilverStripe\ORM\FieldType\DBComposite;
24
use SilverStripe\ORM\FieldType\DBDatetime;
25
use SilverStripe\ORM\FieldType\DBField;
26
use SilverStripe\ORM\Filters\SearchFilter;
27
use SilverStripe\ORM\Queries\SQLDelete;
28
use SilverStripe\ORM\Queries\SQLInsert;
29
use SilverStripe\ORM\Search\SearchContext;
30
use SilverStripe\Security\Member;
31
use SilverStripe\Security\Permission;
32
use SilverStripe\Security\Security;
33
use SilverStripe\View\SSViewer;
34
use SilverStripe\View\ViewableData;
35
use stdClass;
36
37
/**
38
 * A single database record & abstract class for the data-access-model.
39
 *
40
 * <h2>Extensions</h2>
41
 *
42
 * See {@link Extension} and {@link DataExtension}.
43
 *
44
 * <h2>Permission Control</h2>
45
 *
46
 * Object-level access control by {@link Permission}. Permission codes are arbitrary
47
 * strings which can be selected on a group-by-group basis.
48
 *
49
 * <code>
50
 * class Article extends DataObject implements PermissionProvider {
51
 *  static $api_access = true;
52
 *
53
 *  function canView($member = false) {
54
 *    return Permission::check('ARTICLE_VIEW');
55
 *  }
56
 *  function canEdit($member = false) {
57
 *    return Permission::check('ARTICLE_EDIT');
58
 *  }
59
 *  function canDelete() {
60
 *    return Permission::check('ARTICLE_DELETE');
61
 *  }
62
 *  function canCreate() {
63
 *    return Permission::check('ARTICLE_CREATE');
64
 *  }
65
 *  function providePermissions() {
66
 *    return array(
67
 *      'ARTICLE_VIEW' => 'Read an article object',
68
 *      'ARTICLE_EDIT' => 'Edit an article object',
69
 *      'ARTICLE_DELETE' => 'Delete an article object',
70
 *      'ARTICLE_CREATE' => 'Create an article object',
71
 *    );
72
 *  }
73
 * }
74
 * </code>
75
 *
76
 * Object-level access control by {@link Group} membership:
77
 * <code>
78
 * class Article extends DataObject {
79
 *   static $api_access = true;
80
 *
81
 *   function canView($member = false) {
82
 *     if(!$member) $member = Security::getCurrentUser();
83
 *     return $member->inGroup('Subscribers');
84
 *   }
85
 *   function canEdit($member = false) {
86
 *     if(!$member) $member = Security::getCurrentUser();
87
 *     return $member->inGroup('Editors');
88
 *   }
89
 *
90
 *   // ...
91
 * }
92
 * </code>
93
 *
94
 * If any public method on this class is prefixed with an underscore,
95
 * the results are cached in memory through {@link cachedCall()}.
96
 *
97
 *
98
 * @todo Add instance specific removeExtension() which undos loadExtraStatics()
99
 *  and defineMethods()
100
 *
101
 * @property int $ID ID of the DataObject, 0 if the DataObject doesn't exist in database.
102
 * @property int $OldID ID of object, if deleted
103
 * @property string $ClassName Class name of the DataObject
104
 * @property string $LastEdited Date and time of DataObject's last modification.
105
 * @property string $Created Date and time of DataObject creation.
106
 */
107
class DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider, Resettable
108
{
109
110
    /**
111
     * Human-readable singular name.
112
     * @var string
113
     * @config
114
     */
115
    private static $singular_name = null;
116
117
    /**
118
     * Human-readable plural name
119
     * @var string
120
     * @config
121
     */
122
    private static $plural_name = null;
123
124
    /**
125
     * Allow API access to this object?
126
     * @todo Define the options that can be set here
127
     * @config
128
     */
129
    private static $api_access = false;
130
131
    /**
132
     * Allows specification of a default value for the ClassName field.
133
     * Configure this value only in subclasses of DataObject.
134
     *
135
     * @config
136
     * @var string
137
     */
138
    private static $default_classname = null;
139
140
    /**
141
     * @deprecated 4.0..5.0
142
     * @var bool
143
     */
144
    public $destroyed = false;
145
146
    /**
147
     * Data stored in this objects database record. An array indexed by fieldname.
148
     *
149
     * Use {@link toMap()} if you want an array representation
150
     * of this object, as the $record array might contain lazy loaded field aliases.
151
     *
152
     * @var array
153
     */
154
    protected $record;
155
156
    /**
157
     * If selected through a many_many through relation, this is the instance of the through record
158
     *
159
     * @var DataObject
160
     */
161
    protected $joinRecord;
162
163
    /**
164
     * Represents a field that hasn't changed (before === after, thus before == after)
165
     */
166
    const CHANGE_NONE = 0;
167
168
    /**
169
     * Represents a field that has changed type, although not the loosely defined value.
170
     * (before !== after && before == after)
171
     * E.g. change 1 to true or "true" to true, but not true to 0.
172
     * Value changes are by nature also considered strict changes.
173
     */
174
    const CHANGE_STRICT = 1;
175
176
    /**
177
     * Represents a field that has changed the loosely defined value
178
     * (before != after, thus, before !== after))
179
     * E.g. change false to true, but not false to 0
180
     */
181
    const CHANGE_VALUE = 2;
182
183
    /**
184
     * An array indexed by fieldname, true if the field has been changed.
185
     * Use {@link getChangedFields()} and {@link isChanged()} to inspect
186
     * the changed state.
187
     *
188
     * @var array
189
     */
190
    private $changed;
191
192
    /**
193
     * The database record (in the same format as $record), before
194
     * any changes.
195
     * @var array
196
     */
197
    protected $original;
198
199
    /**
200
     * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete()
201
     * @var boolean
202
     */
203
    protected $brokenOnDelete = false;
204
205
    /**
206
     * Used by onBeforeWrite() to ensure child classes call parent::onBeforeWrite()
207
     * @var boolean
208
     */
209
    protected $brokenOnWrite = false;
210
211
    /**
212
     * @config
213
     * @var boolean Should dataobjects be validated before they are written?
214
     * Caution: Validation can contain safeguards against invalid/malicious data,
215
     * and check permission levels (e.g. on {@link Group}). Therefore it is recommended
216
     * to only disable validation for very specific use cases.
217
     */
218
    private static $validation_enabled = true;
219
220
    /**
221
     * Static caches used by relevant functions.
222
     *
223
     * @var array
224
     */
225
    protected static $_cache_get_one;
226
227
    /**
228
     * Cache of field labels
229
     *
230
     * @var array
231
     */
232
    protected static $_cache_field_labels = array();
233
234
    /**
235
     * Base fields which are not defined in static $db
236
     *
237
     * @config
238
     * @var array
239
     */
240
    private static $fixed_fields = array(
241
        'ID' => 'PrimaryKey',
242
        'ClassName' => 'DBClassName',
243
        'LastEdited' => 'DBDatetime',
244
        'Created' => 'DBDatetime',
245
    );
246
247
    /**
248
     * Override table name for this class. If ignored will default to FQN of class.
249
     * This option is not inheritable, and must be set on each class.
250
     * If left blank naming will default to the legacy (3.x) behaviour.
251
     *
252
     * @var string
253
     */
254
    private static $table_name = null;
255
256
    /**
257
     * Non-static relationship cache, indexed by component name.
258
     *
259
     * @var DataObject[]
260
     */
261
    protected $components;
262
263
    /**
264
     * Non-static cache of has_many and many_many relations that can't be written until this object is saved.
265
     *
266
     * @var UnsavedRelationList[]
267
     */
268
    protected $unsavedRelations;
269
270
    /**
271
     * List of relations that should be cascade deleted, similar to `owns`
272
     * Note: This will trigger delete on many_many objects, not only the mapping table.
273
     * For many_many through you can specify the components you want to delete separately
274
     * (many_many or has_many sub-component)
275
     *
276
     * @config
277
     * @var array
278
     */
279
    private static $cascade_deletes = [];
280
281
    /**
282
     * Get schema object
283
     *
284
     * @return DataObjectSchema
285
     */
286
    public static function getSchema()
287
    {
288
        return Injector::inst()->get(DataObjectSchema::class);
289
    }
290
291
    /**
292
     * Construct a new DataObject.
293
     *
294
295
     * @param array|null $record Used internally for rehydrating an object from database content.
296
     *                           Bypasses setters on this class, and hence should not be used
297
     *                           for populating data on new records.
298
     * @param boolean $isSingleton This this to true if this is a singleton() object, a stub for calling methods.
299
     *                             Singletons don't have their defaults set.
300
     * @param array $queryParams List of DataQuery params necessary to lazy load, or load related objects.
301
     */
302
    public function __construct($record = null, $isSingleton = false, $queryParams = array())
303
    {
304
        parent::__construct();
305
306
        // Set query params on the DataObject to tell the lazy loading mechanism the context the object creation context
307
        $this->setSourceQueryParams($queryParams);
308
309
        // Set the fields data.
310
        if (!$record) {
311
            $record = array(
312
                'ID' => 0,
313
                'ClassName' => static::class,
314
                'RecordClassName' => static::class
315
            );
316
        }
317
318
        if ($record instanceof stdClass) {
319
            $record = (array)$record;
320
        }
321
322
        if (!is_array($record)) {
323
            if (is_object($record)) {
324
                $passed = "an object of type '".get_class($record)."'";
325
            } else {
326
                $passed = "The value '$record'";
327
            }
328
329
            user_error(
330
                "DataObject::__construct passed $passed.  It's supposed to be passed an array,"
331
                . " taken straight from the database.  Perhaps you should use DataList::create()->First(); instead?",
332
                E_USER_WARNING
333
            );
334
            $record = null;
335
        }
336
337
        // Set $this->record to $record, but ignore NULLs
338
        $this->record = array();
339
        foreach ($record as $k => $v) {
340
            // Ensure that ID is stored as a number and not a string
341
            // To do: this kind of clean-up should be done on all numeric fields, in some relatively
342
            // performant manner
343
            if ($v !== null) {
344
                if ($k == 'ID' && is_numeric($v)) {
345
                    $this->record[$k] = (int)$v;
346
                } else {
347
                    $this->record[$k] = $v;
348
                }
349
            }
350
        }
351
352
        // Identify fields that should be lazy loaded, but only on existing records
353
        if (!empty($record['ID'])) {
354
            // Get all field specs scoped to class for later lazy loading
355
            $fields = static::getSchema()->fieldSpecs(
356
                static::class,
357
                DataObjectSchema::INCLUDE_CLASS | DataObjectSchema::DB_ONLY
358
            );
359
            foreach ($fields as $field => $fieldSpec) {
360
                $fieldClass = strtok($fieldSpec, ".");
361
                if (!array_key_exists($field, $record)) {
362
                    $this->record[$field.'_Lazy'] = $fieldClass;
363
                }
364
            }
365
        }
366
367
        $this->original = $this->record;
368
369
        // Keep track of the modification date of all the data sourced to make this page
370
        // From this we create a Last-Modified HTTP header
371
        if (isset($record['LastEdited'])) {
372
            HTTP::register_modification_date($record['LastEdited']);
373
        }
374
375
        // Must be called after parent constructor
376
        if (!$isSingleton && (!isset($this->record['ID']) || !$this->record['ID'])) {
377
            $this->populateDefaults();
378
        }
379
380
        // prevent populateDefaults() and setField() from marking overwritten defaults as changed
381
        $this->changed = array();
382
    }
383
384
    /**
385
     * Destroy all of this objects dependant objects and local caches.
386
     * You'll need to call this to get the memory of an object that has components or extensions freed.
387
     */
388
    public function destroy()
389
    {
390
        $this->flushCache(false);
391
    }
392
393
    /**
394
     * Create a duplicate of this node. Can duplicate many_many relations
395
     *
396
     * @param bool $doWrite Perform a write() operation before returning the object.
397
     * If this is true, it will create the duplicate in the database.
398
     * @param bool|string $manyMany Which many-many to duplicate. Set to true to duplicate all, false to duplicate none.
399
     * Alternatively set to the string of the relation config to duplicate
400
     * (supports 'many_many', or 'belongs_many_many')
401
     * @return static A duplicate of this node. The exact type will be the type of this node.
402
     */
403
    public function duplicate($doWrite = true, $manyMany = 'many_many')
404
    {
405
        $map = $this->toMap();
406
        unset($map['Created']);
407
        /** @var static $clone */
408
        $clone = Injector::inst()->create(static::class, $map, false, $this->getSourceQueryParams());
409
        $clone->ID = 0;
410
411
        $clone->invokeWithExtensions('onBeforeDuplicate', $this, $doWrite, $manyMany);
412
        if ($manyMany) {
413
            $this->duplicateManyManyRelations($this, $clone, $manyMany);
414
        }
415
        if ($doWrite) {
416
            $clone->write();
417
        }
418
        $clone->invokeWithExtensions('onAfterDuplicate', $this, $doWrite, $manyMany);
419
420
        return $clone;
421
    }
422
423
    /**
424
     * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object.
425
     *
426
     * @param DataObject $sourceObject the source object to duplicate from
427
     * @param DataObject $destinationObject the destination object to populate with the duplicated relations
428
     * @param bool|string $filter
429
     */
430
    protected function duplicateManyManyRelations($sourceObject, $destinationObject, $filter)
431
    {
432
        // Get list of relations to duplicate
433
        if ($filter === 'many_many' || $filter === 'belongs_many_many') {
434
            $relations = $sourceObject->config()->get($filter);
435
        } elseif ($filter === true) {
436
            $relations = $sourceObject->manyMany();
437
        } else {
438
            throw new InvalidArgumentException("Invalid many_many duplication filter");
439
        }
440
        foreach ($relations as $manyManyName => $type) {
441
            $this->duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName);
442
        }
443
    }
444
445
    /**
446
     * Duplicates a single many_many relation from one object to another
447
     *
448
     * @param DataObject $sourceObject
449
     * @param DataObject $destinationObject
450
     * @param string $manyManyName
451
     */
452
    protected function duplicateManyManyRelation($sourceObject, $destinationObject, $manyManyName)
453
    {
454
        // Ensure this component exists on the destination side as well
455
        if (!static::getSchema()->manyManyComponent(get_class($destinationObject), $manyManyName)) {
456
            return;
457
        }
458
459
        // Copy all components from source to destination
460
        $source = $sourceObject->getManyManyComponents($manyManyName);
461
        $dest = $destinationObject->getManyManyComponents($manyManyName);
462
        foreach ($source as $item) {
463
            $dest->add($item);
464
        }
465
    }
466
467
    /**
468
     * Return obsolete class name, if this is no longer a valid class
469
     *
470
     * @return string
471
     */
472
    public function getObsoleteClassName()
473
    {
474
        $className = $this->getField("ClassName");
475
        if (!ClassInfo::exists($className)) {
0 ignored issues
show
Bug introduced by
It seems like $className can also be of type object; however, parameter $class of SilverStripe\Core\ClassInfo::exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

475
        if (!ClassInfo::exists(/** @scrutinizer ignore-type */ $className)) {
Loading history...
476
            return $className;
477
        }
478
        return null;
479
    }
480
481
    /**
482
     * Gets name of this class
483
     *
484
     * @return string
485
     */
486
    public function getClassName()
487
    {
488
        $className = $this->getField("ClassName");
489
        if (!ClassInfo::exists($className)) {
0 ignored issues
show
Bug introduced by
It seems like $className can also be of type object; however, parameter $class of SilverStripe\Core\ClassInfo::exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

489
        if (!ClassInfo::exists(/** @scrutinizer ignore-type */ $className)) {
Loading history...
490
            return static::class;
491
        }
492
        return $className;
493
    }
494
495
    /**
496
     * Set the ClassName attribute. {@link $class} is also updated.
497
     * Warning: This will produce an inconsistent record, as the object
498
     * instance will not automatically switch to the new subclass.
499
     * Please use {@link newClassInstance()} for this purpose,
500
     * or destroy and reinstanciate the record.
501
     *
502
     * @param string $className The new ClassName attribute (a subclass of {@link DataObject})
503
     * @return $this
504
     */
505
    public function setClassName($className)
506
    {
507
        $className = trim($className);
508
        if (!$className || !is_subclass_of($className, self::class)) {
509
            return $this;
510
        }
511
512
        $this->setField("ClassName", $className);
513
        $this->setField('RecordClassName', $className);
514
        return $this;
515
    }
516
517
    /**
518
     * Create a new instance of a different class from this object's record.
519
     * This is useful when dynamically changing the type of an instance. Specifically,
520
     * it ensures that the instance of the class is a match for the className of the
521
     * record. Don't set the {@link DataObject->class} or {@link DataObject->ClassName}
522
     * property manually before calling this method, as it will confuse change detection.
523
     *
524
     * If the new class is different to the original class, defaults are populated again
525
     * because this will only occur automatically on instantiation of a DataObject if
526
     * there is no record, or the record has no ID. In this case, we do have an ID but
527
     * we still need to repopulate the defaults.
528
     *
529
     * @param string $newClassName The name of the new class
530
     *
531
     * @return DataObject The new instance of the new class, The exact type will be of the class name provided.
532
     */
533
    public function newClassInstance($newClassName)
534
    {
535
        if (!is_subclass_of($newClassName, self::class)) {
536
            throw new InvalidArgumentException("$newClassName is not a valid subclass of DataObject");
537
        }
538
539
        $originalClass = $this->ClassName;
540
541
        /** @var DataObject $newInstance */
542
        $newInstance = Injector::inst()->create($newClassName, $this->record, false);
543
544
        // Modify ClassName
545
        if ($newClassName != $originalClass) {
546
            $newInstance->setClassName($newClassName);
547
            $newInstance->populateDefaults();
548
            $newInstance->forceChange();
549
        }
550
551
        return $newInstance;
552
    }
553
554
    /**
555
     * Adds methods from the extensions.
556
     * Called by Object::__construct() once per class.
557
     */
558
    public function defineMethods()
559
    {
560
        parent::defineMethods();
561
562
        if (static::class === self::class) {
563
             return;
564
        }
565
566
        // Set up accessors for joined items
567
        if ($manyMany = $this->manyMany()) {
568
            foreach ($manyMany as $relationship => $class) {
569
                $this->addWrapperMethod($relationship, 'getManyManyComponents');
570
            }
571
        }
572
        if ($hasMany = $this->hasMany()) {
573
            foreach ($hasMany as $relationship => $class) {
574
                $this->addWrapperMethod($relationship, 'getComponents');
575
            }
576
        }
577
        if ($hasOne = $this->hasOne()) {
578
            foreach ($hasOne as $relationship => $class) {
579
                $this->addWrapperMethod($relationship, 'getComponent');
580
            }
581
        }
582
        if ($belongsTo = $this->belongsTo()) {
583
            foreach (array_keys($belongsTo) as $relationship) {
584
                $this->addWrapperMethod($relationship, 'getComponent');
585
            }
586
        }
587
    }
588
589
    /**
590
     * Returns true if this object "exists", i.e., has a sensible value.
591
     * The default behaviour for a DataObject is to return true if
592
     * the object exists in the database, you can override this in subclasses.
593
     *
594
     * @return boolean true if this object exists
595
     */
596
    public function exists()
597
    {
598
        return (isset($this->record['ID']) && $this->record['ID'] > 0);
599
    }
600
601
    /**
602
     * Returns TRUE if all values (other than "ID") are
603
     * considered empty (by weak boolean comparison).
604
     *
605
     * @return boolean
606
     */
607
    public function isEmpty()
608
    {
609
        $fixed = DataObject::config()->uninherited('fixed_fields');
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
610
        foreach ($this->toMap() as $field => $value) {
611
            // only look at custom fields
612
            if (isset($fixed[$field])) {
613
                continue;
614
            }
615
616
            $dbObject = $this->dbObject($field);
617
            if (!$dbObject) {
618
                continue;
619
            }
620
            if ($dbObject->exists()) {
621
                return false;
622
            }
623
        }
624
        return true;
625
    }
626
627
    /**
628
     * Pluralise this item given a specific count.
629
     *
630
     * E.g. "0 Pages", "1 File", "3 Images"
631
     *
632
     * @param string $count
633
     * @return string
634
     */
635
    public function i18n_pluralise($count)
636
    {
637
        $default = 'one ' . $this->i18n_singular_name() . '|{count} ' . $this->i18n_plural_name();
638
        return i18n::_t(
639
            static::class.'.PLURALS',
640
            $default,
641
            [ 'count' => $count ]
642
        );
643
    }
644
645
    /**
646
     * Get the user friendly singular name of this DataObject.
647
     * If the name is not defined (by redefining $singular_name in the subclass),
648
     * this returns the class name.
649
     *
650
     * @return string User friendly singular name of this DataObject
651
     */
652
    public function singular_name()
653
    {
654
        $name = $this->config()->get('singular_name');
655
        if ($name) {
656
            return $name;
657
        }
658
        return ucwords(trim(strtolower(preg_replace(
659
            '/_?([A-Z])/',
660
            ' $1',
661
            ClassInfo::shortName($this)
662
        ))));
663
    }
664
665
    /**
666
     * Get the translated user friendly singular name of this DataObject
667
     * same as singular_name() but runs it through the translating function
668
     *
669
     * Translating string is in the form:
670
     *     $this->class.SINGULARNAME
671
     * Example:
672
     *     Page.SINGULARNAME
673
     *
674
     * @return string User friendly translated singular name of this DataObject
675
     */
676
    public function i18n_singular_name()
677
    {
678
        return _t(static::class.'.SINGULARNAME', $this->singular_name());
679
    }
680
681
    /**
682
     * Get the user friendly plural name of this DataObject
683
     * If the name is not defined (by renaming $plural_name in the subclass),
684
     * this returns a pluralised version of the class name.
685
     *
686
     * @return string User friendly plural name of this DataObject
687
     */
688
    public function plural_name()
689
    {
690
        if ($name = $this->config()->get('plural_name')) {
691
            return $name;
692
        }
693
        $name = $this->singular_name();
694
        //if the penultimate character is not a vowel, replace "y" with "ies"
695
        if (preg_match('/[^aeiou]y$/i', $name)) {
696
            $name = substr($name, 0, -1) . 'ie';
0 ignored issues
show
Bug introduced by
Are you sure substr($name, 0, -1) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

696
            $name = /** @scrutinizer ignore-type */ substr($name, 0, -1) . 'ie';
Loading history...
697
        }
698
        return ucfirst($name . 's');
699
    }
700
701
    /**
702
     * Get the translated user friendly plural name of this DataObject
703
     * Same as plural_name but runs it through the translation function
704
     * Translation string is in the form:
705
     *      $this->class.PLURALNAME
706
     * Example:
707
     *      Page.PLURALNAME
708
     *
709
     * @return string User friendly translated plural name of this DataObject
710
     */
711
    public function i18n_plural_name()
712
    {
713
        return _t(static::class.'.PLURALNAME', $this->plural_name());
714
    }
715
716
    /**
717
     * Standard implementation of a title/label for a specific
718
     * record. Tries to find properties 'Title' or 'Name',
719
     * and falls back to the 'ID'. Useful to provide
720
     * user-friendly identification of a record, e.g. in errormessages
721
     * or UI-selections.
722
     *
723
     * Overload this method to have a more specialized implementation,
724
     * e.g. for an Address record this could be:
725
     * <code>
726
     * function getTitle() {
727
     *   return "{$this->StreetNumber} {$this->StreetName} {$this->City}";
728
     * }
729
     * </code>
730
     *
731
     * @return string
732
     */
733
    public function getTitle()
734
    {
735
        $schema = static::getSchema();
736
        if ($schema->fieldSpec($this, 'Title')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $schema->fieldSpec($this, 'Title') of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
737
            return $this->getField('Title');
738
        }
739
        if ($schema->fieldSpec($this, 'Name')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $schema->fieldSpec($this, 'Name') of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
740
            return $this->getField('Name');
741
        }
742
743
        return "#{$this->ID}";
744
    }
745
746
    /**
747
     * Returns the associated database record - in this case, the object itself.
748
     * This is included so that you can call $dataOrController->data() and get a DataObject all the time.
749
     *
750
     * @return DataObject Associated database record
751
     */
752
    public function data()
753
    {
754
        return $this;
755
    }
756
757
    /**
758
     * Convert this object to a map.
759
     *
760
     * @return array The data as a map.
761
     */
762
    public function toMap()
763
    {
764
        $this->loadLazyFields();
765
        return $this->record;
766
    }
767
768
    /**
769
     * Return all currently fetched database fields.
770
     *
771
     * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields.
772
     * Obviously, this makes it a lot faster.
773
     *
774
     * @return array The data as a map.
775
     */
776
    public function getQueriedDatabaseFields()
777
    {
778
        return $this->record;
779
    }
780
781
    /**
782
     * Update a number of fields on this object, given a map of the desired changes.
783
     *
784
     * The field names can be simple names, or you can use a dot syntax to access $has_one relations.
785
     * For example, array("Author.FirstName" => "Jim") will set $this->Author()->FirstName to "Jim".
786
     *
787
     * update() doesn't write the main object, but if you use the dot syntax, it will write()
788
     * the related objects that it alters.
789
     *
790
     * @param array $data A map of field name to data values to update.
791
     * @return DataObject $this
792
     */
793
    public function update($data)
794
    {
795
        foreach ($data as $key => $value) {
796
            // Implement dot syntax for updates
797
            if (strpos($key, '.') !== false) {
798
                $relations = explode('.', $key);
799
                $fieldName = array_pop($relations);
800
                /** @var static $relObj */
801
                $relObj = $this;
802
                $relation = null;
803
                foreach ($relations as $i => $relation) {
804
                    // no support for has_many or many_many relationships,
805
                    // as the updater wouldn't know which object to write to (or create)
806
                    if ($relObj->$relation() instanceof DataObject) {
807
                        $parentObj = $relObj;
808
                        $relObj = $relObj->$relation();
809
                        // If the intermediate relationship objects haven't been created, then write them
810
                        if ($i<sizeof($relations)-1 && !$relObj->ID || (!$relObj->ID && $parentObj !== $this)) {
0 ignored issues
show
Bug introduced by
The call to sizeof() has too few arguments starting with mode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

810
                        if ($i</** @scrutinizer ignore-call */ sizeof($relations)-1 && !$relObj->ID || (!$relObj->ID && $parentObj !== $this)) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
811
                            $relObj->write();
812
                            $relatedFieldName = $relation."ID";
813
                            $parentObj->$relatedFieldName = $relObj->ID;
814
                            $parentObj->write();
815
                        }
816
                    } else {
817
                        user_error(
818
                            "DataObject::update(): Can't traverse relationship '$relation'," .
819
                            "it has to be a has_one relationship or return a single DataObject",
820
                            E_USER_NOTICE
821
                        );
822
                        // unset relation object so we don't write properties to the wrong object
823
                        $relObj = null;
824
                        break;
825
                    }
826
                }
827
828
                if ($relObj) {
829
                    $relObj->$fieldName = $value;
830
                    $relObj->write();
831
                    $relatedFieldName = $relation."ID";
832
                    $this->$relatedFieldName = $relObj->ID;
833
                    $relObj->flushCache();
834
                } else {
835
                    $class = static::class;
836
                    user_error("Couldn't follow dot syntax '{$key}' on '{$class}' object", E_USER_WARNING);
837
                }
838
            } else {
839
                $this->$key = $value;
840
            }
841
        }
842
        return $this;
843
    }
844
845
    /**
846
     * Pass changes as a map, and try to
847
     * get automatic casting for these fields.
848
     * Doesn't write to the database. To write the data,
849
     * use the write() method.
850
     *
851
     * @param array $data A map of field name to data values to update.
852
     * @return DataObject $this
853
     */
854
    public function castedUpdate($data)
855
    {
856
        foreach ($data as $k => $v) {
857
            $this->setCastedField($k, $v);
858
        }
859
        return $this;
860
    }
861
862
    /**
863
     * Merges data and relations from another object of same class,
864
     * without conflict resolution. Allows to specify which
865
     * dataset takes priority in case its not empty.
866
     * has_one-relations are just transferred with priority 'right'.
867
     * has_many and many_many-relations are added regardless of priority.
868
     *
869
     * Caution: has_many/many_many relations are moved rather than duplicated,
870
     * meaning they are not connected to the merged object any longer.
871
     * Caution: Just saves updated has_many/many_many relations to the database,
872
     * doesn't write the updated object itself (just writes the object-properties).
873
     * Caution: Does not delete the merged object.
874
     * Caution: Does now overwrite Created date on the original object.
875
     *
876
     * @param DataObject $rightObj
877
     * @param string $priority left|right Determines who wins in case of a conflict (optional)
878
     * @param bool $includeRelations Merge any existing relations (optional)
879
     * @param bool $overwriteWithEmpty Overwrite existing left values with empty right values.
880
     *                            Only applicable with $priority='right'. (optional)
881
     * @return Boolean
882
     */
883
    public function merge($rightObj, $priority = 'right', $includeRelations = true, $overwriteWithEmpty = false)
884
    {
885
        $leftObj = $this;
886
887
        if ($leftObj->ClassName != $rightObj->ClassName) {
888
            // we can't merge similiar subclasses because they might have additional relations
889
            user_error("DataObject->merge(): Invalid object class '{$rightObj->ClassName}'
890
			(expected '{$leftObj->ClassName}').", E_USER_WARNING);
891
            return false;
892
        }
893
894
        if (!$rightObj->ID) {
895
            user_error("DataObject->merge(): Please write your merged-in object to the database before merging,
896
				to make sure all relations are transferred properly.').", E_USER_WARNING);
897
            return false;
898
        }
899
900
        // makes sure we don't merge data like ID or ClassName
901
        $rightData = DataObject::getSchema()->fieldSpecs(get_class($rightObj));
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
902
        foreach ($rightData as $key => $rightSpec) {
903
            // Don't merge ID
904
            if ($key === 'ID') {
905
                continue;
906
            }
907
908
            // Only merge relations if allowed
909
            if ($rightSpec === 'ForeignKey' && !$includeRelations) {
910
                continue;
911
            }
912
913
            // don't merge conflicting values if priority is 'left'
914
            if ($priority == 'left' && $leftObj->{$key} !== $rightObj->{$key}) {
915
                continue;
916
            }
917
918
            // don't overwrite existing left values with empty right values (if $overwriteWithEmpty is set)
919
            if ($priority == 'right' && !$overwriteWithEmpty && empty($rightObj->{$key})) {
920
                continue;
921
            }
922
923
            // TODO remove redundant merge of has_one fields
924
            $leftObj->{$key} = $rightObj->{$key};
925
        }
926
927
        // merge relations
928
        if ($includeRelations) {
929
            if ($manyMany = $this->manyMany()) {
930
                foreach ($manyMany as $relationship => $class) {
931
                    /** @var DataObject $leftComponents */
932
                    $leftComponents = $leftObj->getManyManyComponents($relationship);
933
                    $rightComponents = $rightObj->getManyManyComponents($relationship);
934
                    if ($rightComponents && $rightComponents->exists()) {
935
                        $leftComponents->addMany($rightComponents->column('ID'));
936
                    }
937
                    $leftComponents->write();
938
                }
939
            }
940
941
            if ($hasMany = $this->hasMany()) {
942
                foreach ($hasMany as $relationship => $class) {
943
                    $leftComponents = $leftObj->getComponents($relationship);
944
                    $rightComponents = $rightObj->getComponents($relationship);
945
                    if ($rightComponents && $rightComponents->exists()) {
946
                        $leftComponents->addMany($rightComponents->column('ID'));
947
                    }
948
                    $leftComponents->write();
0 ignored issues
show
Bug introduced by
The method write() does not exist on SilverStripe\ORM\UnsavedRelationList. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

948
                    $leftComponents->/** @scrutinizer ignore-call */ 
949
                                     write();
Loading history...
949
                }
950
            }
951
        }
952
953
        return true;
954
    }
955
956
    /**
957
     * Forces the record to think that all its data has changed.
958
     * Doesn't write to the database. Only sets fields as changed
959
     * if they are not already marked as changed.
960
     *
961
     * @return $this
962
     */
963
    public function forceChange()
964
    {
965
        // Ensure lazy fields loaded
966
        $this->loadLazyFields();
967
        $fields = static::getSchema()->fieldSpecs(static::class);
968
969
        // $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well
970
        $fieldNames = array_unique(array_merge(
971
            array_keys($this->record),
972
            array_keys($fields)
973
        ));
974
975
        foreach ($fieldNames as $fieldName) {
976
            if (!isset($this->changed[$fieldName])) {
977
                $this->changed[$fieldName] = self::CHANGE_STRICT;
978
            }
979
            // Populate the null values in record so that they actually get written
980
            if (!isset($this->record[$fieldName])) {
981
                $this->record[$fieldName] = null;
982
            }
983
        }
984
985
        // @todo Find better way to allow versioned to write a new version after forceChange
986
        if ($this->isChanged('Version')) {
987
            unset($this->changed['Version']);
988
        }
989
        return $this;
990
    }
991
992
    /**
993
     * Validate the current object.
994
     *
995
     * By default, there is no validation - objects are always valid!  However, you can overload this method in your
996
     * DataObject sub-classes to specify custom validation, or use the hook through DataExtension.
997
     *
998
     * Invalid objects won't be able to be written - a warning will be thrown and no write will occur.  onBeforeWrite()
999
     * and onAfterWrite() won't get called either.
1000
     *
1001
     * It is expected that you call validate() in your own application to test that an object is valid before
1002
     * attempting a write, and respond appropriately if it isn't.
1003
     *
1004
     * @see {@link ValidationResult}
1005
     * @return ValidationResult
1006
     */
1007
    public function validate()
1008
    {
1009
        $result = ValidationResult::create();
1010
        $this->extend('validate', $result);
1011
        return $result;
1012
    }
1013
1014
    /**
1015
     * Public accessor for {@see DataObject::validate()}
1016
     *
1017
     * @return ValidationResult
1018
     */
1019
    public function doValidate()
1020
    {
1021
        Deprecation::notice('5.0', 'Use validate');
1022
        return $this->validate();
1023
    }
1024
1025
    /**
1026
     * Event handler called before writing to the database.
1027
     * You can overload this to clean up or otherwise process data before writing it to the
1028
     * database.  Don't forget to call parent::onBeforeWrite(), though!
1029
     *
1030
     * This called after {@link $this->validate()}, so you can be sure that your data is valid.
1031
     *
1032
     * @uses DataExtension->onBeforeWrite()
1033
     */
1034
    protected function onBeforeWrite()
1035
    {
1036
        $this->brokenOnWrite = false;
1037
1038
        $dummy = null;
1039
        $this->extend('onBeforeWrite', $dummy);
1040
    }
1041
1042
    /**
1043
     * Event handler called after writing to the database.
1044
     * You can overload this to act upon changes made to the data after it is written.
1045
     * $this->changed will have a record
1046
     * database.  Don't forget to call parent::onAfterWrite(), though!
1047
     *
1048
     * @uses DataExtension->onAfterWrite()
1049
     */
1050
    protected function onAfterWrite()
1051
    {
1052
        $dummy = null;
1053
        $this->extend('onAfterWrite', $dummy);
1054
    }
1055
1056
    /**
1057
     * Find all objects that will be cascade deleted if this object is deleted
1058
     *
1059
     * Notes:
1060
     *   - If this object is versioned, objects will only be searched in the same stage as the given record.
1061
     *   - This will only be useful prior to deletion, as post-deletion this record will no longer exist.
1062
     *
1063
     * @param bool $recursive True if recursive
1064
     * @param ArrayList $list Optional list to add items to
1065
     * @return ArrayList list of objects
1066
     */
1067
    public function findCascadeDeletes($recursive = true, $list = null)
1068
    {
1069
        // Find objects in these relationships
1070
        return $this->findRelatedObjects('cascade_deletes', $recursive, $list);
1071
    }
1072
1073
    /**
1074
     * Event handler called before deleting from the database.
1075
     * You can overload this to clean up or otherwise process data before delete this
1076
     * record.  Don't forget to call parent::onBeforeDelete(), though!
1077
     *
1078
     * @uses DataExtension->onBeforeDelete()
1079
     */
1080
    protected function onBeforeDelete()
1081
    {
1082
        $this->brokenOnDelete = false;
1083
1084
        $dummy = null;
1085
        $this->extend('onBeforeDelete', $dummy);
1086
1087
        // Cascade deletes
1088
        $deletes = $this->findCascadeDeletes(false);
1089
        foreach ($deletes as $delete) {
1090
            $delete->delete();
1091
        }
1092
    }
1093
1094
    protected function onAfterDelete()
1095
    {
1096
        $this->extend('onAfterDelete');
1097
    }
1098
1099
    /**
1100
     * Load the default values in from the self::$defaults array.
1101
     * Will traverse the defaults of the current class and all its parent classes.
1102
     * Called by the constructor when creating new records.
1103
     *
1104
     * @uses DataExtension->populateDefaults()
1105
     * @return DataObject $this
1106
     */
1107
    public function populateDefaults()
1108
    {
1109
        $classes = array_reverse(ClassInfo::ancestry($this));
1110
1111
        foreach ($classes as $class) {
1112
            $defaults = Config::inst()->get($class, 'defaults', Config::UNINHERITED);
1113
1114
            if ($defaults && !is_array($defaults)) {
1115
                user_error(
1116
                    "Bad '" . static::class . "' defaults given: " . var_export($defaults, true),
1117
                    E_USER_WARNING
1118
                );
1119
                $defaults = null;
1120
            }
1121
1122
            if ($defaults) {
1123
                foreach ($defaults as $fieldName => $fieldValue) {
1124
                // SRM 2007-03-06: Stricter check
1125
                    if (!isset($this->$fieldName) || $this->$fieldName === null) {
1126
                        $this->$fieldName = $fieldValue;
1127
                    }
1128
                // Set many-many defaults with an array of ids
1129
                    if (is_array($fieldValue) && $this->getSchema()->manyManyComponent(static::class, $fieldName)) {
1130
                        /** @var ManyManyList $manyManyJoin */
1131
                        $manyManyJoin = $this->$fieldName();
1132
                        $manyManyJoin->setByIDList($fieldValue);
1133
                    }
1134
                }
1135
            }
1136
            if ($class == self::class) {
1137
                break;
1138
            }
1139
        }
1140
1141
        $this->extend('populateDefaults');
1142
        return $this;
1143
    }
1144
1145
    /**
1146
     * Determine validation of this object prior to write
1147
     *
1148
     * @return ValidationException Exception generated by this write, or null if valid
1149
     */
1150
    protected function validateWrite()
1151
    {
1152
        if ($this->ObsoleteClassName) {
0 ignored issues
show
Bug Best Practice introduced by
The property ObsoleteClassName does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
1153
            return new ValidationException(
1154
                "Object is of class '{$this->ObsoleteClassName}' which doesn't exist - ".
1155
                "you need to change the ClassName before you can write it"
1156
            );
1157
        }
1158
1159
        // Note: Validation can only be disabled at the global level, not per-model
1160
        if (DataObject::config()->uninherited('validation_enabled')) {
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1161
            $result = $this->validate();
1162
            if (!$result->isValid()) {
1163
                return new ValidationException($result);
1164
            }
1165
        }
1166
        return null;
1167
    }
1168
1169
    /**
1170
     * Prepare an object prior to write
1171
     *
1172
     * @throws ValidationException
1173
     */
1174
    protected function preWrite()
1175
    {
1176
        // Validate this object
1177
        if ($writeException = $this->validateWrite()) {
1178
            // Used by DODs to clean up after themselves, eg, Versioned
1179
            $this->invokeWithExtensions('onAfterSkippedWrite');
1180
            throw $writeException;
1181
        }
1182
1183
        // Check onBeforeWrite
1184
        $this->brokenOnWrite = true;
1185
        $this->onBeforeWrite();
1186
        if ($this->brokenOnWrite) {
1187
            user_error(static::class . " has a broken onBeforeWrite() function."
1188
                . " Make sure that you call parent::onBeforeWrite().", E_USER_ERROR);
1189
        }
1190
    }
1191
1192
    /**
1193
     * Detects and updates all changes made to this object
1194
     *
1195
     * @param bool $forceChanges If set to true, force all fields to be treated as changed
1196
     * @return bool True if any changes are detected
1197
     */
1198
    protected function updateChanges($forceChanges = false)
1199
    {
1200
        if ($forceChanges) {
1201
            // Force changes, but only for loaded fields
1202
            foreach ($this->record as $field => $value) {
1203
                $this->changed[$field] = static::CHANGE_VALUE;
1204
            }
1205
            return true;
1206
        }
1207
        return $this->isChanged();
1208
    }
1209
1210
    /**
1211
     * Writes a subset of changes for a specific table to the given manipulation
1212
     *
1213
     * @param string $baseTable Base table
1214
     * @param string $now Timestamp to use for the current time
1215
     * @param bool $isNewRecord Whether this should be treated as a new record write
1216
     * @param array $manipulation Manipulation to write to
1217
     * @param string $class Class of table to manipulate
1218
     */
1219
    protected function prepareManipulationTable($baseTable, $now, $isNewRecord, &$manipulation, $class)
1220
    {
1221
        $schema = $this->getSchema();
1222
        $table = $schema->tableName($class);
1223
        $manipulation[$table] = array();
1224
1225
        // Extract records for this table
1226
        foreach ($this->record as $fieldName => $fieldValue) {
1227
            // we're not attempting to reset the BaseTable->ID
1228
            // Ignore unchanged fields or attempts to reset the BaseTable->ID
1229
            if (empty($this->changed[$fieldName]) || ($table === $baseTable && $fieldName === 'ID')) {
1230
                continue;
1231
            }
1232
1233
            // Ensure this field pertains to this table
1234
            $specification = $schema->fieldSpec($class, $fieldName, DataObjectSchema::DB_ONLY | DataObjectSchema::UNINHERITED);
1235
            if (!$specification) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $specification of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1236
                continue;
1237
            }
1238
1239
            // if database column doesn't correlate to a DBField instance...
1240
            $fieldObj = $this->dbObject($fieldName);
1241
            if (!$fieldObj) {
1242
                $fieldObj = DBField::create_field('Varchar', $fieldValue, $fieldName);
1243
            }
1244
1245
            // Write to manipulation
1246
            $fieldObj->writeToManipulation($manipulation[$table]);
1247
        }
1248
1249
        // Ensure update of Created and LastEdited columns
1250
        if ($baseTable === $table) {
1251
            $manipulation[$table]['fields']['LastEdited'] = $now;
1252
            if ($isNewRecord) {
1253
                $manipulation[$table]['fields']['Created']
1254
                    = empty($this->record['Created'])
1255
                        ? $now
1256
                        : $this->record['Created'];
1257
                $manipulation[$table]['fields']['ClassName'] = static::class;
1258
            }
1259
        }
1260
1261
        // Inserts done one the base table are performed in another step, so the manipulation should instead
1262
        // attempt an update, as though it were a normal update.
1263
        $manipulation[$table]['command'] = $isNewRecord ? 'insert' : 'update';
1264
        $manipulation[$table]['id'] = $this->record['ID'];
1265
        $manipulation[$table]['class'] = $class;
1266
    }
1267
1268
    /**
1269
     * Ensures that a blank base record exists with the basic fixed fields for this dataobject
1270
     *
1271
     * Does nothing if an ID is already assigned for this record
1272
     *
1273
     * @param string $baseTable Base table
1274
     * @param string $now Timestamp to use for the current time
1275
     */
1276
    protected function writeBaseRecord($baseTable, $now)
1277
    {
1278
        // Generate new ID if not specified
1279
        if ($this->isInDB()) {
1280
            return;
1281
        }
1282
1283
        // Perform an insert on the base table
1284
        $insert = new SQLInsert('"'.$baseTable.'"');
1285
        $insert
1286
            ->assign('"Created"', $now)
1287
            ->execute();
1288
        $this->changed['ID'] = self::CHANGE_VALUE;
1289
        $this->record['ID'] = DB::get_generated_id($baseTable);
1290
    }
1291
1292
    /**
1293
     * Generate and write the database manipulation for all changed fields
1294
     *
1295
     * @param string $baseTable Base table
1296
     * @param string $now Timestamp to use for the current time
1297
     * @param bool $isNewRecord If this is a new record
1298
     */
1299
    protected function writeManipulation($baseTable, $now, $isNewRecord)
1300
    {
1301
        // Generate database manipulations for each class
1302
        $manipulation = array();
1303
        foreach (ClassInfo::ancestry(static::class, true) as $class) {
1304
            $this->prepareManipulationTable($baseTable, $now, $isNewRecord, $manipulation, $class);
1305
        }
1306
1307
        // Allow extensions to extend this manipulation
1308
        $this->extend('augmentWrite', $manipulation);
1309
1310
        // New records have their insert into the base data table done first, so that they can pass the
1311
        // generated ID on to the rest of the manipulation
1312
        if ($isNewRecord) {
1313
            $manipulation[$baseTable]['command'] = 'update';
1314
        }
1315
1316
        // Perform the manipulation
1317
        DB::manipulate($manipulation);
1318
    }
1319
1320
    /**
1321
     * Writes all changes to this object to the database.
1322
     *  - It will insert a record whenever ID isn't set, otherwise update.
1323
     *  - All relevant tables will be updated.
1324
     *  - $this->onBeforeWrite() gets called beforehand.
1325
     *  - Extensions such as Versioned will ammend the database-write to ensure that a version is saved.
1326
     *
1327
     *  @uses DataExtension->augmentWrite()
1328
     *
1329
     * @param boolean $showDebug Show debugging information
1330
     * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists
1331
     * @param boolean $forceWrite Write to database even if there are no changes
1332
     * @param boolean $writeComponents Call write() on all associated component instances which were previously
1333
     *                                 retrieved through {@link getComponent()}, {@link getComponents()} or
1334
     *                                 {@link getManyManyComponents()} (Default: false)
1335
     * @return int The ID of the record
1336
     * @throws ValidationException Exception that can be caught and handled by the calling function
1337
     */
1338
    public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false)
1339
    {
1340
        $now = DBDatetime::now()->Rfc2822();
1341
1342
        // Execute pre-write tasks
1343
        $this->preWrite();
1344
1345
        // Check if we are doing an update or an insert
1346
        $isNewRecord = !$this->isInDB() || $forceInsert;
1347
1348
        // Check changes exist, abort if there are none
1349
        $hasChanges = $this->updateChanges($isNewRecord);
1350
        if ($hasChanges || $forceWrite || $isNewRecord) {
1351
            // Ensure Created and LastEdited are populated
1352
            if (!isset($this->record['Created'])) {
1353
                $this->record['Created'] = $now;
1354
            }
1355
            $this->record['LastEdited'] = $now;
1356
1357
            // New records have their insert into the base data table done first, so that they can pass the
1358
            // generated primary key on to the rest of the manipulation
1359
            $baseTable = $this->baseTable();
1360
            $this->writeBaseRecord($baseTable, $now);
1361
1362
            // Write the DB manipulation for all changed fields
1363
            $this->writeManipulation($baseTable, $now, $isNewRecord);
1364
1365
            // If there's any relations that couldn't be saved before, save them now (we have an ID here)
1366
            $this->writeRelations();
1367
            $this->onAfterWrite();
1368
            $this->changed = array();
1369
        } else {
1370
            if ($showDebug) {
1371
                Debug::message("no changes for DataObject");
1372
            }
1373
1374
            // Used by DODs to clean up after themselves, eg, Versioned
1375
            $this->invokeWithExtensions('onAfterSkippedWrite');
1376
        }
1377
1378
        // Write relations as necessary
1379
        if ($writeComponents) {
1380
            $this->writeComponents(true);
1381
        }
1382
1383
        // Clears the cache for this object so get_one returns the correct object.
1384
        $this->flushCache();
1385
1386
        return $this->record['ID'];
1387
    }
1388
1389
    /**
1390
     * Writes cached relation lists to the database, if possible
1391
     */
1392
    public function writeRelations()
1393
    {
1394
        if (!$this->isInDB()) {
1395
            return;
1396
        }
1397
1398
        // If there's any relations that couldn't be saved before, save them now (we have an ID here)
1399
        if ($this->unsavedRelations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->unsavedRelations of type SilverStripe\ORM\UnsavedRelationList[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1400
            foreach ($this->unsavedRelations as $name => $list) {
1401
                $list->changeToList($this->$name());
1402
            }
1403
            $this->unsavedRelations = array();
1404
        }
1405
    }
1406
1407
    /**
1408
     * Write the cached components to the database. Cached components could refer to two different instances of the
1409
     * same record.
1410
     *
1411
     * @param bool $recursive Recursively write components
1412
     * @return DataObject $this
1413
     */
1414
    public function writeComponents($recursive = false)
1415
    {
1416
        if ($this->components) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->components of type SilverStripe\ORM\DataObject[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1417
            foreach ($this->components as $component) {
1418
                $component->write(false, false, false, $recursive);
1419
            }
1420
        }
1421
1422
        if ($join = $this->getJoin()) {
1423
            $join->write(false, false, false, $recursive);
1424
        }
1425
1426
        return $this;
1427
    }
1428
1429
    /**
1430
     * Delete this data object.
1431
     * $this->onBeforeDelete() gets called.
1432
     * Note that in Versioned objects, both Stage and Live will be deleted.
1433
     *  @uses DataExtension->augmentSQL()
1434
     */
1435
    public function delete()
1436
    {
1437
        $this->brokenOnDelete = true;
1438
        $this->onBeforeDelete();
1439
        if ($this->brokenOnDelete) {
1440
            user_error(static::class . " has a broken onBeforeDelete() function."
1441
                . " Make sure that you call parent::onBeforeDelete().", E_USER_ERROR);
1442
        }
1443
1444
        // Deleting a record without an ID shouldn't do anything
1445
        if (!$this->ID) {
1446
            throw new LogicException("DataObject::delete() called on a DataObject without an ID");
1447
        }
1448
1449
        // TODO: This is quite ugly.  To improve:
1450
        //  - move the details of the delete code in the DataQuery system
1451
        //  - update the code to just delete the base table, and rely on cascading deletes in the DB to do the rest
1452
        //    obviously, that means getting requireTable() to configure cascading deletes ;-)
1453
        $srcQuery = DataList::create(static::class)
0 ignored issues
show
Bug introduced by
static::class of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1453
        $srcQuery = DataList::create(/** @scrutinizer ignore-type */ static::class)
Loading history...
1454
            ->filter('ID', $this->ID)
1455
            ->dataQuery()
1456
            ->query();
1457
        $queriedTables = $srcQuery->queriedTables();
1458
        $this->extend('updateDeleteTables', $queriedTables, $srcQuery);
1459
        foreach ($queriedTables as $table) {
1460
            $delete = SQLDelete::create("\"$table\"", array('"ID"' => $this->ID));
1461
            $this->extend('updateDeleteTable', $delete, $table, $queriedTables, $srcQuery);
1462
            $delete->execute();
1463
        }
1464
        // Remove this item out of any caches
1465
        $this->flushCache();
1466
1467
        $this->onAfterDelete();
1468
1469
        $this->OldID = $this->ID;
1470
        $this->ID = 0;
1471
    }
1472
1473
    /**
1474
     * Delete the record with the given ID.
1475
     *
1476
     * @param string $className The class name of the record to be deleted
1477
     * @param int $id ID of record to be deleted
1478
     */
1479
    public static function delete_by_id($className, $id)
1480
    {
1481
        $obj = DataObject::get_by_id($className, $id);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1482
        if ($obj) {
1483
            $obj->delete();
1484
        } else {
1485
            user_error("$className object #$id wasn't found when calling DataObject::delete_by_id", E_USER_WARNING);
1486
        }
1487
    }
1488
1489
    /**
1490
     * Get the class ancestry, including the current class name.
1491
     * The ancestry will be returned as an array of class names, where the 0th element
1492
     * will be the class that inherits directly from DataObject, and the last element
1493
     * will be the current class.
1494
     *
1495
     * @return array Class ancestry
1496
     */
1497
    public function getClassAncestry()
1498
    {
1499
        return ClassInfo::ancestry(static::class);
1500
    }
1501
1502
    /**
1503
     * Return a component object from a one to one relationship, as a DataObject.
1504
     * If no component is available, an 'empty component' will be returned for
1505
     * non-polymorphic relations, or for polymorphic relations with a class set.
1506
     *
1507
     * @param string $componentName Name of the component
1508
     * @return DataObject The component object. It's exact type will be that of the component.
1509
     * @throws Exception
1510
     */
1511
    public function getComponent($componentName)
1512
    {
1513
        if (isset($this->components[$componentName])) {
1514
            return $this->components[$componentName];
1515
        }
1516
1517
        $schema = static::getSchema();
1518
        if ($class = $schema->hasOneComponent(static::class, $componentName)) {
1519
            $joinField = $componentName . 'ID';
1520
            $joinID    = $this->getField($joinField);
1521
1522
            // Extract class name for polymorphic relations
1523
            if ($class === self::class) {
1524
                $class = $this->getField($componentName . 'Class');
1525
                if (empty($class)) {
1526
                    return null;
1527
                }
1528
            }
1529
1530
            if ($joinID) {
1531
                // Ensure that the selected object originates from the same stage, subsite, etc
1532
                $component = DataObject::get($class)
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type object; however, parameter $callerClass of SilverStripe\ORM\DataObject::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1532
                $component = DataObject::get(/** @scrutinizer ignore-type */ $class)
Loading history...
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1533
                    ->filter('ID', $joinID)
1534
                    ->setDataQueryParam($this->getInheritableQueryParams())
1535
                    ->first();
1536
            }
1537
1538
            if (empty($component)) {
1539
                $component = Injector::inst()->create($class);
1540
            }
1541
        } elseif ($class = $schema->belongsToComponent(static::class, $componentName)) {
1542
            $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic);
1543
            $joinID = $this->ID;
1544
1545
            if ($joinID) {
1546
                // Prepare filter for appropriate join type
1547
                if ($polymorphic) {
1548
                    $filter = array(
1549
                        "{$joinField}ID" => $joinID,
1550
                        "{$joinField}Class" => static::class,
1551
                    );
1552
                } else {
1553
                    $filter = array(
1554
                        $joinField => $joinID
1555
                    );
1556
                }
1557
1558
                // Ensure that the selected object originates from the same stage, subsite, etc
1559
                $component = DataObject::get($class)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1560
                    ->filter($filter)
1561
                    ->setDataQueryParam($this->getInheritableQueryParams())
1562
                    ->first();
1563
            }
1564
1565
            if (empty($component)) {
1566
                $component = Injector::inst()->create($class);
1567
                if ($polymorphic) {
1568
                    $component->{$joinField.'ID'} = $this->ID;
1569
                    $component->{$joinField.'Class'} = static::class;
1570
                } else {
1571
                    $component->$joinField = $this->ID;
1572
                }
1573
            }
1574
        } else {
1575
            throw new InvalidArgumentException(
1576
                "DataObject->getComponent(): Could not find component '$componentName'."
1577
            );
1578
        }
1579
1580
        $this->components[$componentName] = $component;
1581
        return $component;
1582
    }
1583
1584
    /**
1585
     * Returns a one-to-many relation as a HasManyList
1586
     *
1587
     * @param string $componentName Name of the component
1588
     * @return HasManyList|UnsavedRelationList The components of the one-to-many relationship.
1589
     */
1590
    public function getComponents($componentName)
1591
    {
1592
        $result = null;
1593
1594
        $schema = $this->getSchema();
1595
        $componentClass = $schema->hasManyComponent(static::class, $componentName);
1596
        if (!$componentClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $componentClass of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1597
            throw new InvalidArgumentException(sprintf(
1598
                "DataObject::getComponents(): Unknown 1-to-many component '%s' on class '%s'",
1599
                $componentName,
1600
                static::class
1601
            ));
1602
        }
1603
1604
        // If we haven't been written yet, we can't save these relations, so use a list that handles this case
1605
        if (!$this->ID) {
1606
            if (!isset($this->unsavedRelations[$componentName])) {
1607
                $this->unsavedRelations[$componentName] =
1608
                    new UnsavedRelationList(static::class, $componentName, $componentClass);
1609
            }
1610
            return $this->unsavedRelations[$componentName];
1611
        }
1612
1613
        // Determine type and nature of foreign relation
1614
        $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'has_many', $polymorphic);
1615
        /** @var HasManyList $result */
1616
        if ($polymorphic) {
1617
            $result = PolymorphicHasManyList::create($componentClass, $joinField, static::class);
0 ignored issues
show
Bug introduced by
$componentClass of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1617
            $result = PolymorphicHasManyList::create(/** @scrutinizer ignore-type */ $componentClass, $joinField, static::class);
Loading history...
1618
        } else {
1619
            $result = HasManyList::create($componentClass, $joinField);
1620
        }
1621
1622
        return $result
1623
            ->setDataQueryParam($this->getInheritableQueryParams())
1624
            ->forForeignID($this->ID);
1625
    }
1626
1627
    /**
1628
     * Find the foreign class of a relation on this DataObject, regardless of the relation type.
1629
     *
1630
     * @param string $relationName Relation name.
1631
     * @return string Class name, or null if not found.
1632
     */
1633
    public function getRelationClass($relationName)
1634
    {
1635
        // Parse many_many
1636
        $manyManyComponent = $this->getSchema()->manyManyComponent(static::class, $relationName);
1637
        if ($manyManyComponent) {
1638
            return $manyManyComponent['childClass'];
1639
        }
1640
1641
        // Go through all relationship configuration fields.
1642
        $config = $this->config();
1643
        $candidates = array_merge(
1644
            ($relations = $config->get('has_one')) ? $relations : array(),
1645
            ($relations = $config->get('has_many')) ? $relations : array(),
1646
            ($relations = $config->get('belongs_to')) ? $relations : array()
1647
        );
1648
1649
        if (isset($candidates[$relationName])) {
1650
            $remoteClass = $candidates[$relationName];
1651
1652
            // If dot notation is present, extract just the first part that contains the class.
1653
            if (($fieldPos = strpos($remoteClass, '.'))!==false) {
1654
                return substr($remoteClass, 0, $fieldPos);
1655
            }
1656
1657
            // Otherwise just return the class
1658
            return $remoteClass;
1659
        }
1660
1661
        return null;
1662
    }
1663
1664
    /**
1665
     * Given a relation name, determine the relation type
1666
     *
1667
     * @param string $component Name of component
1668
     * @return string has_one, has_many, many_many, belongs_many_many or belongs_to
1669
     */
1670
    public function getRelationType($component)
1671
    {
1672
        $types = array('has_one', 'has_many', 'many_many', 'belongs_many_many', 'belongs_to');
1673
        $config = $this->config();
1674
        foreach ($types as $type) {
1675
            $relations = $config->get($type);
1676
            if ($relations && isset($relations[$component])) {
1677
                return $type;
1678
            }
1679
        }
1680
        return null;
1681
    }
1682
1683
    /**
1684
     * Given a relation declared on a remote class, generate a substitute component for the opposite
1685
     * side of the relation.
1686
     *
1687
     * Notes on behaviour:
1688
     *  - This can still be used on components that are defined on both sides, but do not need to be.
1689
     *  - All has_ones on remote class will be treated as local has_many, even if they are belongs_to
1690
     *  - Cannot be used on polymorphic relationships
1691
     *  - Cannot be used on unsaved objects.
1692
     *
1693
     * @param string $remoteClass
1694
     * @param string $remoteRelation
1695
     * @return DataList|DataObject The component, either as a list or single object
1696
     * @throws BadMethodCallException
1697
     * @throws InvalidArgumentException
1698
     */
1699
    public function inferReciprocalComponent($remoteClass, $remoteRelation)
1700
    {
1701
        $remote = DataObject::singleton($remoteClass);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1702
        $class = $remote->getRelationClass($remoteRelation);
1703
        $schema = static::getSchema();
1704
1705
        // Validate arguments
1706
        if (!$this->isInDB()) {
1707
            throw new BadMethodCallException(__METHOD__ . " cannot be called on unsaved objects");
1708
        }
1709
        if (empty($class)) {
1710
            throw new InvalidArgumentException(sprintf(
1711
                "%s invoked with invalid relation %s.%s",
1712
                __METHOD__,
1713
                $remoteClass,
1714
                $remoteRelation
1715
            ));
1716
        }
1717
        if ($class === self::class) {
1718
            throw new InvalidArgumentException(sprintf(
1719
                "%s cannot generate opposite component of relation %s.%s as it is polymorphic. " .
1720
                "This method does not support polymorphic relationships",
1721
                __METHOD__,
1722
                $remoteClass,
1723
                $remoteRelation
1724
            ));
1725
        }
1726
        if (!is_a($this, $class, true)) {
1727
            throw new InvalidArgumentException(sprintf(
1728
                "Relation %s on %s does not refer to objects of type %s",
1729
                $remoteRelation,
1730
                $remoteClass,
1731
                static::class
1732
            ));
1733
        }
1734
1735
        // Check the relation type to mock
1736
        $relationType = $remote->getRelationType($remoteRelation);
1737
        switch ($relationType) {
1738
            case 'has_one': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1739
                // Mock has_many
1740
                $joinField = "{$remoteRelation}ID";
1741
                $componentClass = $schema->classForField($remoteClass, $joinField);
1742
                $result = HasManyList::create($componentClass, $joinField);
0 ignored issues
show
Bug introduced by
$componentClass of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1742
                $result = HasManyList::create(/** @scrutinizer ignore-type */ $componentClass, $joinField);
Loading history...
1743
                return $result
1744
                    ->setDataQueryParam($this->getInheritableQueryParams())
1745
                    ->forForeignID($this->ID);
1746
            }
1747
            case 'belongs_to':
1748
            case 'has_many': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1749
                // These relations must have a has_one on the other end, so find it
1750
                $joinField = $schema->getRemoteJoinField($remoteClass, $remoteRelation, $relationType, $polymorphic);
1751
                if ($polymorphic) {
1752
                    throw new InvalidArgumentException(sprintf(
1753
                        "%s cannot generate opposite component of relation %s.%s, as the other end appears" .
1754
                        "to be a has_one polymorphic. This method does not support polymorphic relationships",
1755
                        __METHOD__,
1756
                        $remoteClass,
1757
                        $remoteRelation
1758
                    ));
1759
                }
1760
                $joinID = $this->getField($joinField);
1761
                if (empty($joinID)) {
1762
                    return null;
1763
                }
1764
                // Get object by joined ID
1765
                return DataObject::get($remoteClass)
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
1766
                    ->filter('ID', $joinID)
1767
                    ->setDataQueryParam($this->getInheritableQueryParams())
1768
                    ->first();
1769
            }
1770
            case 'many_many':
1771
            case 'belongs_many_many': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1772
                // Get components and extra fields from parent
1773
                $manyMany = $remote->getSchema()->manyManyComponent($remoteClass, $remoteRelation);
1774
                $extraFields = $schema->manyManyExtraFieldsForComponent($remoteClass, $remoteRelation) ?: array();
1775
1776
                // Reverse parent and component fields and create an inverse ManyManyList
1777
                /** @var RelationList $result */
1778
                $result = Injector::inst()->create(
1779
                    $manyMany['relationClass'],
1780
                    $manyMany['parentClass'], // Substitute parent class for dataClass
1781
                    $manyMany['join'],
1782
                    $manyMany['parentField'], // Reversed parent / child field
1783
                    $manyMany['childField'], // Reversed parent / child field
1784
                    $extraFields
1785
                );
1786
                $this->extend('updateManyManyComponents', $result);
1787
1788
                // If this is called on a singleton, then we return an 'orphaned relation' that can have the
1789
                // foreignID set elsewhere.
1790
                return $result
1791
                    ->setDataQueryParam($this->getInheritableQueryParams())
1792
                    ->forForeignID($this->ID);
1793
            }
1794
            default: {
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1795
                return null;
1796
            }
1797
        }
1798
    }
1799
1800
    /**
1801
     * Returns a many-to-many component, as a ManyManyList.
1802
     * @param string $componentName Name of the many-many component
1803
     * @return RelationList|UnsavedRelationList The set of components
1804
     */
1805
    public function getManyManyComponents($componentName)
1806
    {
1807
        $schema = static::getSchema();
1808
        $manyManyComponent = $schema->manyManyComponent(static::class, $componentName);
1809
        if (!$manyManyComponent) {
1810
            throw new InvalidArgumentException(sprintf(
1811
                "DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'",
1812
                $componentName,
1813
                static::class
1814
            ));
1815
        }
1816
1817
        // If we haven't been written yet, we can't save these relations, so use a list that handles this case
1818
        if (!$this->ID) {
1819
            if (!isset($this->unsavedRelations[$componentName])) {
1820
                $this->unsavedRelations[$componentName] =
1821
                    new UnsavedRelationList($manyManyComponent['parentClass'], $componentName, $manyManyComponent['childClass']);
1822
            }
1823
            return $this->unsavedRelations[$componentName];
1824
        }
1825
1826
        $extraFields = $schema->manyManyExtraFieldsForComponent(static::class, $componentName) ?: array();
1827
        /** @var RelationList $result */
1828
        $result = Injector::inst()->create(
1829
            $manyManyComponent['relationClass'],
1830
            $manyManyComponent['childClass'],
1831
            $manyManyComponent['join'],
1832
            $manyManyComponent['childField'],
1833
            $manyManyComponent['parentField'],
1834
            $extraFields
1835
        );
1836
1837
1838
        // Store component data in query meta-data
1839
        $result = $result->alterDataQuery(function ($query) use ($extraFields) {
1840
            /** @var DataQuery $query */
1841
            $query->setQueryParam('Component.ExtraFields', $extraFields);
1842
        });
1843
1844
        $this->extend('updateManyManyComponents', $result);
1845
1846
        // If this is called on a singleton, then we return an 'orphaned relation' that can have the
1847
        // foreignID set elsewhere.
1848
        return $result
1849
            ->setDataQueryParam($this->getInheritableQueryParams())
1850
            ->forForeignID($this->ID);
1851
    }
1852
1853
    /**
1854
     * Return the class of a one-to-one component.  If $component is null, return all of the one-to-one components and
1855
     * their classes. If the selected has_one is a polymorphic field then 'DataObject' will be returned for the type.
1856
     *
1857
     * @return string|array The class of the one-to-one component, or an array of all one-to-one components and
1858
     *                          their classes.
1859
     */
1860
    public function hasOne()
1861
    {
1862
        return (array)$this->config()->get('has_one');
1863
    }
1864
1865
    /**
1866
     * Returns the class of a remote belongs_to relationship. If no component is specified a map of all components and
1867
     * their class name will be returned.
1868
     *
1869
     * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have
1870
     *        the field data stripped off. It defaults to TRUE.
1871
     * @return string|array
1872
     */
1873
    public function belongsTo($classOnly = true)
1874
    {
1875
        $belongsTo = (array)$this->config()->get('belongs_to');
1876
        if ($belongsTo && $classOnly) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $belongsTo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1877
            return preg_replace('/(.+)?\..+/', '$1', $belongsTo);
1878
        } else {
1879
            return $belongsTo ? $belongsTo : array();
1880
        }
1881
    }
1882
1883
    /**
1884
     * Gets the class of a one-to-many relationship. If no $component is specified then an array of all the one-to-many
1885
     * relationships and their classes will be returned.
1886
     *
1887
     * @param bool $classOnly If this is TRUE, than any has_many relationships in the form "ClassName.Field" will have
1888
     *        the field data stripped off. It defaults to TRUE.
1889
     * @return string|array|false
1890
     */
1891
    public function hasMany($classOnly = true)
1892
    {
1893
        $hasMany = (array)$this->config()->get('has_many');
1894
        if ($hasMany && $classOnly) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hasMany of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1895
            return preg_replace('/(.+)?\..+/', '$1', $hasMany);
1896
        } else {
1897
            return $hasMany ? $hasMany : array();
1898
        }
1899
    }
1900
1901
    /**
1902
     * Return the many-to-many extra fields specification.
1903
     *
1904
     * If you don't specify a component name, it returns all
1905
     * extra fields for all components available.
1906
     *
1907
     * @return array|null
1908
     */
1909
    public function manyManyExtraFields()
1910
    {
1911
        return $this->config()->get('many_many_extraFields');
1912
    }
1913
1914
    /**
1915
     * Return information about a many-to-many component.
1916
     * The return value is an array of (parentclass, childclass).  If $component is null, then all many-many
1917
     * components are returned.
1918
     *
1919
     * @see DataObjectSchema::manyManyComponent()
1920
     * @return array|null An array of (parentclass, childclass), or an array of all many-many components
1921
     */
1922
    public function manyMany()
1923
    {
1924
        $config = $this->config();
1925
        $manyManys = (array)$config->get('many_many');
1926
        $belongsManyManys = (array)$config->get('belongs_many_many');
1927
        $items = array_merge($manyManys, $belongsManyManys);
1928
        return $items;
1929
    }
1930
1931
    /**
1932
     * This returns an array (if it exists) describing the database extensions that are required, or false if none
1933
     *
1934
     * This is experimental, and is currently only a Postgres-specific enhancement.
1935
     *
1936
     * @param string $class
1937
     * @return array|false
1938
     */
1939
    public function database_extensions($class)
1940
    {
1941
        $extensions = Config::inst()->get($class, 'database_extensions', Config::UNINHERITED);
1942
        if ($extensions) {
1943
            return $extensions;
1944
        } else {
1945
            return false;
1946
        }
1947
    }
1948
1949
    /**
1950
     * Generates a SearchContext to be used for building and processing
1951
     * a generic search form for properties on this object.
1952
     *
1953
     * @return SearchContext
1954
     */
1955
    public function getDefaultSearchContext()
1956
    {
1957
        return new SearchContext(
1958
            static::class,
1959
            $this->scaffoldSearchFields(),
1960
            $this->defaultSearchFilters()
1961
        );
1962
    }
1963
1964
    /**
1965
     * Determine which properties on the DataObject are
1966
     * searchable, and map them to their default {@link FormField}
1967
     * representations. Used for scaffolding a searchform for {@link ModelAdmin}.
1968
     *
1969
     * Some additional logic is included for switching field labels, based on
1970
     * how generic or specific the field type is.
1971
     *
1972
     * Used by {@link SearchContext}.
1973
     *
1974
     * @param array $_params
1975
     *   'fieldClasses': Associative array of field names as keys and FormField classes as values
1976
     *   'restrictFields': Numeric array of a field name whitelist
1977
     * @return FieldList
1978
     */
1979
    public function scaffoldSearchFields($_params = null)
1980
    {
1981
        $params = array_merge(
1982
            array(
1983
                'fieldClasses' => false,
1984
                'restrictFields' => false
1985
            ),
1986
            (array)$_params
1987
        );
1988
        $fields = new FieldList();
1989
        foreach ($this->searchableFields() as $fieldName => $spec) {
1990
            if ($params['restrictFields'] && !in_array($fieldName, $params['restrictFields'])) {
1991
                continue;
1992
            }
1993
1994
            // If a custom fieldclass is provided as a string, use it
1995
            $field = null;
1996
            if ($params['fieldClasses'] && isset($params['fieldClasses'][$fieldName])) {
1997
                $fieldClass = $params['fieldClasses'][$fieldName];
1998
                $field = new $fieldClass($fieldName);
1999
            // If we explicitly set a field, then construct that
2000
            } elseif (isset($spec['field'])) {
2001
                // If it's a string, use it as a class name and construct
2002
                if (is_string($spec['field'])) {
2003
                    $fieldClass = $spec['field'];
2004
                    $field = new $fieldClass($fieldName);
2005
2006
                // If it's a FormField object, then just use that object directly.
2007
                } elseif ($spec['field'] instanceof FormField) {
2008
                    $field = $spec['field'];
2009
2010
                // Otherwise we have a bug
2011
                } else {
2012
                    user_error("Bad value for searchable_fields, 'field' value: "
2013
                        . var_export($spec['field'], true), E_USER_WARNING);
2014
                }
2015
2016
            // Otherwise, use the database field's scaffolder
2017
            } else {
2018
                $field = $this->relObject($fieldName)->scaffoldSearchField();
2019
            }
2020
2021
            // Allow fields to opt out of search
2022
            if (!$field) {
2023
                continue;
2024
            }
2025
2026
            if (strstr($fieldName, '.')) {
2027
                $field->setName(str_replace('.', '__', $fieldName));
2028
            }
2029
            $field->setTitle($spec['title']);
2030
2031
            $fields->push($field);
2032
        }
2033
        return $fields;
2034
    }
2035
2036
    /**
2037
     * Scaffold a simple edit form for all properties on this dataobject,
2038
     * based on default {@link FormField} mapping in {@link DBField::scaffoldFormField()}.
2039
     * Field labels/titles will be auto generated from {@link DataObject::fieldLabels()}.
2040
     *
2041
     * @uses FormScaffolder
2042
     *
2043
     * @param array $_params Associative array passing through properties to {@link FormScaffolder}.
2044
     * @return FieldList
2045
     */
2046
    public function scaffoldFormFields($_params = null)
2047
    {
2048
        $params = array_merge(
2049
            array(
2050
                'tabbed' => false,
2051
                'includeRelations' => false,
2052
                'restrictFields' => false,
2053
                'fieldClasses' => false,
2054
                'ajaxSafe' => false
2055
            ),
2056
            (array)$_params
2057
        );
2058
2059
        $fs = FormScaffolder::create($this);
0 ignored issues
show
Bug introduced by
$this of type SilverStripe\ORM\DataObject is incompatible with the type array expected by parameter $args of SilverStripe\Forms\FormScaffolder::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2059
        $fs = FormScaffolder::create(/** @scrutinizer ignore-type */ $this);
Loading history...
2060
        $fs->tabbed = $params['tabbed'];
2061
        $fs->includeRelations = $params['includeRelations'];
2062
        $fs->restrictFields = $params['restrictFields'];
2063
        $fs->fieldClasses = $params['fieldClasses'];
2064
        $fs->ajaxSafe = $params['ajaxSafe'];
2065
2066
        return $fs->getFieldList();
2067
    }
2068
2069
    /**
2070
     * Allows user code to hook into DataObject::getCMSFields prior to updateCMSFields
2071
     * being called on extensions
2072
     *
2073
     * @param callable $callback The callback to execute
2074
     */
2075
    protected function beforeUpdateCMSFields($callback)
2076
    {
2077
        $this->beforeExtending('updateCMSFields', $callback);
2078
    }
2079
2080
    /**
2081
     * Centerpiece of every data administration interface in Silverstripe,
2082
     * which returns a {@link FieldList} suitable for a {@link Form} object.
2083
     * If not overloaded, we're using {@link scaffoldFormFields()} to automatically
2084
     * generate this set. To customize, overload this method in a subclass
2085
     * or extended onto it by using {@link DataExtension->updateCMSFields()}.
2086
     *
2087
     * <code>
2088
     * class MyCustomClass extends DataObject {
2089
     *  static $db = array('CustomProperty'=>'Boolean');
2090
     *
2091
     *  function getCMSFields() {
2092
     *    $fields = parent::getCMSFields();
2093
     *    $fields->addFieldToTab('Root.Content',new CheckboxField('CustomProperty'));
2094
     *    return $fields;
2095
     *  }
2096
     * }
2097
     * </code>
2098
     *
2099
     * @see Good example of complex FormField building: SiteTree::getCMSFields()
2100
     *
2101
     * @return FieldList Returns a TabSet for usage within the CMS - don't use for frontend forms.
2102
     */
2103
    public function getCMSFields()
2104
    {
2105
        $tabbedFields = $this->scaffoldFormFields(array(
2106
            // Don't allow has_many/many_many relationship editing before the record is first saved
2107
            'includeRelations' => ($this->ID > 0),
2108
            'tabbed' => true,
2109
            'ajaxSafe' => true
2110
        ));
2111
2112
        $this->extend('updateCMSFields', $tabbedFields);
2113
2114
        return $tabbedFields;
2115
    }
2116
2117
    /**
2118
     * need to be overload by solid dataobject, so that the customised actions of that dataobject,
2119
     * including that dataobject's extensions customised actions could be added to the EditForm.
2120
     *
2121
     * @return FieldList an Empty FieldList(); need to be overload by solid subclass
2122
     */
2123
    public function getCMSActions()
2124
    {
2125
        $actions = new FieldList();
2126
        $this->extend('updateCMSActions', $actions);
2127
        return $actions;
2128
    }
2129
2130
2131
    /**
2132
     * Used for simple frontend forms without relation editing
2133
     * or {@link TabSet} behaviour. Uses {@link scaffoldFormFields()}
2134
     * by default. To customize, either overload this method in your
2135
     * subclass, or extend it by {@link DataExtension->updateFrontEndFields()}.
2136
     *
2137
     * @todo Decide on naming for "website|frontend|site|page" and stick with it in the API
2138
     *
2139
     * @param array $params See {@link scaffoldFormFields()}
2140
     * @return FieldList Always returns a simple field collection without TabSet.
2141
     */
2142
    public function getFrontEndFields($params = null)
2143
    {
2144
        $untabbedFields = $this->scaffoldFormFields($params);
2145
        $this->extend('updateFrontEndFields', $untabbedFields);
2146
2147
        return $untabbedFields;
2148
    }
2149
2150
    public function getViewerTemplates($suffix = '')
2151
    {
2152
        return SSViewer::get_templates_by_class(static::class, $suffix, $this->baseClass());
2153
    }
2154
2155
    /**
2156
     * Gets the value of a field.
2157
     * Called by {@link __get()} and any getFieldName() methods you might create.
2158
     *
2159
     * @param string $field The name of the field
2160
     * @return mixed The field value
2161
     */
2162
    public function getField($field)
2163
    {
2164
        // If we already have an object in $this->record, then we should just return that
2165
        if (isset($this->record[$field]) && is_object($this->record[$field])) {
2166
            return $this->record[$field];
2167
        }
2168
2169
        // Do we have a field that needs to be lazy loaded?
2170
        if (isset($this->record[$field.'_Lazy'])) {
2171
            $tableClass = $this->record[$field.'_Lazy'];
2172
            $this->loadLazyFields($tableClass);
2173
        }
2174
2175
        // In case of complex fields, return the DBField object
2176
        if (static::getSchema()->compositeField(static::class, $field)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::getSchema()->com...(static::class, $field) of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2177
            $this->record[$field] = $this->dbObject($field);
2178
        }
2179
2180
        return isset($this->record[$field]) ? $this->record[$field] : null;
2181
    }
2182
2183
    /**
2184
     * Loads all the stub fields that an initial lazy load didn't load fully.
2185
     *
2186
     * @param string $class Class to load the values from. Others are joined as required.
2187
     * Not specifying a tableClass will load all lazy fields from all tables.
2188
     * @return bool Flag if lazy loading succeeded
2189
     */
2190
    protected function loadLazyFields($class = null)
2191
    {
2192
        if (!$this->isInDB() || !is_numeric($this->ID)) {
2193
            return false;
2194
        }
2195
2196
        if (!$class) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2197
            $loaded = array();
2198
2199
            foreach ($this->record as $key => $value) {
2200
                if (strlen($key) > 5 && substr($key, -5) == '_Lazy' && !array_key_exists($value, $loaded)) {
2201
                    $this->loadLazyFields($value);
2202
                    $loaded[$value] = $value;
2203
                }
2204
            }
2205
2206
            return false;
2207
        }
2208
2209
        $dataQuery = new DataQuery($class);
2210
2211
        // Reset query parameter context to that of this DataObject
2212
        if ($params = $this->getSourceQueryParams()) {
2213
            foreach ($params as $key => $value) {
2214
                $dataQuery->setQueryParam($key, $value);
2215
            }
2216
        }
2217
2218
        // Limit query to the current record, unless it has the Versioned extension,
2219
        // in which case it requires special handling through augmentLoadLazyFields()
2220
        $schema = static::getSchema();
2221
        $baseIDColumn = $schema->sqlColumnForField($this, 'ID');
2222
        $dataQuery->where([
2223
            $baseIDColumn => $this->record['ID']
2224
        ])->limit(1);
2225
2226
        $columns = array();
2227
2228
        // Add SQL for fields, both simple & multi-value
2229
        // TODO: This is copy & pasted from buildSQL(), it could be moved into a method
2230
        $databaseFields = $schema->databaseFields($class, false);
2231
        foreach ($databaseFields as $k => $v) {
2232
            if (!isset($this->record[$k]) || $this->record[$k] === null) {
2233
                $columns[] = $k;
2234
            }
2235
        }
2236
2237
        if ($columns) {
2238
            $query = $dataQuery->query();
2239
            $this->extend('augmentLoadLazyFields', $query, $dataQuery, $this);
2240
            $this->extend('augmentSQL', $query, $dataQuery);
2241
2242
            $dataQuery->setQueriedColumns($columns);
2243
            $newData = $dataQuery->execute()->record();
2244
2245
            // Load the data into record
2246
            if ($newData) {
2247
                foreach ($newData as $k => $v) {
2248
                    if (in_array($k, $columns)) {
2249
                        $this->record[$k] = $v;
2250
                        $this->original[$k] = $v;
2251
                        unset($this->record[$k . '_Lazy']);
2252
                    }
2253
                }
2254
2255
            // No data means that the query returned nothing; assign 'null' to all the requested fields
2256
            } else {
2257
                foreach ($columns as $k) {
2258
                    $this->record[$k] = null;
2259
                    $this->original[$k] = null;
2260
                    unset($this->record[$k . '_Lazy']);
2261
                }
2262
            }
2263
        }
2264
        return true;
2265
    }
2266
2267
    /**
2268
     * Return the fields that have changed.
2269
     *
2270
     * The change level affects what the functions defines as "changed":
2271
     * - Level CHANGE_STRICT (integer 1) will return strict changes, even !== ones.
2272
     * - Level CHANGE_VALUE (integer 2) is more lenient, it will only return real data changes,
2273
     *   for example a change from 0 to null would not be included.
2274
     *
2275
     * Example return:
2276
     * <code>
2277
     * array(
2278
     *   'Title' = array('before' => 'Home', 'after' => 'Home-Changed', 'level' => DataObject::CHANGE_VALUE)
2279
     * )
2280
     * </code>
2281
     *
2282
     * @param boolean|array $databaseFieldsOnly Filter to determine which fields to return. Set to true
2283
     * to return all database fields, or an array for an explicit filter. false returns all fields.
2284
     * @param int $changeLevel The strictness of what is defined as change. Defaults to strict
2285
     * @return array
2286
     */
2287
    public function getChangedFields($databaseFieldsOnly = false, $changeLevel = self::CHANGE_STRICT)
2288
    {
2289
        $changedFields = array();
2290
2291
        // Update the changed array with references to changed obj-fields
2292
        foreach ($this->record as $k => $v) {
2293
            // Prevents DBComposite infinite looping on isChanged
2294
            if (is_array($databaseFieldsOnly) && !in_array($k, $databaseFieldsOnly)) {
2295
                continue;
2296
            }
2297
            if (is_object($v) && method_exists($v, 'isChanged') && $v->isChanged()) {
2298
                $this->changed[$k] = self::CHANGE_VALUE;
2299
            }
2300
        }
2301
2302
        if (is_array($databaseFieldsOnly)) {
2303
            $fields = array_intersect_key((array)$this->changed, array_flip($databaseFieldsOnly));
2304
        } elseif ($databaseFieldsOnly) {
2305
            $fieldsSpecs = static::getSchema()->fieldSpecs(static::class);
2306
            $fields = array_intersect_key((array)$this->changed, $fieldsSpecs);
2307
        } else {
2308
            $fields = $this->changed;
2309
        }
2310
2311
        // Filter the list to those of a certain change level
2312
        if ($changeLevel > self::CHANGE_STRICT) {
2313
            if ($fields) {
2314
                foreach ($fields as $name => $level) {
2315
                    if ($level < $changeLevel) {
2316
                        unset($fields[$name]);
2317
                    }
2318
                }
2319
            }
2320
        }
2321
2322
        if ($fields) {
2323
            foreach ($fields as $name => $level) {
2324
                $changedFields[$name] = array(
2325
                'before' => array_key_exists($name, $this->original) ? $this->original[$name] : null,
2326
                'after' => array_key_exists($name, $this->record) ? $this->record[$name] : null,
2327
                'level' => $level
2328
                );
2329
            }
2330
        }
2331
2332
        return $changedFields;
2333
    }
2334
2335
    /**
2336
     * Uses {@link getChangedFields()} to determine if fields have been changed
2337
     * since loading them from the database.
2338
     *
2339
     * @param string $fieldName Name of the database field to check, will check for any if not given
2340
     * @param int $changeLevel See {@link getChangedFields()}
2341
     * @return boolean
2342
     */
2343
    public function isChanged($fieldName = null, $changeLevel = self::CHANGE_STRICT)
2344
    {
2345
        $fields = $fieldName ? array($fieldName) : true;
2346
        $changed = $this->getChangedFields($fields, $changeLevel);
2347
        if (!isset($fieldName)) {
2348
            return !empty($changed);
2349
        } else {
2350
            return array_key_exists($fieldName, $changed);
2351
        }
2352
    }
2353
2354
    /**
2355
     * Set the value of the field
2356
     * Called by {@link __set()} and any setFieldName() methods you might create.
2357
     *
2358
     * @param string $fieldName Name of the field
2359
     * @param mixed $val New field value
2360
     * @return $this
2361
     */
2362
    public function setField($fieldName, $val)
2363
    {
2364
        $this->objCacheClear();
2365
        //if it's a has_one component, destroy the cache
2366
        if (substr($fieldName, -2) == 'ID') {
2367
            unset($this->components[substr($fieldName, 0, -2)]);
2368
        }
2369
2370
        // If we've just lazy-loaded the column, then we need to populate the $original array
2371
        if (isset($this->record[$fieldName.'_Lazy'])) {
2372
            $tableClass = $this->record[$fieldName.'_Lazy'];
2373
            $this->loadLazyFields($tableClass);
2374
        }
2375
2376
        // Situation 1: Passing an DBField
2377
        if ($val instanceof DBField) {
2378
            $val->setName($fieldName);
2379
            $val->saveInto($this);
2380
2381
            // Situation 1a: Composite fields should remain bound in case they are
2382
            // later referenced to update the parent dataobject
2383
            if ($val instanceof DBComposite) {
2384
                $val->bindTo($this);
2385
                $this->record[$fieldName] = $val;
2386
            }
2387
        // Situation 2: Passing a literal or non-DBField object
2388
        } else {
2389
            // If this is a proper database field, we shouldn't be getting non-DBField objects
2390
            if (is_object($val) && static::getSchema()->fieldSpec(static::class, $fieldName)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::getSchema()->fie...tic::class, $fieldName) of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2391
                throw new InvalidArgumentException('DataObject::setField: passed an object that is not a DBField');
2392
            }
2393
2394
            // if a field is not existing or has strictly changed
2395
            if (!isset($this->record[$fieldName]) || $this->record[$fieldName] !== $val) {
2396
                // TODO Add check for php-level defaults which are not set in the db
2397
                // TODO Add check for hidden input-fields (readonly) which are not set in the db
2398
                // At the very least, the type has changed
2399
                $this->changed[$fieldName] = self::CHANGE_STRICT;
2400
2401
                if ((!isset($this->record[$fieldName]) && $val)
2402
                    || (isset($this->record[$fieldName]) && $this->record[$fieldName] != $val)
2403
                ) {
2404
                    // Value has changed as well, not just the type
2405
                    $this->changed[$fieldName] = self::CHANGE_VALUE;
2406
                }
2407
2408
                // Value is always saved back when strict check succeeds.
2409
                $this->record[$fieldName] = $val;
2410
            }
2411
        }
2412
        return $this;
2413
    }
2414
2415
    /**
2416
     * Set the value of the field, using a casting object.
2417
     * This is useful when you aren't sure that a date is in SQL format, for example.
2418
     * setCastedField() can also be used, by forms, to set related data.  For example, uploaded images
2419
     * can be saved into the Image table.
2420
     *
2421
     * @param string $fieldName Name of the field
2422
     * @param mixed $value New field value
2423
     * @return $this
2424
     */
2425
    public function setCastedField($fieldName, $value)
2426
    {
2427
        if (!$fieldName) {
2428
            user_error("DataObject::setCastedField: Called without a fieldName", E_USER_ERROR);
2429
        }
2430
        $fieldObj = $this->dbObject($fieldName);
2431
        if ($fieldObj) {
2432
            $fieldObj->setValue($value);
2433
            $fieldObj->saveInto($this);
2434
        } else {
2435
            $this->$fieldName = $value;
2436
        }
2437
        return $this;
2438
    }
2439
2440
    /**
2441
     * {@inheritdoc}
2442
     */
2443
    public function castingHelper($field)
2444
    {
2445
        $fieldSpec = static::getSchema()->fieldSpec(static::class, $field);
2446
        if ($fieldSpec) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fieldSpec of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2447
            return $fieldSpec;
2448
        }
2449
2450
        // many_many_extraFields aren't presented by db(), so we check if the source query params
2451
        // provide us with meta-data for a many_many relation we can inspect for extra fields.
2452
        $queryParams = $this->getSourceQueryParams();
2453
        if (!empty($queryParams['Component.ExtraFields'])) {
2454
            $extraFields = $queryParams['Component.ExtraFields'];
2455
2456
            if (isset($extraFields[$field])) {
2457
                return $extraFields[$field];
2458
            }
2459
        }
2460
2461
        return parent::castingHelper($field);
2462
    }
2463
2464
    /**
2465
     * Returns true if the given field exists in a database column on any of
2466
     * the objects tables and optionally look up a dynamic getter with
2467
     * get<fieldName>().
2468
     *
2469
     * @param string $field Name of the field
2470
     * @return boolean True if the given field exists
2471
     */
2472
    public function hasField($field)
2473
    {
2474
        $schema = static::getSchema();
2475
        return (
2476
            array_key_exists($field, $this->record)
2477
            || $schema->fieldSpec(static::class, $field)
0 ignored issues
show
Bug Best Practice introduced by
The expression $schema->fieldSpec(static::class, $field) of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2478
            || (substr($field, -2) == 'ID') && $schema->hasOneComponent(static::class, substr($field, 0, -2))
0 ignored issues
show
Bug introduced by
It seems like substr($field, 0, -2) can also be of type false; however, parameter $component of SilverStripe\ORM\DataObj...hema::hasOneComponent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2478
            || (substr($field, -2) == 'ID') && $schema->hasOneComponent(static::class, /** @scrutinizer ignore-type */ substr($field, 0, -2))
Loading history...
Bug Best Practice introduced by
The expression $schema->hasOneComponent... substr($field, 0, -2)) of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2479
            || $this->hasMethod("get{$field}")
2480
        );
2481
    }
2482
2483
    /**
2484
     * Returns true if the given field exists as a database column
2485
     *
2486
     * @param string $field Name of the field
2487
     *
2488
     * @return boolean
2489
     */
2490
    public function hasDatabaseField($field)
2491
    {
2492
        $spec = static::getSchema()->fieldSpec(static::class, $field, DataObjectSchema::DB_ONLY);
2493
        return !empty($spec);
2494
    }
2495
2496
    /**
2497
     * Returns true if the member is allowed to do the given action.
2498
     * See {@link extendedCan()} for a more versatile tri-state permission control.
2499
     *
2500
     * @param string $perm The permission to be checked, such as 'View'.
2501
     * @param Member $member The member whose permissions need checking.  Defaults to the currently logged
2502
     * in user.
2503
     * @param array $context Additional $context to pass to extendedCan()
2504
     *
2505
     * @return boolean True if the the member is allowed to do the given action
2506
     */
2507
    public function can($perm, $member = null, $context = array())
2508
    {
2509
        if (!$member) {
2510
            $member = Security::getCurrentUser();
2511
        }
2512
2513
        if ($member && Permission::checkMember($member, "ADMIN")) {
2514
            return true;
2515
        }
2516
2517
        if (is_string($perm) && method_exists($this, 'can' . ucfirst($perm))) {
2518
            $method = 'can' . ucfirst($perm);
2519
            return $this->$method($member);
2520
        }
2521
2522
        $results = $this->extendedCan('can', $member);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $results is correct as $this->extendedCan('can', $member) targeting SilverStripe\ORM\DataObject::extendedCan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2523
        if (isset($results)) {
2524
            return $results;
2525
        }
2526
2527
        return ($member && Permission::checkMember($member, $perm));
2528
    }
2529
2530
    /**
2531
     * Process tri-state responses from permission-alterting extensions.  The extensions are
2532
     * expected to return one of three values:
2533
     *
2534
     *  - false: Disallow this permission, regardless of what other extensions say
2535
     *  - true: Allow this permission, as long as no other extensions return false
2536
     *  - NULL: Don't affect the outcome
2537
     *
2538
     * This method itself returns a tri-state value, and is designed to be used like this:
2539
     *
2540
     * <code>
2541
     * $extended = $this->extendedCan('canDoSomething', $member);
2542
     * if($extended !== null) return $extended;
2543
     * else return $normalValue;
2544
     * </code>
2545
     *
2546
     * @param string $methodName Method on the same object, e.g. {@link canEdit()}
2547
     * @param Member|int $member
2548
     * @param array $context Optional context
2549
     * @return boolean|null
2550
     */
2551
    public function extendedCan($methodName, $member, $context = array())
2552
    {
2553
        $results = $this->extend($methodName, $member, $context);
2554
        if ($results && is_array($results)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
2555
            // Remove NULLs
2556
            $results = array_filter($results, function ($v) {
2557
                return !is_null($v);
2558
            });
2559
            // If there are any non-NULL responses, then return the lowest one of them.
2560
            // If any explicitly deny the permission, then we don't get access
2561
            if ($results) {
2562
                return min($results);
2563
            }
2564
        }
2565
        return null;
2566
    }
2567
2568
    /**
2569
     * @param Member $member
2570
     * @return boolean
2571
     */
2572
    public function canView($member = null)
2573
    {
2574
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $extended is correct as $this->extendedCan(__FUNCTION__, $member) targeting SilverStripe\ORM\DataObject::extendedCan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2575
        if ($extended !== null) {
2576
            return $extended;
2577
        }
2578
        return Permission::check('ADMIN', 'any', $member);
2579
    }
2580
2581
    /**
2582
     * @param Member $member
2583
     * @return boolean
2584
     */
2585
    public function canEdit($member = null)
2586
    {
2587
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $extended is correct as $this->extendedCan(__FUNCTION__, $member) targeting SilverStripe\ORM\DataObject::extendedCan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2588
        if ($extended !== null) {
2589
            return $extended;
2590
        }
2591
        return Permission::check('ADMIN', 'any', $member);
2592
    }
2593
2594
    /**
2595
     * @param Member $member
2596
     * @return boolean
2597
     */
2598
    public function canDelete($member = null)
2599
    {
2600
        $extended = $this->extendedCan(__FUNCTION__, $member);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $extended is correct as $this->extendedCan(__FUNCTION__, $member) targeting SilverStripe\ORM\DataObject::extendedCan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2601
        if ($extended !== null) {
2602
            return $extended;
2603
        }
2604
        return Permission::check('ADMIN', 'any', $member);
2605
    }
2606
2607
    /**
2608
     * @param Member $member
2609
     * @param array $context Additional context-specific data which might
2610
     * affect whether (or where) this object could be created.
2611
     * @return boolean
2612
     */
2613
    public function canCreate($member = null, $context = array())
2614
    {
2615
        $extended = $this->extendedCan(__FUNCTION__, $member, $context);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $extended is correct as $this->extendedCan(__FUN...N__, $member, $context) targeting SilverStripe\ORM\DataObject::extendedCan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
2616
        if ($extended !== null) {
2617
            return $extended;
2618
        }
2619
        return Permission::check('ADMIN', 'any', $member);
2620
    }
2621
2622
    /**
2623
     * Debugging used by Debug::show()
2624
     *
2625
     * @return string HTML data representing this object
2626
     */
2627
    public function debug()
2628
    {
2629
        $class = static::class;
2630
        $val = "<h3>Database record: {$class}</h3>\n<ul>\n";
2631
        if ($this->record) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->record of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
2632
            foreach ($this->record as $fieldName => $fieldVal) {
2633
                $val .= "\t<li>$fieldName: " . Debug::text($fieldVal) . "</li>\n";
2634
            }
2635
        }
2636
        $val .= "</ul>\n";
2637
        return $val;
2638
    }
2639
2640
    /**
2641
     * Return the DBField object that represents the given field.
2642
     * This works similarly to obj() with 2 key differences:
2643
     *   - it still returns an object even when the field has no value.
2644
     *   - it only matches fields and not methods
2645
     *   - it matches foreign keys generated by has_one relationships, eg, "ParentID"
2646
     *
2647
     * @param string $fieldName Name of the field
2648
     * @return DBField The field as a DBField object
2649
     */
2650
    public function dbObject($fieldName)
2651
    {
2652
        // Check for field in DB
2653
        $schema = static::getSchema();
2654
        $helper = $schema->fieldSpec(static::class, $fieldName, DataObjectSchema::INCLUDE_CLASS);
2655
        if (!$helper) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $helper of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
2656
            return null;
2657
        }
2658
2659
        if (!isset($this->record[$fieldName]) && isset($this->record[$fieldName . '_Lazy'])) {
2660
            $tableClass = $this->record[$fieldName . '_Lazy'];
2661
            $this->loadLazyFields($tableClass);
2662
        }
2663
2664
        $value = isset($this->record[$fieldName])
2665
            ? $this->record[$fieldName]
2666
            : null;
2667
2668
        // If we have a DBField object in $this->record, then return that
2669
        if ($value instanceof DBField) {
2670
            return $value;
2671
        }
2672
2673
        list($class, $spec) = explode('.', $helper);
2674
        /** @var DBField $obj */
2675
        $table = $schema->tableName($class);
2676
        $obj = Injector::inst()->create($spec, $fieldName);
2677
        $obj->setTable($table);
2678
        $obj->setValue($value, $this, false);
2679
        return $obj;
2680
    }
2681
2682
    /**
2683
     * Traverses to a DBField referenced by relationships between data objects.
2684
     *
2685
     * The path to the related field is specified with dot separated syntax
2686
     * (eg: Parent.Child.Child.FieldName).
2687
     *
2688
     * @param string $fieldPath
2689
     *
2690
     * @return mixed DBField of the field on the object or a DataList instance.
2691
     */
2692
    public function relObject($fieldPath)
2693
    {
2694
        $object = null;
2695
2696
        if (strpos($fieldPath, '.') !== false) {
2697
            $parts = explode('.', $fieldPath);
2698
            $fieldName = array_pop($parts);
2699
2700
            // Traverse dot syntax
2701
            $component = $this;
2702
2703
            foreach ($parts as $relation) {
2704
                if ($component instanceof SS_List) {
2705
                    if (method_exists($component, $relation)) {
2706
                        $component = $component->$relation();
2707
                    } else {
2708
                        /** @var DataList $component */
2709
                        $component = $component->relation($relation);
2710
                    }
2711
                } else {
2712
                    $component = $component->$relation();
2713
                }
2714
            }
2715
2716
            $object = $component->dbObject($fieldName);
2717
        } else {
2718
            $object = $this->dbObject($fieldPath);
2719
        }
2720
2721
        return $object;
2722
    }
2723
2724
    /**
2725
     * Traverses to a field referenced by relationships between data objects, returning the value
2726
     * The path to the related field is specified with dot separated syntax (eg: Parent.Child.Child.FieldName)
2727
     *
2728
     * @param $fieldName string
2729
     * @return string | null - will return null on a missing value
2730
     */
2731
    public function relField($fieldName)
2732
    {
2733
        $component = $this;
2734
2735
        // We're dealing with relations here so we traverse the dot syntax
2736
        if (strpos($fieldName, '.') !== false) {
2737
            $relations = explode('.', $fieldName);
2738
            $fieldName = array_pop($relations);
2739
            foreach ($relations as $relation) {
2740
                // Inspect $component for element $relation
2741
                if ($component->hasMethod($relation)) {
2742
                    // Check nested method
2743
                    $component = $component->$relation();
2744
                } elseif ($component instanceof SS_List) {
2745
                    // Select adjacent relation from DataList
2746
                    /** @var DataList $component */
2747
                    $component = $component->relation($relation);
2748
                } elseif ($component instanceof DataObject
2749
                    && ($dbObject = $component->dbObject($relation))
0 ignored issues
show
Bug introduced by
The method dbObject() does not exist on SilverStripe\ORM\FieldType\DBField. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

2749
                    && ($dbObject = $component->/** @scrutinizer ignore-call */ dbObject($relation))
Loading history...
2750
                ) {
2751
                    // Select db object
2752
                    $component = $dbObject;
2753
                } else {
2754
                    user_error("$relation is not a relation/field on ".get_class($component), E_USER_ERROR);
2755
                }
2756
            }
2757
        }
2758
2759
        // Bail if the component is null
2760
        if (!$component) {
2761
            return null;
2762
        }
2763
        if ($component->hasMethod($fieldName)) {
2764
            return $component->$fieldName();
2765
        }
2766
        return $component->$fieldName;
2767
    }
2768
2769
    /**
2770
     * Temporary hack to return an association name, based on class, to get around the mangle
2771
     * of having to deal with reverse lookup of relationships to determine autogenerated foreign keys.
2772
     *
2773
     * @param string $className
2774
     * @return string
2775
     */
2776
    public function getReverseAssociation($className)
2777
    {
2778
        if (is_array($this->manyMany())) {
2779
            $many_many = array_flip($this->manyMany());
2780
            if (array_key_exists($className, $many_many)) {
2781
                return $many_many[$className];
2782
            }
2783
        }
2784
        if (is_array($this->hasMany())) {
2785
            $has_many = array_flip($this->hasMany());
2786
            if (array_key_exists($className, $has_many)) {
2787
                return $has_many[$className];
2788
            }
2789
        }
2790
        if (is_array($this->hasOne())) {
2791
            $has_one = array_flip($this->hasOne());
2792
            if (array_key_exists($className, $has_one)) {
2793
                return $has_one[$className];
2794
            }
2795
        }
2796
2797
        return false;
2798
    }
2799
2800
    /**
2801
     * Return all objects matching the filter
2802
     * sub-classes are automatically selected and included
2803
     *
2804
     * @param string $callerClass The class of objects to be returned
2805
     * @param string|array $filter A filter to be inserted into the WHERE clause.
2806
     * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
2807
     * @param string|array $sort A sort expression to be inserted into the ORDER
2808
     * BY clause.  If omitted, self::$default_sort will be used.
2809
     * @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead.
2810
     * @param string|array $limit A limit expression to be inserted into the LIMIT clause.
2811
     * @param string $containerClass The container class to return the results in.
2812
     *
2813
     * @todo $containerClass is Ignored, why?
2814
     *
2815
     * @return DataList The objects matching the filter, in the class specified by $containerClass
2816
     */
2817
    public static function get(
2818
        $callerClass = null,
2819
        $filter = "",
2820
        $sort = "",
2821
        $join = "",
2822
        $limit = null,
2823
        $containerClass = DataList::class
2824
    ) {
2825
2826
        if ($callerClass == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $callerClass of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
2827
            $callerClass = get_called_class();
2828
            if ($callerClass == self::class) {
2829
                throw new \InvalidArgumentException('Call <classname>::get() instead of DataObject::get()');
2830
            }
2831
2832
            if ($filter || $sort || $join || $limit || ($containerClass != DataList::class)) {
2833
                throw new \InvalidArgumentException('If calling <classname>::get() then you shouldn\'t pass any other'
2834
                    . ' arguments');
2835
            }
2836
2837
            return DataList::create(get_called_class());
0 ignored issues
show
Bug introduced by
get_called_class() of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2837
            return DataList::create(/** @scrutinizer ignore-type */ get_called_class());
Loading history...
2838
        }
2839
2840
        if ($join) {
2841
            throw new \InvalidArgumentException(
2842
                'The $join argument has been removed. Use leftJoin($table, $joinClause) instead.'
2843
            );
2844
        }
2845
2846
        $result = DataList::create($callerClass)->where($filter)->sort($sort);
2847
2848
        if ($limit && strpos($limit, ',') !== false) {
0 ignored issues
show
Bug introduced by
It seems like $limit can also be of type array; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2848
        if ($limit && strpos(/** @scrutinizer ignore-type */ $limit, ',') !== false) {
Loading history...
2849
            $limitArguments = explode(',', $limit);
0 ignored issues
show
Bug introduced by
It seems like $limit can also be of type array; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

2849
            $limitArguments = explode(',', /** @scrutinizer ignore-type */ $limit);
Loading history...
2850
            $result = $result->limit($limitArguments[1], $limitArguments[0]);
2851
        } elseif ($limit) {
2852
            $result = $result->limit($limit);
2853
        }
2854
2855
        return $result;
2856
    }
2857
2858
2859
    /**
2860
     * Return the first item matching the given query.
2861
     * All calls to get_one() are cached.
2862
     *
2863
     * @param string $callerClass The class of objects to be returned
2864
     * @param string|array $filter A filter to be inserted into the WHERE clause.
2865
     * Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
2866
     * @param boolean $cache Use caching
2867
     * @param string $orderby A sort expression to be inserted into the ORDER BY clause.
2868
     *
2869
     * @return DataObject|null The first item matching the query
2870
     */
2871
    public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "")
2872
    {
2873
        $SNG = singleton($callerClass);
2874
2875
        $cacheComponents = array($filter, $orderby, $SNG->extend('cacheKeyComponent'));
2876
        $cacheKey = md5(serialize($cacheComponents));
2877
2878
        $item = null;
2879
        if (!$cache || !isset(self::$_cache_get_one[$callerClass][$cacheKey])) {
2880
            $dl = DataObject::get($callerClass)->where($filter)->sort($orderby);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
2881
            $item = $dl->first();
2882
2883
            if ($cache) {
2884
                self::$_cache_get_one[$callerClass][$cacheKey] = $item;
2885
                if (!self::$_cache_get_one[$callerClass][$cacheKey]) {
2886
                    self::$_cache_get_one[$callerClass][$cacheKey] = false;
2887
                }
2888
            }
2889
        }
2890
2891
        if ($cache) {
2892
            return self::$_cache_get_one[$callerClass][$cacheKey] ?: null;
2893
        } else {
2894
            return $item;
2895
        }
2896
    }
2897
2898
    /**
2899
     * Flush the cached results for all relations (has_one, has_many, many_many)
2900
     * Also clears any cached aggregate data.
2901
     *
2902
     * @param boolean $persistent When true will also clear persistent data stored in the Cache system.
2903
     *                            When false will just clear session-local cached data
2904
     * @return DataObject $this
2905
     */
2906
    public function flushCache($persistent = true)
2907
    {
2908
        if (static::class == self::class) {
2909
            self::$_cache_get_one = array();
2910
            return $this;
2911
        }
2912
2913
        $classes = ClassInfo::ancestry(static::class);
2914
        foreach ($classes as $class) {
2915
            if (isset(self::$_cache_get_one[$class])) {
2916
                unset(self::$_cache_get_one[$class]);
2917
            }
2918
        }
2919
2920
        $this->extend('flushCache');
2921
2922
        $this->components = array();
2923
        return $this;
2924
    }
2925
2926
    /**
2927
     * Flush the get_one global cache and destroy associated objects.
2928
     */
2929
    public static function flush_and_destroy_cache()
2930
    {
2931
        if (self::$_cache_get_one) {
0 ignored issues
show
Bug Best Practice introduced by
The expression self::_cache_get_one of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
2932
            foreach (self::$_cache_get_one as $class => $items) {
2933
                if (is_array($items)) {
2934
                    foreach ($items as $item) {
2935
                        if ($item) {
2936
                            $item->destroy();
2937
                        }
2938
                    }
2939
                }
2940
            }
2941
        }
2942
        self::$_cache_get_one = array();
2943
    }
2944
2945
    /**
2946
     * Reset all global caches associated with DataObject.
2947
     */
2948
    public static function reset()
2949
    {
2950
        // @todo Decouple these
2951
        DBClassName::clear_classname_cache();
2952
        ClassInfo::reset_db_cache();
2953
        static::getSchema()->reset();
2954
        self::$_cache_get_one = array();
2955
        self::$_cache_field_labels = array();
2956
    }
2957
2958
    /**
2959
     * Return the given element, searching by ID
2960
     *
2961
     * @param string $callerClass The class of the object to be returned
2962
     * @param int $id The id of the element
2963
     * @param boolean $cache See {@link get_one()}
2964
     *
2965
     * @return DataObject The element
2966
     */
2967
    public static function get_by_id($callerClass, $id, $cache = true)
2968
    {
2969
        if (!is_numeric($id)) {
2970
            user_error("DataObject::get_by_id passed a non-numeric ID #$id", E_USER_WARNING);
2971
        }
2972
2973
        // Pass to get_one
2974
        $column = static::getSchema()->sqlColumnForField($callerClass, 'ID');
2975
        return DataObject::get_one($callerClass, array($column => $id), $cache);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
2976
    }
2977
2978
    /**
2979
     * Get the name of the base table for this object
2980
     *
2981
     * @return string
2982
     */
2983
    public function baseTable()
2984
    {
2985
        return static::getSchema()->baseDataTable($this);
2986
    }
2987
2988
    /**
2989
     * Get the base class for this object
2990
     *
2991
     * @return string
2992
     */
2993
    public function baseClass()
2994
    {
2995
        return static::getSchema()->baseDataClass($this);
2996
    }
2997
2998
    /**
2999
     * @var array Parameters used in the query that built this object.
3000
     * This can be used by decorators (e.g. lazy loading) to
3001
     * run additional queries using the same context.
3002
     */
3003
    protected $sourceQueryParams;
3004
3005
    /**
3006
     * @see $sourceQueryParams
3007
     * @return array
3008
     */
3009
    public function getSourceQueryParams()
3010
    {
3011
        return $this->sourceQueryParams;
3012
    }
3013
3014
    /**
3015
     * Get list of parameters that should be inherited to relations on this object
3016
     *
3017
     * @return array
3018
     */
3019
    public function getInheritableQueryParams()
3020
    {
3021
        $params = $this->getSourceQueryParams();
3022
        $this->extend('updateInheritableQueryParams', $params);
3023
        return $params;
3024
    }
3025
3026
    /**
3027
     * @see $sourceQueryParams
3028
     * @param array
3029
     */
3030
    public function setSourceQueryParams($array)
3031
    {
3032
        $this->sourceQueryParams = $array;
3033
    }
3034
3035
    /**
3036
     * @see $sourceQueryParams
3037
     * @param string $key
3038
     * @param string $value
3039
     */
3040
    public function setSourceQueryParam($key, $value)
3041
    {
3042
        $this->sourceQueryParams[$key] = $value;
3043
    }
3044
3045
    /**
3046
     * @see $sourceQueryParams
3047
     * @param string $key
3048
     * @return string
3049
     */
3050
    public function getSourceQueryParam($key)
3051
    {
3052
        if (isset($this->sourceQueryParams[$key])) {
3053
            return $this->sourceQueryParams[$key];
3054
        }
3055
        return null;
3056
    }
3057
3058
    //-------------------------------------------------------------------------------------------//
3059
3060
    /**
3061
     * Check the database schema and update it as necessary.
3062
     *
3063
     * @uses DataExtension->augmentDatabase()
3064
     */
3065
    public function requireTable()
3066
    {
3067
        // Only build the table if we've actually got fields
3068
        $schema = static::getSchema();
3069
        $table = $schema->tableName(static::class);
3070
        $fields = $schema->databaseFields(static::class, false);
3071
        $indexes = $schema->databaseIndexes(static::class, false);
3072
        $extensions = self::database_extensions(static::class);
0 ignored issues
show
Bug Best Practice introduced by
The method SilverStripe\ORM\DataObject::database_extensions() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

3072
        /** @scrutinizer ignore-call */ 
3073
        $extensions = self::database_extensions(static::class);
Loading history...
3073
3074
        if (empty($table)) {
3075
            throw new LogicException(
3076
                "Class " . static::class . " not loaded by manifest, or no database table configured"
3077
            );
3078
        }
3079
3080
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
3081
            $hasAutoIncPK = get_parent_class($this) === self::class;
3082
            DB::require_table(
3083
                $table,
3084
                $fields,
3085
                $indexes,
3086
                $hasAutoIncPK,
3087
                $this->config()->get('create_table_options'),
3088
                $extensions
3089
            );
3090
        } else {
3091
            DB::dont_require_table($table);
3092
        }
3093
3094
        // Build any child tables for many_many items
3095
        if ($manyMany = $this->uninherited('many_many')) {
3096
            $extras = $this->uninherited('many_many_extraFields');
3097
            foreach ($manyMany as $component => $spec) {
3098
                // Get many_many spec
3099
                $manyManyComponent = $schema->manyManyComponent(static::class, $component);
3100
                $parentField = $manyManyComponent['parentField'];
3101
                $childField = $manyManyComponent['childField'];
3102
                $tableOrClass = $manyManyComponent['join'];
3103
3104
                // Skip if backed by actual class
3105
                if (class_exists($tableOrClass)) {
3106
                    continue;
3107
                }
3108
3109
                // Build fields
3110
                $manymanyFields = array(
3111
                    $parentField => "Int",
3112
                    $childField => "Int",
3113
                );
3114
                if (isset($extras[$component])) {
3115
                    $manymanyFields = array_merge($manymanyFields, $extras[$component]);
3116
                }
3117
3118
                // Build index list
3119
                $manymanyIndexes = [
3120
                    $parentField => [
3121
                        'type' => 'index',
3122
                        'name' => $parentField,
3123
                        'columns' => [$parentField],
3124
                    ],
3125
                    $childField => [
3126
                        'type' => 'index',
3127
                        'name' =>$childField,
3128
                        'columns' => [$childField],
3129
                    ],
3130
                ];
3131
                DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, $extensions);
0 ignored issues
show
Bug introduced by
$manymanyIndexes of type array<mixed,array<string,mixed|string|array>> is incompatible with the type string expected by parameter $indexSchema of SilverStripe\ORM\DB::require_table(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3131
                DB::require_table($tableOrClass, $manymanyFields, /** @scrutinizer ignore-type */ $manymanyIndexes, true, null, $extensions);
Loading history...
Bug introduced by
$extensions of type false is incompatible with the type array expected by parameter $extensions of SilverStripe\ORM\DB::require_table(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3131
                DB::require_table($tableOrClass, $manymanyFields, $manymanyIndexes, true, null, /** @scrutinizer ignore-type */ $extensions);
Loading history...
Bug introduced by
$manymanyFields of type string[]|array is incompatible with the type string expected by parameter $fieldSchema of SilverStripe\ORM\DB::require_table(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

3131
                DB::require_table($tableOrClass, /** @scrutinizer ignore-type */ $manymanyFields, $manymanyIndexes, true, null, $extensions);
Loading history...
3132
            }
3133
        }
3134
3135
        // Let any extentions make their own database fields
3136
        $this->extend('augmentDatabase', $dummy);
3137
    }
3138
3139
    /**
3140
     * Add default records to database. This function is called whenever the
3141
     * database is built, after the database tables have all been created. Overload
3142
     * this to add default records when the database is built, but make sure you
3143
     * call parent::requireDefaultRecords().
3144
     *
3145
     * @uses DataExtension->requireDefaultRecords()
3146
     */
3147
    public function requireDefaultRecords()
3148
    {
3149
        $defaultRecords = $this->config()->uninherited('default_records');
3150
3151
        if (!empty($defaultRecords)) {
3152
            $hasData = DataObject::get_one(static::class);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
3153
            if (!$hasData) {
3154
                $className = static::class;
3155
                foreach ($defaultRecords as $record) {
3156
                    $obj = Injector::inst()->create($className, $record);
3157
                    $obj->write();
3158
                }
3159
                DB::alteration_message("Added default records to $className table", "created");
3160
            }
3161
        }
3162
3163
        // Let any extentions make their own database default data
3164
        $this->extend('requireDefaultRecords', $dummy);
3165
    }
3166
3167
    /**
3168
     * Get the default searchable fields for this object, as defined in the
3169
     * $searchable_fields list. If searchable fields are not defined on the
3170
     * data object, uses a default selection of summary fields.
3171
     *
3172
     * @return array
3173
     */
3174
    public function searchableFields()
3175
    {
3176
        // can have mixed format, need to make consistent in most verbose form
3177
        $fields = $this->config()->get('searchable_fields');
3178
        $labels = $this->fieldLabels();
3179
3180
        // fallback to summary fields (unless empty array is explicitly specified)
3181
        if (! $fields && ! is_array($fields)) {
3182
            $summaryFields = array_keys($this->summaryFields());
3183
            $fields = array();
3184
3185
            // remove the custom getters as the search should not include them
3186
            $schema = static::getSchema();
3187
            if ($summaryFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $summaryFields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
3188
                foreach ($summaryFields as $key => $name) {
3189
                    $spec = $name;
3190
3191
                    // Extract field name in case this is a method called on a field (e.g. "Date.Nice")
3192
                    if (($fieldPos = strpos($name, '.')) !== false) {
3193
                        $name = substr($name, 0, $fieldPos);
3194
                    }
3195
3196
                    if ($schema->fieldSpec($this, $name)) {
3197
                        $fields[] = $name;
3198
                    } elseif ($this->relObject($spec)) {
3199
                        $fields[] = $spec;
3200
                    }
3201
                }
3202
            }
3203
        }
3204
3205
        // we need to make sure the format is unified before
3206
        // augmenting fields, so extensions can apply consistent checks
3207
        // but also after augmenting fields, because the extension
3208
        // might use the shorthand notation as well
3209
3210
        // rewrite array, if it is using shorthand syntax
3211
        $rewrite = array();
3212
        foreach ($fields as $name => $specOrName) {
3213
            $identifer = (is_int($name)) ? $specOrName : $name;
3214
3215
            if (is_int($name)) {
3216
                // Format: array('MyFieldName')
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3217
                $rewrite[$identifer] = array();
3218
            } elseif (is_array($specOrName)) {
3219
                // Format: array('MyFieldName' => array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3220
                //   'filter => 'ExactMatchFilter',
3221
                //   'field' => 'NumericField', // optional
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3222
                //   'title' => 'My Title', // optional
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3223
                // ))
3224
                $rewrite[$identifer] = array_merge(
3225
                    array('filter' => $this->relObject($identifer)->config()->get('default_search_filter_class')),
3226
                    (array)$specOrName
3227
                );
3228
            } else {
3229
                // Format: array('MyFieldName' => 'ExactMatchFilter')
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
3230
                $rewrite[$identifer] = array(
3231
                    'filter' => $specOrName,
3232
                );
3233
            }
3234
            if (!isset($rewrite[$identifer]['title'])) {
3235
                $rewrite[$identifer]['title'] = (isset($labels[$identifer]))
3236
                    ? $labels[$identifer] : FormField::name_to_label($identifer);
3237
            }
3238
            if (!isset($rewrite[$identifer]['filter'])) {
3239
                /** @skipUpgrade */
3240
                $rewrite[$identifer]['filter'] = 'PartialMatchFilter';
3241
            }
3242
        }
3243
3244
        $fields = $rewrite;
3245
3246
        // apply DataExtensions if present
3247
        $this->extend('updateSearchableFields', $fields);
3248
3249
        return $fields;
3250
    }
3251
3252
    /**
3253
     * Get any user defined searchable fields labels that
3254
     * exist. Allows overriding of default field names in the form
3255
     * interface actually presented to the user.
3256
     *
3257
     * The reason for keeping this separate from searchable_fields,
3258
     * which would be a logical place for this functionality, is to
3259
     * avoid bloating and complicating the configuration array. Currently
3260
     * much of this system is based on sensible defaults, and this property
3261
     * would generally only be set in the case of more complex relationships
3262
     * between data object being required in the search interface.
3263
     *
3264
     * Generates labels based on name of the field itself, if no static property
3265
     * {@link self::field_labels} exists.
3266
     *
3267
     * @uses $field_labels
3268
     * @uses FormField::name_to_label()
3269
     *
3270
     * @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
3271
     *
3272
     * @return array|string Array of all element labels if no argument given, otherwise the label of the field
3273
     */
3274
    public function fieldLabels($includerelations = true)
3275
    {
3276
        $cacheKey = static::class . '_' . $includerelations;
3277
3278
        if (!isset(self::$_cache_field_labels[$cacheKey])) {
3279
            $customLabels = $this->config()->get('field_labels');
3280
            $autoLabels = array();
3281
3282
            // get all translated static properties as defined in i18nCollectStatics()
3283
            $ancestry = ClassInfo::ancestry(static::class);
3284
            $ancestry = array_reverse($ancestry);
3285
            if ($ancestry) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ancestry of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
3286
                foreach ($ancestry as $ancestorClass) {
3287
                    if ($ancestorClass === ViewableData::class) {
3288
                        break;
3289
                    }
3290
                    $types = [
3291
                        'db' => (array)Config::inst()->get($ancestorClass, 'db', Config::UNINHERITED)
3292
                    ];
3293
                    if ($includerelations) {
3294
                        $types['has_one'] = (array)Config::inst()->get($ancestorClass, 'has_one', Config::UNINHERITED);
3295
                        $types['has_many'] = (array)Config::inst()->get($ancestorClass, 'has_many', Config::UNINHERITED);
3296
                        $types['many_many'] = (array)Config::inst()->get($ancestorClass, 'many_many', Config::UNINHERITED);
3297
                        $types['belongs_many_many'] = (array)Config::inst()->get($ancestorClass, 'belongs_many_many', Config::UNINHERITED);
3298
                    }
3299
                    foreach ($types as $type => $attrs) {
3300
                        foreach ($attrs as $name => $spec) {
3301
                            $autoLabels[$name] = _t("{$ancestorClass}.{$type}_{$name}", FormField::name_to_label($name));
3302
                        }
3303
                    }
3304
                }
3305
            }
3306
3307
            $labels = array_merge((array)$autoLabels, (array)$customLabels);
3308
            $this->extend('updateFieldLabels', $labels);
3309
            self::$_cache_field_labels[$cacheKey] = $labels;
3310
        }
3311
3312
        return self::$_cache_field_labels[$cacheKey];
3313
    }
3314
3315
    /**
3316
     * Get a human-readable label for a single field,
3317
     * see {@link fieldLabels()} for more details.
3318
     *
3319
     * @uses fieldLabels()
3320
     * @uses FormField::name_to_label()
3321
     *
3322
     * @param string $name Name of the field
3323
     * @return string Label of the field
3324
     */
3325
    public function fieldLabel($name)
3326
    {
3327
        $labels = $this->fieldLabels();
3328
        return (isset($labels[$name])) ? $labels[$name] : FormField::name_to_label($name);
3329
    }
3330
3331
    /**
3332
     * Get the default summary fields for this object.
3333
     *
3334
     * @todo use the translation apparatus to return a default field selection for the language
3335
     *
3336
     * @return array
3337
     */
3338
    public function summaryFields()
3339
    {
3340
        $fields = $this->config()->get('summary_fields');
3341
3342
        // if fields were passed in numeric array,
3343
        // convert to an associative array
3344
        if ($fields && array_key_exists(0, $fields)) {
3345
            $fields = array_combine(array_values($fields), array_values($fields));
3346
        }
3347
3348
        if (!$fields) {
3349
            $fields = array();
3350
            // try to scaffold a couple of usual suspects
3351
            if ($this->hasField('Name')) {
3352
                $fields['Name'] = 'Name';
3353
            }
3354
            if (static::getSchema()->fieldSpec($this, 'Title')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::getSchema()->fieldSpec($this, 'Title') of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
3355
                $fields['Title'] = 'Title';
3356
            }
3357
            if ($this->hasField('Description')) {
3358
                $fields['Description'] = 'Description';
3359
            }
3360
            if ($this->hasField('FirstName')) {
3361
                $fields['FirstName'] = 'First Name';
3362
            }
3363
        }
3364
        $this->extend("updateSummaryFields", $fields);
3365
3366
        // Final fail-over, just list ID field
3367
        if (!$fields) {
3368
            $fields['ID'] = 'ID';
3369
        }
3370
3371
        // Localize fields (if possible)
3372
        foreach ($this->fieldLabels(false) as $name => $label) {
3373
            // only attempt to localize if the label definition is the same as the field name.
3374
            // this will preserve any custom labels set in the summary_fields configuration
3375
            if (isset($fields[$name]) && $name === $fields[$name]) {
3376
                $fields[$name] = $label;
3377
            }
3378
        }
3379
3380
        return $fields;
3381
    }
3382
3383
    /**
3384
     * Defines a default list of filters for the search context.
3385
     *
3386
     * If a filter class mapping is defined on the data object,
3387
     * it is constructed here. Otherwise, the default filter specified in
3388
     * {@link DBField} is used.
3389
     *
3390
     * @todo error handling/type checking for valid FormField and SearchFilter subclasses?
3391
     *
3392
     * @return array
3393
     */
3394
    public function defaultSearchFilters()
3395
    {
3396
        $filters = array();
3397
3398
        foreach ($this->searchableFields() as $name => $spec) {
3399
            if (empty($spec['filter'])) {
3400
                /** @skipUpgrade */
3401
                $filters[$name] = 'PartialMatchFilter';
3402
            } elseif ($spec['filter'] instanceof SearchFilter) {
3403
                $filters[$name] = $spec['filter'];
3404
            } else {
3405
                $filters[$name] = Injector::inst()->create($spec['filter'], $name);
3406
            }
3407
        }
3408
3409
        return $filters;
3410
    }
3411
3412
    /**
3413
     * @return boolean True if the object is in the database
3414
     */
3415
    public function isInDB()
3416
    {
3417
        return is_numeric($this->ID) && $this->ID > 0;
3418
    }
3419
3420
    /*
3421
	 * @ignore
3422
	 */
3423
    private static $subclass_access = true;
3424
3425
    /**
3426
     * Temporarily disable subclass access in data object qeur
3427
     */
3428
    public static function disable_subclass_access()
3429
    {
3430
        self::$subclass_access = false;
3431
    }
3432
    public static function enable_subclass_access()
3433
    {
3434
        self::$subclass_access = true;
3435
    }
3436
3437
    //-------------------------------------------------------------------------------------------//
3438
3439
    /**
3440
     * Database field definitions.
3441
     * This is a map from field names to field type. The field
3442
     * type should be a class that extends .
3443
     * @var array
3444
     * @config
3445
     */
3446
    private static $db = [];
3447
3448
    /**
3449
     * Use a casting object for a field. This is a map from
3450
     * field name to class name of the casting object.
3451
     *
3452
     * @var array
3453
     */
3454
    private static $casting = array(
3455
        "Title" => 'Text',
3456
    );
3457
3458
    /**
3459
     * Specify custom options for a CREATE TABLE call.
3460
     * Can be used to specify a custom storage engine for specific database table.
3461
     * All options have to be keyed for a specific database implementation,
3462
     * identified by their class name (extending from {@link SS_Database}).
3463
     *
3464
     * <code>
3465
     * array(
3466
     *  'MySQLDatabase' => 'ENGINE=MyISAM'
3467
     * )
3468
     * </code>
3469
     *
3470
     * Caution: This API is experimental, and might not be
3471
     * included in the next major release. Please use with care.
3472
     *
3473
     * @var array
3474
     * @config
3475
     */
3476
    private static $create_table_options = array(
3477
        MySQLSchemaManager::ID => 'ENGINE=InnoDB'
3478
    );
3479
3480
    /**
3481
     * If a field is in this array, then create a database index
3482
     * on that field. This is a map from fieldname to index type.
3483
     * See {@link SS_Database->requireIndex()} and custom subclasses for details on the array notation.
3484
     *
3485
     * @var array
3486
     * @config
3487
     */
3488
    private static $indexes = null;
3489
3490
    /**
3491
     * Inserts standard column-values when a DataObject
3492
     * is instanciated. Does not insert default records {@see $default_records}.
3493
     * This is a map from fieldname to default value.
3494
     *
3495
     *  - If you would like to change a default value in a sub-class, just specify it.
3496
     *  - If you would like to disable the default value given by a parent class, set the default value to 0,'',
3497
     *    or false in your subclass.  Setting it to null won't work.
3498
     *
3499
     * @var array
3500
     * @config
3501
     */
3502
    private static $defaults = [];
3503
3504
    /**
3505
     * Multidimensional array which inserts default data into the database
3506
     * on a db/build-call as long as the database-table is empty. Please use this only
3507
     * for simple constructs, not for SiteTree-Objects etc. which need special
3508
     * behaviour such as publishing and ParentNodes.
3509
     *
3510
     * Example:
3511
     * array(
3512
     *  array('Title' => "DefaultPage1", 'PageTitle' => 'page1'),
3513
     *  array('Title' => "DefaultPage2")
3514
     * ).
3515
     *
3516
     * @var array
3517
     * @config
3518
     */
3519
    private static $default_records = null;
3520
3521
    /**
3522
     * One-to-zero relationship defintion. This is a map of component name to data type. In order to turn this into a
3523
     * true one-to-one relationship you can add a {@link DataObject::$belongs_to} relationship on the child class.
3524
     *
3525
     * Note that you cannot have a has_one and belongs_to relationship with the same name.
3526
     *
3527
     *  @var array
3528
     * @config
3529
     */
3530
    private static $has_one = [];
3531
3532
    /**
3533
     * A meta-relationship that allows you to define the reverse side of a {@link DataObject::$has_one}.
3534
     *
3535
     * This does not actually create any data structures, but allows you to query the other object in a one-to-one
3536
     * relationship from the child object. If you have multiple belongs_to links to another object you can use the
3537
     * syntax "ClassName.HasOneName" to specify which foreign has_one key on the other object to use.
3538
     *
3539
     * Note that you cannot have a has_one and belongs_to relationship with the same name.
3540
     *
3541
     * @var array
3542
     * @config
3543
     */
3544
    private static $belongs_to = [];
3545
3546
    /**
3547
     * This defines a one-to-many relationship. It is a map of component name to the remote data class.
3548
     *
3549
     * This relationship type does not actually create a data structure itself - you need to define a matching $has_one
3550
     * relationship on the child class. Also, if the $has_one relationship on the child class has multiple links to this
3551
     * class you can use the syntax "ClassName.HasOneRelationshipName" in the remote data class definition to show
3552
     * which foreign key to use.
3553
     *
3554
     * @var array
3555
     * @config
3556
     */
3557
    private static $has_many = [];
3558
3559
    /**
3560
     * many-many relationship definitions.
3561
     * This is a map from component name to data type.
3562
     * @var array
3563
     * @config
3564
     */
3565
    private static $many_many = [];
3566
3567
    /**
3568
     * Extra fields to include on the connecting many-many table.
3569
     * This is a map from field name to field type.
3570
     *
3571
     * Example code:
3572
     * <code>
3573
     * public static $many_many_extraFields = array(
3574
     *  'Members' => array(
3575
     *          'Role' => 'Varchar(100)'
3576
     *      )
3577
     * );
3578
     * </code>
3579
     *
3580
     * @var array
3581
     * @config
3582
     */
3583
    private static $many_many_extraFields = [];
3584
3585
    /**
3586
     * The inverse side of a many-many relationship.
3587
     * This is a map from component name to data type.
3588
     * @var array
3589
     * @config
3590
     */
3591
    private static $belongs_many_many = [];
3592
3593
    /**
3594
     * The default sort expression. This will be inserted in the ORDER BY
3595
     * clause of a SQL query if no other sort expression is provided.
3596
     * @var string
3597
     * @config
3598
     */
3599
    private static $default_sort = null;
3600
3601
    /**
3602
     * Default list of fields that can be scaffolded by the ModelAdmin
3603
     * search interface.
3604
     *
3605
     * Overriding the default filter, with a custom defined filter:
3606
     * <code>
3607
     *  static $searchable_fields = array(
3608
     *     "Name" => "PartialMatchFilter"
3609
     *  );
3610
     * </code>
3611
     *
3612
     * Overriding the default form fields, with a custom defined field.
3613
     * The 'filter' parameter will be generated from {@link DBField::$default_search_filter_class}.
3614
     * The 'title' parameter will be generated from {@link DataObject->fieldLabels()}.
3615
     * <code>
3616
     *  static $searchable_fields = array(
3617
     *    "Name" => array(
3618
     *      "field" => "TextField"
3619
     *    )
3620
     *  );
3621
     * </code>
3622
     *
3623
     * Overriding the default form field, filter and title:
3624
     * <code>
3625
     *  static $searchable_fields = array(
3626
     *    "Organisation.ZipCode" => array(
3627
     *      "field" => "TextField",
3628
     *      "filter" => "PartialMatchFilter",
3629
     *      "title" => 'Organisation ZIP'
3630
     *    )
3631
     *  );
3632
     * </code>
3633
     * @config
3634
     */
3635
    private static $searchable_fields = null;
3636
3637
    /**
3638
     * User defined labels for searchable_fields, used to override
3639
     * default display in the search form.
3640
     * @config
3641
     */
3642
    private static $field_labels = [];
3643
3644
    /**
3645
     * Provides a default list of fields to be used by a 'summary'
3646
     * view of this object.
3647
     * @config
3648
     */
3649
    private static $summary_fields = [];
3650
3651
    public function provideI18nEntities()
3652
    {
3653
        // Note: see http://guides.rubyonrails.org/i18n.html#pluralization for rules
3654
        // Best guess for a/an rule. Better guesses require overriding in subclasses
3655
        $pluralName = $this->plural_name();
3656
        $singularName = $this->singular_name();
3657
        $conjunction = preg_match('/^[aeiou]/i', $singularName) ? 'An ' : 'A ';
3658
        return [
3659
            static::class.'.SINGULARNAME' => $this->singular_name(),
3660
            static::class.'.PLURALNAME' => $pluralName,
3661
            static::class.'.PLURALS' => [
3662
                'one' => $conjunction . $singularName,
3663
                'other' => '{count} ' . $pluralName
3664
            ]
3665
        ];
3666
    }
3667
3668
    /**
3669
     * Returns true if the given method/parameter has a value
3670
     * (Uses the DBField::hasValue if the parameter is a database field)
3671
     *
3672
     * @param string $field The field name
3673
     * @param array $arguments
3674
     * @param bool $cache
3675
     * @return boolean
3676
     */
3677
    public function hasValue($field, $arguments = null, $cache = true)
3678
    {
3679
        // has_one fields should not use dbObject to check if a value is given
3680
        $hasOne = static::getSchema()->hasOneComponent(static::class, $field);
3681
        if (!$hasOne && ($obj = $this->dbObject($field))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $hasOne of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
3682
            return $obj->exists();
3683
        } else {
3684
            return parent::hasValue($field, $arguments, $cache);
3685
        }
3686
    }
3687
3688
    /**
3689
     * If selected through a many_many through relation, this is the instance of the joined record
3690
     *
3691
     * @return DataObject
3692
     */
3693
    public function getJoin()
3694
    {
3695
        return $this->joinRecord;
3696
    }
3697
3698
    /**
3699
     * Set joining object
3700
     *
3701
     * @param DataObject $object
3702
     * @param string $alias Alias
3703
     * @return $this
3704
     */
3705
    public function setJoin(DataObject $object, $alias = null)
3706
    {
3707
        $this->joinRecord = $object;
3708
        if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
3709
            if (static::getSchema()->fieldSpec(static::class, $alias)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::getSchema()->fie...(static::class, $alias) of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
3710
                throw new InvalidArgumentException(
3711
                    "Joined record $alias cannot also be a db field"
3712
                );
3713
            }
3714
            $this->record[$alias] = $object;
3715
        }
3716
        return $this;
3717
    }
3718
3719
    /**
3720
     * Find objects in the given relationships, merging them into the given list
3721
     *
3722
     * @param string $source Config property to extract relationships from
3723
     * @param bool $recursive True if recursive
3724
     * @param ArrayList $list If specified, items will be added to this list. If not, a new
3725
     * instance of ArrayList will be constructed and returned
3726
     * @return ArrayList The list of related objects
3727
     */
3728
    public function findRelatedObjects($source, $recursive = true, $list = null)
3729
    {
3730
        if (!$list) {
3731
            $list = new ArrayList();
3732
        }
3733
3734
        // Skip search for unsaved records
3735
        if (!$this->isInDB()) {
3736
            return $list;
3737
        }
3738
3739
        $relationships = $this->config()->get($source) ?: [];
3740
        foreach ($relationships as $relationship) {
3741
            // Warn if invalid config
3742
            if (!$this->hasMethod($relationship)) {
3743
                trigger_error(sprintf(
3744
                    "Invalid %s config value \"%s\" on object on class \"%s\"",
3745
                    $source,
3746
                    $relationship,
3747
                    get_class($this)
3748
                ), E_USER_WARNING);
3749
                continue;
3750
            }
3751
3752
            // Inspect value of this relationship
3753
            $items = $this->{$relationship}();
3754
3755
            // Merge any new item
3756
            $newItems = $this->mergeRelatedObjects($list, $items);
3757
3758
            // Recurse if necessary
3759
            if ($recursive) {
3760
                foreach ($newItems as $item) {
3761
                    /** @var DataObject $item */
3762
                    $item->findRelatedObjects($source, true, $list);
3763
                }
3764
            }
3765
        }
3766
        return $list;
3767
    }
3768
3769
    /**
3770
     * Helper method to merge owned/owning items into a list.
3771
     * Items already present in the list will be skipped.
3772
     *
3773
     * @param ArrayList $list Items to merge into
3774
     * @param mixed $items List of new items to merge
3775
     * @return ArrayList List of all newly added items that did not already exist in $list
3776
     */
3777
    public function mergeRelatedObjects($list, $items)
3778
    {
3779
        $added = new ArrayList();
3780
        if (!$items) {
3781
            return $added;
3782
        }
3783
        if ($items instanceof DataObject) {
3784
            $items = [$items];
3785
        }
3786
3787
        /** @var DataObject $item */
3788
        foreach ($items as $item) {
3789
            $this->mergeRelatedObject($list, $added, $item);
3790
        }
3791
        return $added;
3792
    }
3793
3794
    /**
3795
     * Merge single object into a list, but ensures that existing objects are not
3796
     * re-added.
3797
     *
3798
     * @param ArrayList $list Global list
3799
     * @param ArrayList $added Additional list to insert into
3800
     * @param DataObject $item Item to add
3801
     */
3802
    protected function mergeRelatedObject($list, $added, $item)
3803
    {
3804
        // Identify item
3805
        $itemKey = get_class($item) . '/' . $item->ID;
3806
3807
        // Write if saved, versioned, and not already added
3808
        if ($item->isInDB() && !isset($list[$itemKey])) {
3809
            $list[$itemKey] = $item;
3810
            $added[$itemKey] = $item;
3811
        }
3812
3813
        // Add joined record (from many_many through) automatically
3814
        $joined = $item->getJoin();
3815
        if ($joined) {
3816
            $this->mergeRelatedObject($list, $added, $joined);
3817
        }
3818
    }
3819
}
3820