-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathHasOne.php
More file actions
134 lines (108 loc) · 4.79 KB
/
HasOne.php
File metadata and controls
134 lines (108 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Atk4\Data\Reference;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Data\Reference;
class HasOne extends Reference
{
use Model\FieldPropertiesTrait;
use Model\JoinLinkTrait;
/**
* Reference\HasOne will also add a field corresponding
* to 'ourField' unless it exists.
*/
protected function init(): void
{
parent::init();
if (!$this->ourField) {
$this->ourField = $this->link;
}
// for references use "integer" as a default type
if (!(new \ReflectionProperty($this, 'type'))->isInitialized($this)) {
$this->type = 'integer';
}
$this->referenceLink = $this->link; // named differently than in Model\FieldPropertiesTrait
$fieldPropsRefl = (new \ReflectionClass(Model\FieldPropertiesTrait::class))->getProperties();
$fieldPropsRefl[] = (new \ReflectionClass(Model\JoinLinkTrait::class))->getProperty('joinName');
$ourModel = $this->getOurModel(null);
if (!$ourModel->hasField($this->ourField)) {
$fieldSeed = [];
foreach ($fieldPropsRefl as $fieldPropRefl) {
$v = $this->{$fieldPropRefl->getName()};
$vDefault = \PHP_MAJOR_VERSION < 8
? ($fieldPropRefl->getDeclaringClass()->getDefaultProperties()[$fieldPropRefl->getName()] ?? null)
: (null ?? $fieldPropRefl->getDefaultValue()); // @phpstan-ignore-line for PHP 7.x
if ($v !== $vDefault) {
$fieldSeed[$fieldPropRefl->getName()] = $v;
}
}
$ourModel->addField($this->ourField, $fieldSeed);
}
// TODO seeding thru Model\FieldPropertiesTrait is a hack, at least unset these properties for now
foreach ($fieldPropsRefl as $fieldPropRefl) {
if ($fieldPropRefl->getName() !== 'caption') { // "caption" is also defined in Reference class
unset($this->{$fieldPropRefl->getName()});
}
}
}
/**
* Returns our field or id field.
*/
protected function referenceOurValue(): Field
{
// TODO horrible hack to render the field with a table prefix,
// find a solution how to wrap the field inside custom Field (without owner?)
$ourModelCloned = clone $this->getOurModel(null);
$ourModelCloned->persistenceData['use_table_prefixes'] = true;
return $ourModelCloned->getReference($this->link)->getOurField();
}
/**
* If our model is loaded, then return their model with respective record loaded.
*
* If our model is not loaded, then return their model with condition set.
* This can happen in case of deep traversal $model->ref('Many')->ref('one_id'), for example.
*/
public function ref(Model $ourModel, array $defaults = []): Model
{
$ourModel = $this->getOurModel($ourModel);
$theirModel = $this->createTheirModel($defaults);
if ($ourModel->isEntity()) {
$ourValue = $this->getOurFieldValue($ourModel);
if ($this->getOurFieldName() === $ourModel->idField) {
$this->assertReferenceValueNotNull($ourValue);
$tryLoad = true;
} else {
$tryLoad = false;
}
$theirModelOrig = $theirModel;
if ($ourValue === null) {
$theirModel = null;
} else {
$theirModel->addCondition($this->getTheirFieldName($theirModel), $ourValue);
$theirModel = $tryLoad
? $theirModel->tryLoadOne()
: $theirModel->loadOne();
}
if ($theirModel === null) {
$theirModel = $theirModelOrig->createEntity();
}
}
// their model will be reloaded after saving our model to reflect changes in referenced fields
$theirModel->getModel(true)->reloadAfterSave = false;
if ($ourModel->isEntity()) {
$this->onHookToTheirModel($theirModel, Model::HOOK_AFTER_SAVE, function (Model $theirModel) use ($ourModel) {
$theirValue = $this->theirField ? $theirModel->get($this->theirField) : $theirModel->getId();
if (!$this->getOurField()->compare($this->getOurFieldValue($ourModel), $theirValue)) {
$ourModel->set($this->getOurFieldName(), $theirValue)->save();
}
$theirModel->reload();
});
// add hook to set our field = null when record of referenced model is deleted
$this->onHookToTheirModel($theirModel, Model::HOOK_AFTER_DELETE, function (Model $theirModel) use ($ourModel) {
$ourModel->setNull($this->getOurFieldName());
});
}
return $theirModel;
}
}