1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Relations; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Collection\Indexer\EntityIndexer; |
6
|
|
|
use Bdf\Prime\Collection\Indexer\EntityIndexerInterface; |
7
|
|
|
use Bdf\Prime\Query\Contract\EntityJoinable; |
8
|
|
|
use Bdf\Prime\Query\Contract\ReadOperation; |
9
|
|
|
use Bdf\Prime\Query\Contract\WriteOperation; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MorphTo |
13
|
|
|
*/ |
14
|
|
|
class MorphTo extends BelongsTo |
15
|
|
|
{ |
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* {@inheritdoc} |
18
|
|
|
* |
19
|
|
|
* The alias should have #[sub entity] at its end |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
21
|
6 |
|
public function join($query, $alias = null) |
|
|
|
|
22
|
|
|
{ |
23
|
6 |
|
$parts = explode('#', (string)$alias); |
|
|
|
|
24
|
|
|
|
25
|
6 |
|
if (!isset($parts[1])) { |
26
|
1 |
|
throw new \LogicException('Joins are not supported on polymorph without discriminator'); |
27
|
|
|
} |
28
|
|
|
|
29
|
5 |
|
$this->loadDistantFromType(end($parts)); |
30
|
|
|
|
31
|
|
|
//TODO should be in join clause |
|
|
|
|
32
|
5 |
|
$query->where($this->getLocalAlias($query).$this->discriminator, $this->discriminatorValue); |
33
|
|
|
|
34
|
|
|
// Join to the real alias (i.e. without the discriminator) |
35
|
5 |
|
parent::join($query, $parts[0]); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
5 |
|
public function joinRepositories(EntityJoinable $query, $alias = null, $discriminatorValue = null) |
|
|
|
|
42
|
|
|
{ |
43
|
5 |
|
$this->loadDistantFromType($discriminatorValue); |
44
|
|
|
|
45
|
|
|
return [ |
46
|
5 |
|
$alias => $this->relationRepository() |
|
|
|
|
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
#[ReadOperation] |
54
|
44 |
|
public function load(EntityIndexerInterface $collection, array $with = [], $constraints = [], array $without = []) |
55
|
|
|
{ |
56
|
44 |
|
if ($collection->empty()) { |
57
|
9 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
39 |
|
$with = $this->rearrangeWith($with); |
61
|
39 |
|
$without = $this->rearrangeWithout($without); |
62
|
|
|
|
63
|
39 |
|
foreach ($collection->by($this->discriminator) as $type => $chunk) { |
64
|
39 |
|
$this->loadDistantFromType($type); |
65
|
|
|
|
66
|
39 |
|
parent::load( |
67
|
39 |
|
EntityIndexer::fromArray($this->local->mapper(), $chunk), |
68
|
39 |
|
$with[$type], |
69
|
39 |
|
$constraints, |
70
|
39 |
|
$without[$type] |
71
|
|
|
); |
72
|
|
|
} |
73
|
38 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* @fixme Do not works with EntityCollection |
|
|
|
|
79
|
|
|
* |
80
|
|
|
* @param object $owner |
|
|
|
|
81
|
|
|
*/ |
82
|
2 |
|
public function link($owner) |
83
|
|
|
{ |
84
|
2 |
|
$this->loadDistantFrom($owner); |
85
|
|
|
|
86
|
2 |
|
return parent::link($owner); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
2 |
|
public function associate($owner, $entity) |
93
|
|
|
{ |
94
|
2 |
|
$this->loadDistantFromType($this->discriminator(get_class($entity))); |
95
|
|
|
|
96
|
2 |
|
return parent::associate($owner, $entity); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
|
|
#[WriteOperation] |
103
|
4 |
|
public function saveAll($owner, array $relations = []) |
104
|
|
|
{ |
105
|
4 |
|
$relations = $this->rearrangeWith($relations); |
106
|
4 |
|
$this->loadDistantFrom($owner); |
107
|
|
|
|
108
|
4 |
|
return parent::saveAll($owner, $relations[$this->discriminatorValue]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
#[WriteOperation] |
115
|
4 |
|
public function deleteAll($owner, array $relations = []) |
116
|
|
|
{ |
117
|
4 |
|
$relations = $this->rearrangeWith($relations); |
118
|
4 |
|
$this->loadDistantFrom($owner); |
119
|
|
|
|
120
|
4 |
|
return parent::deleteAll($owner, $relations[$this->discriminatorValue]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Change the current discriminator value |
125
|
|
|
* and update the distant repository of this relation |
126
|
|
|
* |
127
|
|
|
* @param mixed $type |
128
|
|
|
*/ |
129
|
46 |
|
protected function loadDistantFromType($type) |
130
|
|
|
{ |
131
|
46 |
|
$this->discriminatorValue = $type; |
132
|
|
|
|
133
|
46 |
|
$this->updateDistantInfos(); |
134
|
46 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Change the current discriminator value from the entity |
138
|
|
|
* and update the distant repository of this relation |
139
|
|
|
* |
140
|
|
|
* @param object $entity |
141
|
|
|
*/ |
142
|
10 |
|
protected function loadDistantFrom($entity) |
143
|
|
|
{ |
144
|
10 |
|
$this->updateDiscriminatorValue($entity); |
145
|
|
|
|
146
|
10 |
|
$this->updateDistantInfos(); |
147
|
10 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Update the distant repository of this relation |
151
|
|
|
*/ |
152
|
48 |
|
protected function updateDistantInfos() |
153
|
|
|
{ |
154
|
48 |
|
$infos = $this->map($this->discriminatorValue); |
155
|
|
|
|
156
|
48 |
|
$this->distant = $this->local->repository($infos['entity']); |
157
|
48 |
|
$this->distantKey = $infos['distantKey']; |
158
|
|
|
|
159
|
48 |
|
$this->setConstraints(isset($infos['constraints']) ? $infos['constraints'] : []); |
160
|
48 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
|
|
|
|
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
39 |
|
protected function relationQuery($keys, $constraints) |
166
|
|
|
{ |
167
|
39 |
|
return $this->query($keys, $constraints)->by($this->distantKey); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|