-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathHasOne.php
More file actions
146 lines (117 loc) · 5.12 KB
/
HasOne.php
File metadata and controls
146 lines (117 loc) · 5.12 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
135
136
137
138
139
140
141
142
143
144
145
146
<?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;
protected const HOOK_PRIORITY_EARLY = -500;
#[\Override]
protected function init(): void
{
parent::init();
if ($this->ourField === null) {
$this->ourField = $this->link;
}
$checkTheirTypeOrig = $this->checkTheirType;
$this->checkTheirType = false;
try {
$analysingTheirModel = $this->createAnalysingTheirModel();
} finally {
$this->checkTheirType = $checkTheirTypeOrig;
}
// infer our field type from their field
if (($this->type ?? null) === null) {
$this->type = $analysingTheirModel->getField($this->getTheirFieldName($analysingTheirModel))->type;
}
$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();
if (!$ourModel->hasField($this->ourField)) {
$fieldSeed = [];
foreach ($fieldPropsRefl as $fieldPropRefl) {
$v = $this->{$fieldPropRefl->getName()};
$vDefault = \PHP_MAJOR_VERSION === 7
? ($fieldPropRefl->getDeclaringClass()->getDefaultProperties()[$fieldPropRefl->getName()] ?? null)
: (null ?? ($fieldPropRefl->hasDefaultValue() ? $fieldPropRefl->getDefaultValue() : null)); // @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()});
}
}
}
#[\Override]
public function isOneToOne(): bool
{
return true;
}
/**
* 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();
$ourModelCloned->persistenceData['use_table_prefixes'] = true;
return $ourModelCloned->getReference($this->link)->getOurField();
}
/**
* If our model is loaded, then return their model with respective loaded entity.
*
* 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.
*/
#[\Override]
public function ref(Model $ourModelOrEntity, array $defaults = []): Model
{
$this->assertOurModelOrEntity($ourModelOrEntity);
$theirModel = $this->createTheirModel($defaults);
if ($ourModelOrEntity->isEntity()) {
$this->onHookToTheirModel($theirModel, Model::HOOK_AFTER_SAVE, function (Model $theirEntity) use ($ourModelOrEntity) {
$theirValue = $this->theirField !== null
? $theirEntity->get($this->theirField)
: $theirEntity->getId();
if (!$this->getOurField()->compare($this->getOurFieldValue($ourModelOrEntity), $theirValue)) {
$ourModelOrEntity->save([$this->getOurFieldName() => $theirValue]);
}
$theirEntity->reload();
}, [], self::HOOK_PRIORITY_EARLY);
$theirModel->reloadAfterSave = false;
$this->onHookToTheirModel($theirModel, Model::HOOK_AFTER_DELETE, function (Model $theirEntity) use ($ourModelOrEntity) {
$ourModelOrEntity->setNull($this->getOurFieldName());
}, [], self::HOOK_PRIORITY_EARLY);
$ourValue = $this->getOurFieldValue($ourModelOrEntity);
if ($this->getOurFieldName() === $ourModelOrEntity->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();
}
}
return $theirModel;
}
}