1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Storage package |
5
|
|
|
* |
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moss\Storage\Query\Relation; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Many to many relation handler with mediator (pivot) table |
16
|
|
|
* |
17
|
|
|
* @author Michal Wachowski <[email protected]> |
18
|
|
|
* @package Moss\Storage |
19
|
|
|
*/ |
20
|
|
|
class ManyTroughRelation extends AbstractRelation implements RelationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Executes read for one-to-many relation |
24
|
|
|
* |
25
|
|
|
* @param array $result |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function read(&$result) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$relations = []; |
32
|
|
|
$conditions = []; |
33
|
|
|
|
34
|
|
|
foreach ($result as $i => $entity) { |
35
|
|
|
foreach ($this->definition->localKeys() as $local => $refer) { |
36
|
|
|
$conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$relations[$this->buildLocalKey($entity, $this->definition->localKeys())][] = &$result[$i]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$collection = $this->fetch($this->definition->mediator(), $conditions, false); |
43
|
|
|
|
44
|
|
|
// --- MEDIATOR START |
45
|
|
|
|
46
|
|
|
$mediator = []; |
47
|
|
|
$conditions = []; |
48
|
|
|
foreach ($collection as $entity) { |
49
|
|
|
foreach ($this->definition->foreignKeys() as $local => $refer) { |
50
|
|
|
$conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$in = $this->buildForeignKey($entity, $this->definition->localKeys()); |
54
|
|
|
$out = $this->buildLocalKey($entity, $this->definition->foreignKeys()); |
55
|
|
|
$mediator[$out][] = $in; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$collection = $this->fetch($this->definition->entity(), $conditions, true); |
59
|
|
|
|
60
|
|
|
// --- MEDIATOR END |
61
|
|
|
|
62
|
|
|
foreach ($collection as $relEntity) { |
63
|
|
|
$key = $this->buildForeignKey($relEntity, $this->definition->foreignKeys()); |
64
|
|
|
|
65
|
|
|
if (!isset($mediator[$key])) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
foreach ($mediator[$key] as $mkey) { |
69
|
|
|
if (!isset($relations[$mkey])) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
foreach ($relations[$mkey] as &$entity) { |
74
|
|
|
$this->accessor->addPropertyValue($entity, $this->definition->container(), $relEntity); |
75
|
|
|
unset($entity); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Executes write for one-to-many relation |
85
|
|
|
* |
86
|
|
|
* @param array|\ArrayAccess $result |
87
|
|
|
* |
88
|
|
|
* @return array|\ArrayAccess |
89
|
|
|
* @throws RelationException |
90
|
|
|
*/ |
91
|
|
|
public function write(&$result) |
92
|
|
|
{ |
93
|
|
|
$container = $this->accessor->getPropertyValue($result, $this->definition->container()); |
94
|
|
View Code Duplication |
if (empty($container)) { |
|
|
|
|
95
|
|
|
$conditions = []; |
96
|
|
|
foreach ($this->definition->localKeys() as $local => $foreign) { |
97
|
|
|
$conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->cleanup($this->definition->mediator(), [], $conditions); |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($container as $entity) { |
105
|
|
|
$this->storage->write($entity, $this->definition->entity())->execute(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$mediators = []; |
109
|
|
|
|
110
|
|
|
foreach ($container as $entity) { |
111
|
|
|
$mediator = []; |
112
|
|
|
|
113
|
|
|
foreach ($this->definition->localKeys() as $local => $foreign) { |
114
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($result, $local); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ($this->definition->foreignKeys() as $foreign => $local) { |
118
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($entity, $local); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->storage->write($mediator, $this->definition->mediator()) |
122
|
|
|
->execute(); |
123
|
|
|
|
124
|
|
|
$mediators[] = $mediator; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->accessor->setPropertyValue($result, $this->definition->container(), $container); |
128
|
|
|
|
129
|
|
|
$conditions = []; |
130
|
|
|
foreach ($this->definition->localKeys() as $foreign) { |
131
|
|
|
foreach ($mediators as $mediator) { |
132
|
|
|
$conditions[$foreign][] = $this->accessor->getPropertyValue($mediator, $foreign); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->cleanup($this->definition->mediator(), $mediators, $conditions); |
137
|
|
|
|
138
|
|
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Executes delete for one-to-many relation |
143
|
|
|
* |
144
|
|
|
* @param array|\ArrayAccess $result |
145
|
|
|
* |
146
|
|
|
* @return array|\ArrayAccess |
147
|
|
|
* @throws RelationException |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function delete(&$result) |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$container = $this->accessor->getPropertyValue($result, $this->definition->container()); |
152
|
|
|
if (empty($container)) { |
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
foreach ($container as $entity) { |
157
|
|
|
$mediator = []; |
158
|
|
|
|
159
|
|
|
foreach ($this->definition->localKeys() as $local => $foreign) { |
160
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($result, $local); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
foreach ($this->definition->foreignKeys() as $foreign => $local) { |
164
|
|
|
$mediator[$foreign] = $this->accessor->getPropertyValue($entity, $local); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->storage->delete($mediator, $this->definition->mediator())->execute(); |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
$this->accessor->setPropertyValue($result, $this->definition->container(), $container); |
171
|
|
|
|
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.